BlueChips23
BlueChips23

Reputation: 1951

jQuery AJAX GET to a Servlet doesn't show the JSP page - instead returns with HTML code

I know I am doing something stupid and wrong but I can't figure it out. Please help.

I am using Apache Tomcat Servlet 7 with JQuery.

On my web page, when I click on a button, a Jquery GET call is made to my servlet URL. The servlet, after receiving the data (in JSON format), processes the data and creates some result parameters. Then the servlet passes those parameters to a JSP file to load the JSP page. So in summary, when the user clicks on a button, processing happens on the servlet and the result JSP page is loaded.

The problem is, when I make a Jquery GET call from the button, no result page is loaded. Instead when I do console.log(request), I see the entire html content of the rendered JSP page.

Here's my code on the web application when the button is clicked:

var request;
if (request) {
   request.abort();
}
request = $.ajax({
    cache: false,
    type: 'GET',
    url:'http://<MyServerAddress>:8080/<App>/<Servlet>',
     data: {
        'queryType' : 'clickButton',
        'data' : JSON.stringify(buttonPageInfo)
         },
}); 
request.done(function (response, textStatus, jqXHR){
    console.log("Response: " + JSON.stringify(response)); 
        // shows the entire html content of the processed JSP page that I want to load
     });

Here's the code on the server side:

protected void doGet(HttpServletRequest request, HttpServletResponse response) {
   processData(request, response);
}

protected void processData(request, response) {
 /* Do some processing on the data and come up with results */
 RequestDispatcher rd = getServletContext().getRequestDispatcher("/myJSPPage.jsp");
 rd.forward(request, response); 
}

Right after rd.forward(request, response), I expect my web page forward to myJSPPage.jsp to show the result, but nothing happens. Instead console.log("Response: " + JSON.stringify(response)); shows the entire HTML content of the JSP page with the results I am expecting.

What am I doing wrong?

Upvotes: 1

Views: 1410

Answers (2)

BetaRide
BetaRide

Reputation: 16834

Just call windows.open() if you want to do a normal page call. No need to use ajax for that.

Upvotes: 1

Steve C
Steve C

Reputation: 19445

You are getting HTML because you are forwarding to a JSP.

Just remove the RequestDispatcher stuff and make sure that you have flushed the response data.

Upvotes: 2

Related Questions