I want to call a Servlet from ajax in a Java WebApp, using Tomcat7.
I used the annotation @WebServlet("/VServlet") to mapping it in the WebApp, because of Tomcat7 support Servlet 3.0. I call the servlet with the jquery command "$.getJSON('VServlet', function(data){ ... });" but if I use the string 'VServlet', it doesn't work.
Only if I use the entire url 'http:// localhost:8080/WebAppName/VServlet' it works, but only in my pc. However I have to deploy it on a server that has a commercial domain name, obviously different from "localhost".
Please, can anyone tell me if there is a way to address the servlet with a relative url? Because if I use a string like 'http://my.domain.it:8080/WebAppName/VServlet' it doesn't work on the Tomcat7 that is on the server machine.
Thanks.
------------------EDIT-----------------------
I try to explain better my problem. I have a Java Servlet that return a json structure. The servlet is called in a javascript file that have to draw a graph in a web page, building a SVG image. I have to deploy this web app on a commercial Windows server with Tomcat7 installed. The web app generally works. Any errors are displayed from the user point of view. Simply the image doesn't appear if I use that strings I wrote above, to call the servlet. I suppose that the servlet doesn't respond because I use a wrong address/naming. If I use an absolute url, the servlet responds to the js caller, but I need a relative string, so if the domain name of the server will change, I won't change the code of the js file too.
When you call
you are making a HTTP GET request to an URL relative to the current URL. That is, if you're in
/webapp/users/user.xhtmlyou'll be sending a request to/webapp/users/VServletwhich obviously doesn't match the URL pattern of your servlet and not the thing you're trying to achieve.You need to take into account the context path of a JSF application that's available in view as
#{request.contextPath}to build up a proper absolute URL:This will send a request to
http://localhost:8080/webapp/VServlet.In case you need to send request to a path relative to the root, such request URLs start with a slash,
/and represent URLs relative to the root, no matter where you're right now:This will send a request to
http://localhost:8080/VServlet.