0

I have a JSP page which has a form with an input text. When I submit, it goes to a servlet. The servlet processes and creates some objects and sets in request using request.setAttribute(). It then forwards to a page which has some custom JSP tags which use the objects set in servlet.

I want to replace this by Ajax. I have implemented it as follows:

First, the form is submitted through Ajax using POST, the objects which were set earlier using request.setAttribute() are converted to JSON string and sent as response. On success, there is another Ajax GET call to a JSP page which has my custom tags and the JSON string is passed as parameter. The response of this Ajax call is set inside a div.

But it are two Ajax requests. How can I make it a single Ajax request instead?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Prabhat
  • 2,261
  • 7
  • 46
  • 74
  • Close one but here I want to minimize JS as much as possible. AJAX uses JS anyway but I want to make use of existing JSP instead of constructing the page in JS or in servlet. – Prabhat Jun 02 '11 at 12:01
  • possible duplicate of [How to use Servlets and Ajax?](http://stackoverflow.com/questions/4112686) You can make use of existing JSPs. Just add a `` wherein you progressively enhances the page without changing its core functionality. Only the servlet has to be changed to do a conditional check if it's an ajax request or not and then handle accordingly. See also [Simple calculator in JSP](http://stackoverflow.com/questions/4114742). Not using jQuery means that you've to write at least 10 times as much JS code to get it to work in all browsers the world is aware of. – BalusC Jun 02 '11 at 12:02
  • @BalusC - When I said I want to make use of existing JSP, I meant make use of target JSP which has my custom tags. In 'Simple calculator in JSP' example it is forwarded to same page. I want to forward request from say index.jsp to next.jsp which has my custom tags using AJAX. The data for next.jsp must be provided by objects set in servlet. – Prabhat Jun 02 '11 at 12:23
  • That isn't a requirement. That's just how MVC works. – BalusC Jun 02 '11 at 12:26
  • @BalusC - Yes. But how do I set objects or JSON strings of those objects in servlet and include another JSP inside a div with one AJAX request. – Prabhat Jun 02 '11 at 12:31
  • So you want to return HTML instead of JSON? Then just forward to the JSP. – BalusC Jun 02 '11 at 12:33
  • @BalusC - In AJAX, I can send the JSON strings of the objects as response. But for the end user I need include another JSP (with custom tags) inside div. For that, I must make another AJAX request and pass the JSON string. I wanted to know is there anyway I could do it all with one AJAX request i.e. submit a form and I can directly assign response to a div. – Prabhat Jun 02 '11 at 12:37
  • You want to send the output of JSP as Ajax response. The output of JSP is HTML. You just need to send it as HTML and assign the HTML plain to the div. E.g. `$('#divid').html(responseHtml);` – BalusC Jun 02 '11 at 12:42
  • @BalusC - Sorry. I guess you misunderstood me. Currently this is how it works. I have index.jsp and next.jsp(the one with custom tags). When I click submit in index.jsp, it goes to servlet, sets some objects, converts to JSON string, sets it as response and returns back to index.jsp. Thats one AJAX request. Next I am using this JSON string for another AJAX request which is a GET request i.e. xmlhttp.open("GET", "next.jsp?jsonstring=" + jsonstring, true); The result of this is what is set to div.innerHTML. So totally 2 AJAX requests. I want to do this with one. – Prabhat Jun 02 '11 at 12:58
  • 1
    Yes. Instead of returning JSON in first ajax request you need to forward to `next.jsp?jsonstring=jsonstring`. This effectively returns HTML. – BalusC Jun 02 '11 at 13:00
  • @BalusC - Thanks. Now I am getting closer to answer. If I forward to next.jsp, wont the page refresh ? Isnt it same as simple HTTP request ? – Prabhat Jun 02 '11 at 13:04
  • No. It's been invoked by JS/ajax, right? You should not confuse forward with redirect. A forward effectively changes the response, not the request. – BalusC Jun 02 '11 at 13:05
  • Yes. It will be invoked by AJAX. So when I give forward in servlet, the response (responseText) I receive in AJAX is actually rendered (HTML) next.jsp ? – Prabhat Jun 02 '11 at 13:14
  • OK, I posted an answer now the concrete question is finally clear. I've edited your question as well. Your **real** question is after all "But it are two Ajax requests. How can I make it a single Ajax request instead?". – BalusC Jun 02 '11 at 14:12

1 Answers1

0

As per the comments, you just need to let the first request forward to the desired JSP instead of returning the JSON string which you in turn pass back to the JSP.

request.getRequestDispatcher("/WEB-INF/next.jsp?jsonstring=" + jsonstring).forward(request, response);
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555