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.
if your js code is inside jsp page Instead of the code
$.getJSON('VServlet', function(data){ ... });
write
"$.getJSON('${request.contextPath}/VServlet', function(data){ ... });
other wise if this code is in some js file in a function something like
and you are calling function someFunction() from jsp then you add a parameter to the function like this -
and now call it from jsp like this -
someFunction('${request.contextPath}/VServlet')
because ${request.contextPath}/VServlet is an el tag and it will get compiled only in jsp page