Jquery's method of accessing servlet and returning data to the page, jqueryservlet
This example describes how jquery accesses servlet and returns data to the page. Share it with you for your reference. The specific implementation method is as follows:
1. servlet: AjaxServlet. java is as follows:
Copy codeThe Code is as follows: package com. panlong. servlet;
Import java. io. IOException;
Import java. io. PrintWriter;
Import java.net. URLDecoder;
Import javax. servlet. ServletException;
Import javax. servlet. http. HttpServlet;
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
Public class AjaxServlet extends HttpServlet {
Private static final long serialVersionUID = 1L;
Protected void doGet (HttpServletRequest req, HttpServletResponse resp)
Throws ServletException, IOException {
Integer total = (Integer) req. getSession (). getAttribute ("total ");
Int temp = 0;
If (total = null ){
Temp = 1;
} Else {
Temp = total. intValue () + 1;
}
Req. getSession (). setAttribute ("total", temp );
Try {
// 1. Take the Parameter
Resp. setContentType ("text/html; charset = GBK ");
PrintWriter out = resp. getWriter ();
String old = req. getParameter ("name ");
// 2. Check whether the parameter is correct.
// String name = new String (old. getBytes ("iso8859-1"), "UTF-8 ");
String name = URLDecoder. decode (old, "UTF-8 ");
If ("". equals (old) | old = null ){
Out. println ("username required ");
} Else {
If ("liling". equals (name )){
Out. println ("congratulations, logon successful ");
Return;
} Else {
Out. println ("this user name is not registered, you can register [" + name + "] This User name" + temp );
}
}
} Catch (Exception e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
// 3. Check the operation
}
Protected void doPost (HttpServletRequest req, HttpServletResponse resp)
Throws ServletException, IOException {
DoGet (req, resp );
}
}
2. verify. js is as follows:
Copy codeThe Code is as follows: function verify (){
// Method 1 to solve the problem of Chinese garbled characters, the page side sends data as an encodeURI, the server uses new String (old. getBytes ("iso8859-1"), "UTF-8 ");
// Method 2 to solve the problem of Chinese Garbled text, the data sent by the page for two encodeURI, the server uses String name = URLDecoder. decode (old, "UTF-8 ");
Var url = "servlet/AjaxServlet? Name = "+ encodeURI ($ (" # userName "). val ()));
Url = convertURL (url );
$. Get (url, null, function (data ){
$ ("# Result" pai.html (data );
});
}
// Add a time limit to the url address. Sorry, the browser does not read the cache.
Function convertURL (url ){
// Obtain the timestamp
Var timstamp = (new Date (). valueOf ();
// Splice the timestamp information to the url
If (url. indexOf ("? ")> = 0 ){
Url = url + "& t =" + timstamp;
} Else {
Url = url + "? T = "+ timstamp;
}
Return url;
}
3. the foreground page is as follows:
Copy codeThe Code is as follows: <! DOCTYPE html>
<Html>
<Head>
<Title> AJAX instance </title>
<Meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3">
<Meta http-equiv = "description" content = "this is my page">
<Meta http-equiv = "content-type" content = "text/html; charset = GBK">
<Script type = "text/javascript" src = "js/verify. js"> </script>
<Script type = "text/javascript" src = "js/jquery. js"> </script>
<! -- <Link rel = "stylesheet" type = "text/css" href = "./styles.css"> -->
</Head>
<Body>
<Font color = "blue" size = "2"> enter the User name: </font>
<Input type = "text" id = "userName"/> <font color = "red" size = "2"> <span id = "result"> * </span> </font> <br/>
<! -- <Div id = "result"> </div> -->
<Input type = "submit" name = "submit" value = "submit" onclick = "verify ()"/>
</Body>
</Html>
I hope this article will help you with Ajax programming.