Code used to simulate the communication between the jQuery ajax server and the client

Source: Internet
Author: User

Functions:

If the user name is blank, the system prompts "the user name cannot be blank"

If the user name prompts "user name [xxxxxx] already exists, use another user name, 4"

If the user name does not exist, the message "user name [xxxxxx] does not exist, you can use this user name to register, 5"

The running effect is as follows:

Directory structure: server-side AjaxServer Copy codeThe Code is as follows: package com. ljq. test;
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;
@ SuppressWarnings ("serial ")
Public class AjaxServer extends HttpServlet {
@ Override
Protected void doGet (HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
Try {
// Set page UTF-8 encoding
Response. setContentType ("text/html; charset = UTF-8 ");
PrintWriter out = response. getWriter ();
Integer total = (Integer) request. getSession (). getAttribute ("total ");
Int temp = 0;
If (total = null ){
Temp = 1;
} Else {
Temp = total. intValue () + 1;
}
Request. getSession (). setAttribute ("total", temp );
// 1. Take the Parameter
String param = request. getParameter ("name ");
String name = URLDecoder. decode (param, "UTF-8 ");
// 2. Check whether the parameter is valid
If (param = null | param. length () = 0 ){
Out. println ("the user name cannot be blank ");
} Else {
// 3. Verification
If (name. equals ("linjiqin ")){
// 4. Return result data
Out. println ("username [" + name + "] already exists. Please use another username," + temp );
} Else {
Out. println ("user name [" + name + "] does not exist. You can use this user name to register," + temp );
}
}
} Catch (Exception e ){
E. printStackTrace ();
}
}
@ Override
Protected void doPost (HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
DoGet (request, response );
}
}

Configure web. xmlCopy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<Web-app version = "2.4" xmlns = "http://java.sun.com/xml/ns/j2ee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemaLocation = "http://java.sun.com/xml/ns/j2ee
Http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd>
<Servlet>
<Servlet-name> AjaxServer </servlet-name>
<Servlet-class> com. ljq. test. AjaxServer </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-name> AjaxServer </servlet-name>
<Url-pattern>/servlet/ajaxServer </url-pattern>
</Servlet-mapping>
<Welcome-file-list>
<Welcome-file> index. jsp </welcome-file>
</Welcome-file-list>
</Web-app>

Index. jsp pageCopy codeThe Code is as follows: <% @ page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>
<%
String path = request. getContextPath ();
String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";
%>
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Base href = "<% = basePath %>">
<Title> My JSP 'index. jsp 'starting page </title>
<Meta http-equiv = "pragma" content = "no-cache">
<Meta http-equiv = "cache-control" content = "no-cache">
<Meta http-equiv = "expires" content = "0">
<Meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3">
<Meta http-equiv = "description" content = "This is my page">
<Script type = "text/javascript" src = "<% = request. getContextPath () %>/js/jquery-1.3.1.js"> </script>
<Script type = "text/javascript" src = "<% = request. getContextPath () %>/js/validate. js"> </script>
</Head>
<Body>
<! -- Html5 standard: first, the tag name must be in lower case, and then the tag must be closed. The third attribute name follows the camel naming method. The fourth attribute value must be in double quotation marks. -->
Enter the User name: <br/>
<Input id = "userName"/>
<Input type = "button" value = "Verification" onclick = "verify ();"/>
<Div id = "result"> </div>
</Body>
</Html>

Validate. jsCopy codeThe Code is as follows: function verify (){
// Solve the Chinese garbled Method 1: The page side of the data for an encodeURI, server segment using new String (name. getBytes ("iso8859-1"), "UTF-8 ");
// Solve Chinese garbled Method 2: data sent by the page for two encodeURI, the server segment uses URLDecoder. decode (name, "UTF-8 ")
Var url = "servlet/ajaxServer? Name = "+ encodeURI ($ (" # userName "). val ()));
// Do not add "/" before the url; otherwise, the url cannot be accessed.
// Var url = "/servlet/ajaxServer? Name = "+ encodeURI ($ (" # userName "). val (); // Error
Url = convertURL (url );
$. Get (url, null, function (data ){
$ ("# Result" pai.html (data );
});
}
// Add a timestamp to the url address. The browser is cheated and the cache is not read.
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;
}

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.