::::::::::::::::::::::::::
Home
Home Page
Method of generation of a code
Displays
Result of binding of the data
Systems of patterns of pages
Data acquisition without use XML
Data acquisition without use XML
Use JSON on the party of the client
Improved online shop
Review Ajax
Departure XMLHttpRequest
Processing of search servleta
 The search processing with help JavaScript
Problems of use Ajax
As it was more than once spoken, organization W3C is engaged mainly in development and creation of b
OASIS
Hands off PHP!
Automatic redirect (Auto Redirect) on PHP
Breaking of passwords becomes more accessible
Web2.0 Layout
Links
:::::::::::::::::::::::::

 

Processing of search servleta


Processing XMLHttpRequest with the help servleta same, as well as processing of usual HTTP-search from a browser. The coded data sent in contents of search POST, can be received with the help of calls HttpServletRequest.getParameter (). Ajax requests participation in HttpSession, same, as well as in regular Web searches from the application. It is useful for an example of the script of a basket of the buyer as allows me to conclude a status of the user basket of the buyer in JavaBeans and to keep this status during session between two searches.


Listing 4 - a part simple servleta, which processes Ajax searches to update a basket of the buyer. Cart it turns out from session of the user and his{its} status is updated according to parameters of search. Cart then serializuetsja in a XML-format, and XML it is formed in ServletResponse. It is important to establish a content type of search in application/xml, otherwise XMLHttpRequest will not analyze contents of the answer.


Listing 4. A code servleta for processing Ajax-searches



public void doPost (HttpServletRequest req,

HttpServletResponse res)

throws java.io. IOException {


Cart cart = getCartFromSession (req);


String action = req.getParameter ("action");

String item = req.getParameter ("item");


if ((action! = null) ** (item! = null)) {


// To add or remove a product from Cart

if ("add" .equals (action)) {

cart.addItem (item);


} else if ("remove" .equals (action)) {

cart.removeItems (item);


}

}


// Serializuem status Cart in a XML-format

String cartXml = cart.toXml ();


// We write down received XML in search.

res.setContentType ("application/xml");

res.getWriter () .write (cartXml);

}


Listing 5 sets an example a XML-code, received with the help of method Cart.toXml (). To make it it is simple enough. Remember attribute generated in cart an element, which is the time label received with help System.currentTimeMillis ().


Listing 5. An example of serialization in a XML-format of object Cart



<? xml version = " 1.0"?>

<cart generated = "1123969988414" total = " $ 171.95">

<item code = "hat001">

<name> Hat </name>

<quantity> 2 </quantity>

</item>

<item code = "cha001">

<name> Chair </name>

<quantity> 1 </quantity>

</item>

<item code = "dog001">

<name> Dog </name>

<quantity> 1 </quantity>

</item>

</cart>


If you will look on Cart.java in the source of the application accessible in section Loading, you will see, that XML it turns out simply with the help of connection of lines together. Applied in an example, this way - frequently the worst way to receive XML from a code in language Java. I shall offer receptions more interestingly and better in the following part of this series.


Now you know, that CartServlet is requested with help XMLHttpRequest. The following step will be returning to a code of the client where you can see how the answer in a XML-format will be used for updating a status of page.