Java Servlet on Tomcat called from AJAX

1.6k views Asked by At

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.

2

There are 2 answers

0
Ravi Kumar On

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

function someFunction(){
     $.getJSON('VServlet', function(data){ ... });
}

and you are calling function someFunction() from jsp then you add a parameter to the function like this -

function someFunction(url){
         $.getJSON('VServlet', function(data){ ... });
    }

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

4
skuntsel On

When you call

$.getJSON('VServlet', ... );

you are making a HTTP GET request to an URL relative to the current URL. That is, if you're in /webapp/users/user.xhtml you'll be sending a request to /webapp/users/VServlet which 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:

$.getJSON('#{request.contextPath}/VServlet', ... );

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:

$.getJSON('/VServlet', ... );

This will send a request to http://localhost:8080/VServlet.