The servlet code for Checkusername.java is as follows:
Package com.web;
Import java.io.IOException;
Import Java.io.PrintWriter;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
public class Checkusername extends HttpServlet {
public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
Httpservletresponse.setcontenttype ("Text/html:charset=utf-8");
Request.setcharacterencoding ("Utf-8");
Response.setcharacterencoding ("Utf-8");
PrintWriter Out=response.getwriter ();
Get page parameters
String username=request.getparameter ("UserName");
Does the detection parameter match the corresponding data in db
if (username==null| | Username.length () ==0) {
Out.println ("User name cannot be empty");
}else{
Checksum operation
if (Username.equals ("Tfq")) {
Return page data
Out.println ("User name already exists");
}else{
OUT.PRINTLN ("Congratulations! This user is available");
}
}
}
public void DoPost (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
Request.setcharacterencoding ("Utf-8");
Response.setcharacterencoding ("Utf-8");
Doget (request, response);
}
}
The Validata.js page code is as follows:
Verify that the user exists
function Validate () {
1 getting the contents of a text box
document.getElementById ("UserName"); Dom's Way
jquery lookup node way, parameter # Plus id attribute value can find a node
var obj = $ ("#userName");
Solve Chinese garbled 1: The data from the page end is encodeURI (Obj.val ()), the server uses the new String (Username.getbytes ("Iso8859-1", "Utf-8"))
Solve Chinese garbled 2: page end make two times encodeURI (encodeURI (Obj.val ()), Server End with Strng usernamepara=urldecoder.decode (userName, "utf-8");
var userName = Obj.val ();
Alert ("UserName:" + userName);
2 the servlet to send the retrieved content to the server
Encapsulation of calling jquery's XMLHttpRequest object GET request
$.get ("servlet/checkusername?username=" + userName, NULL, function (data) {
3 receive the transmitter returned the data 4 the returned data in the div display
$ ("#showResult"). HTML (data);
});
}
The checkusername.html page code is as follows:
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<title>my JSP ' checkusername.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= "Js/jquery-1.4.2.js" ></script>
<script type= "Text/javascript" src= "Js/validata.js" ></script>
<body>
<center>
<br/>
User name: <input type= "text" id= "UserName"/>
<input type= "button" value= "checksum" onclick= "Validate ()"/>
<div id= "Showresult" ></div>
</center>
</body>