How to send a single Ajax request instead of two - ajax

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?

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

Related

Why dont request.getParameter("html_control_id") work in AJAX call in JSP?

I am sending ajax request from 1 JSP to other JSP. In called JSP when I try to get value of a HTML control with the help of request.getParameter("HTML_control_name/id") It returns null.
request.getParameter("HTML_control_name/id")
request.getParameter() returns the string of the parameters sent in the HTTP Requests.
You can get the value of your form attributes through request.getParameter("Name Of form Attribute")

Is Ajax with JSON or Ajax with dojo are same

I am new in Ajax . Want to use Ajax with Json . I am searching the tutorial for this and i find this.
I want to ask is i am in right direction ?
Is both things are same Ajax with Json and Ajax with dojo?
Not the same
Ajax is a technology that send request and accept data asynchronously(do not need to reload page).
You can use JSON or XML to send the data or just use the string.
When you do the Ajax request, any data type(like array, object, number..) except string will lost their data type and become string, so If you want to reserve their type, you must use data transmit format like JSON and XML.
Dojo just a library which have easier method for doing Ajax. You also can use jQuery, Angular,..Whatever even JavaScript native XMLHttpRequest.

How to send Object via Ajax to Servlet

How do I send an element of type object via ajax to a servlet?
In the ajax I am passing the value as follows below:
data: { mapList : mapLists }
To get the value in the Servlet am doing follows below:
Object o = request.getAttribute("mapList");
System.out.println(o);
However, the returned value is always null. What should I do to get around this problem?
Change your ajax datas by :
data: { 'mapList' : mapLists }
On HTTP GET or POST requests you can only send a list of key/value pairs as parameters to the server so you will have to manually serialize your object to send its attributes in this format.
You should better use HttpServletRequest.getParameter(String) in place of HttpServletRequest.getAttribute(String). Also, what you get as an HTTP GET/POST parameter will always be received in the servlet as a String.
I assume that you are using jQuery to send the ajax request. I also asume that your mapLists variable is a json object. As far as I know, jQuery doesn't automatically convert a json object to a key/value pair HTTP parameter list so you will have to do it by yourself and then parse it back in the servlet. You can use JSON.stringify() to convert your json object or you can serialize it manually.

Java Bean not updating unless refreshing the page

I'm just a rookie, and I'm trying to learn JSP (I'm using scriptlet), servlet, beans and Ajax.
I'm validating some registration forms by calling a servlet (through Ajax). Every time a character is inserted, the Ajax function is recalled and "value" is sent to a servlet whose job is to check if this value is OK. The servlet gets an HttpSession, then it operates on the passed value and saves it into a bean and finally sets session.setAttribute("bean_name", bean). In JSP I inserted the jsp:usebean tag with scope=session. I got no problem retrieving values, for example <%= bean.getUsername()%>, but the problem is all the form values don't get updated unless I refresh the page.
What could it be?
You make the AJAX call to the Servlet. The Servlet does what it needs to do, and renders a response. However, the AJAX call was made by means of JavaScript, after the page was already rendered.
What you need to do is to edit the JavaScript code that issues the AJAX call. Once the AJAX call takes place, your JavaScript code has to wait until a response is returned from the Servlet, and then use client-side facilities (such as DHTML) to edit the already-rendered page.

Zend_Form csrf validation for ajax queries

Here is how I add the csrf to the form
$this->addElement('hash', 'csrf', array('ignore' => false));
When this happens the session is created, Then when the user sends an ajax request, the values in the request are validated by creating an instance of the form, and the form is always valid for the first ajax request since the beginning of initial request which created the html output,
When the ajax request has been sent for the second time something different happens,
That instance of the form has a different csrf value than the originally made one, and when my code is done, the originally created session is destroyed as well, so there is no session to check the received the values against, and hence the form doesn't validated and the following error occurs.
No token was provided to match against
Any ideas at which event, the csrf values of the form are automatically stored in the session?
The hash value is generated at render time and invalidated after the each request.
If you want to continue using Zend_Form_Element_Hash in your AJAX form where the form may submit several times, your AJAX response should include the new hash value. Upon receiving the response, you should update the form data.
There s a solution without any to render in the view : Totaly ajax !
How to use Zend Framework Form Hash (token) with AJAX

Resources