Features are as follows:
If user name is null prompt "User name cannot be empty"
If the user name is prompted "user name [xxxxxx] already exists, use a different username, 4"
If the user name does not exist, "user name [xxxxxx] does not exist, you can use that username to register, 5"
The operation effect is as follows:
Server-Side Ajaxserver
Copy Code code 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 parameters
String param = request.getparameter ("name");
String name = Urldecoder.decode (param, "UTF-8");
2. Check whether the parameters are valid
if (param = null | | param.length () = 0) {
Out.println ("User name cannot be empty");
} else {
3. Check operation
if (Name.equals ("Linjiqin")) {
4. Return the result data
Out.println ("User name [" + name + "] already exists, please use other username," + temp);
} else {
Out.println ("User name [" + name + "] does not exist and can be registered with the user name," + temp);
}
}
catch (Exception e) {
E.printstacktrace ();
}
}
@Override
protected void DoPost (HttpServletRequest request,
HttpServletResponse response) throws Servletexception, IOException {
Doget (request, response);
}
}
Configure Web.xml
Copy Code code 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 page
Copy Code code 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" >
<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 are 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>
<body>
<!--HTML5 Standard: First tag name to lowercase, followed by the label must be closed, the third property name follows Camel nomenclature, the fourth attribute value must be in double quotes-->
Please enter a user name:<br/>
<input id= "UserName"/>
<input type= "button" value= "Verify" onclick= "verify ();" />
<div id= "Result" ></div>
</body>
Validate.js
Copy Code code as follows:
Function Verify () {
///Resolve Chinese garbled method one: the data emitted at the page end is once encodeURI, server segment uses new String (Name.getbytes ("iso8859-1"), "UTF-8");
//Resolve Chinese garbled Method two: page end of the data issued two times encodeURI, server segment using Urldecoder.decode (name, "UTF-8")
var url = "Servlet/ajaxserver?name= "+ encodeURI (encodeURI ($ (" #userName "). Val ());
//Note Do not add "/" before the URL, otherwise you cannot access the URL
//var url = "/servlet/ajaxserver?name=" + encodeURI (encodeURI ($ ("#userName"). Val ()) ); Error
URL = converturl (URL);
$.get (URL, null, function (data) {
$ ("#result"). HTML (data);
});
}
///Add timestamp to URL address, cheat browser, do not read cache
function Converturl (URL) {
//get timestamp
var timstamp = (new Date ()). V Alueof ();
//The timestamp information is spliced to the URL
if (Url.indexof ("?") >= 0) {
url = url + "&t=" + timstamp;
} else {
URL = url + "? t=" + Timstamp;
}
return URL;
}