Jquery's method of accessing servlet and returning data to the page, jqueryservlet

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.