1. belowCodeObtain the request information servlet for the server, such as struts.
Package com. ADU;
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 ajaxserver extends httpservlet {
Private Static final long serialversionuid =-1391282336646351379l;
Protected void doget (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
Response. setcontenttype ("text/html ");
Response. setcharacterencoding ("UTF-8 ");
Printwriter out = response. getwriter ();
Revenue collection
// Obtain the data submitted by jquery URL on the page for processing
// String username = request. getparameter ("name ");
// String Password = request. getparameter ("password ");
String name = request. getparameter ("name ");
String Password = request. getparameter ("password ");
// Convert character set encoding to avoid Chinese garbled characters
String username = urldecoder. Decode (name, "UTF-8 ");
String Password = urldecoder. Decode (password, "UTF-8 ");
// Perform logical judgment to check whether the input is valid
If (username. Length ()! = 0 & username! = NULL & password. Length ()! = 0 & password! = NULL ){
If (username. Equals ("mawanli") & amp; password. Equals ("123 ")){
// Out. Print ("Welcome *" + username + "* visit my web! ");
Out. Print ("Welcome *" + username + "* access! ");
Out. Flush ();
Out. Close ();
} Else {
Out. Print ("Sorry Please login again! ");
// Out. Print ("enter the correct user name and password! ");
}
}
Else {
Out. Print ("Please input right name and password! ");
// Out. Print ("Log on again! ");
}
}
@ Override
Protected void dopost (httpservletrequest req, httpservletresponse resp)
Throws servletexception, ioexception {
Resp. setcontenttype ("text/html ");
This. doget (req, resp );
}
}
2. The code for the page test 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> test Ajax authentication logon </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">
<! --
<LINK rel = "stylesheet" type = "text/CSS" href = "styles.css">
-->
<SCRIPT type = "text/JavaScript" src = "JS/jquery-1.6.min.js"> </SCRIPT> <SCRIPT type = "text/JavaScript">
// Add a timestamp to prevent different URLs from being tested each time. This solves the browser cache problem.
Function converturl (URL ){
VaR timestamp = (new date (). valueof ();
If (URL. indexof ("? ")> = 0 ){
Url = URL + "& t =" + timestamp;
} Else {hair style 123
Url = URL + "? T = "+ timestamp;
}
Return URL;
}
// Encodeuri can be used to solve the problem of Chinese garbled characters when page values are passed.
Function validate (){
VaR name = encodeuri ($ ("# username"). Val ()));
VaR Password = encodeuri ($ ("# password"). Val ()));
VaR url = "ajaxserver? Name = "+ name +" & Password = "+ password;
Url = converturl (URL );
$. Get (URL, null, function (data ){
$ ("# Result" pai.html (data );
});
}
</SCRIPT>
<Body>
<Div align = "center">
<Div>
<Form action = "#" method = "get">
<Table>
<Tr> <TD> User Name: </TD> <input type = "text" name = "username" id = "username"/> </TD> </tr>
<Tr> <TD> password & nbsp; Code: </TD> <input type = "password" id = "password"/> </TD> </tr>
<Tr align = "right"> <TD colspan = "2"> <input type = "button" value = "login" onclick = "Validate ()"/>
<Input type = "reset" value = "cancel"/> </TD> </tr>
</Table>
</Form>
<Div id = "result"> </div>
</Div>
<HR width = "600px" align = "center" size = "3" color = "green"/>
</Div>
</Body>
</Html>
<PRE name = "code" class = "Java"> </PRE> [align = left] [/align]