开发者

How to send a single Ajax request instead of two

开发者 https://www.devze.com 2023-03-09 19:53 出处:网络
I have a JSP page which has a for开发者_StackOverflowm 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.setAttr

I have a JSP page which has a for开发者_StackOverflowm 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?


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消