Reprint to Http://blog.sina.com.cn/s/blog_680288f001014uy2.html, and so on his own test success and then preach their own written code.
Main process: The servlet reads the data in the TXT and passes the data into JS, and the HTML uses JS to get the TXT data and judge it based on the event trigger.
1.usercheck.html
<meta http-equiv= "Content-type" content= "TEXT/HTML;CHARSET=GBK" >
<linkhref= ". /lib/ligerui/skins/aqua/css/usertext.css "rel=" stylesheet "type=" Text/css "/>
<script src= ". /lib/jquery/jquery-1.3.2.min.js "type=" Text/javascript "></script>
<script src= ". /lib/ligerui/js/plugins/useropera.js "type=" Text/javascript "></script>
<body>
Fill in the user name
<input type= "text" id= "Text1" class= "UserClass"/>
<input type= "button" value= "Test" id= "Verifybutton"/>
<div id= "Result" ></div>
</body>
2.usertext.css
. userclass {
border:1px solid red;
Background-image:url (.. /.. /.. /.. /.. /images/star.gif);
Background-repeat:repeat-x;
Background-position:bottom;
}
3.useropera.js
$ (document). Ready (function () {
$ ("#verifyButton"). Click (function () {
var username=$ ("#text1"). Val ();
if (username== "") {
Alert ("User name cannot be empty");
}else{
Uploading a user name to the servlet
$.get ("http://127.0.0.1:8080/ServletDemo/Demo?user=" + encodeURI (encodeURI (UserName)), null,function (response) {
$ ("#result"). HTML (response);
});
}
});
$ ("#text1"). KeyUp (function () {
var value =$ (this). Val ();
if (value== "") {
$ (this). addclass ("UserClass");
}else {
$ (this). Removeclass ("UserClass");
}
});
});
4.Demo
Import java.io.*;
Import javax.servlet.*;
Import javax.servlet.http.*;
public class Demo extends HttpServlet {
Publicdemo () {
}
Public Voiddoget (HttpServletRequest req,httpservletresponse res) {
try{
Res.setheader ("Cache-control", "No-cache");
Res.setcontenttype ("Text/text;charset=utf-8");
Req.setcharacterencoding ("UTF-8");
Stringu=req.getparameter ("user");;
PrintWriter Pw=res.getwriter ();
FileReader Fr=newfilereader ("E:\\jquery\\jquery\\file\\username.txt");
BufferedReader Br=newbufferedreader (FR);
String Line;
Boolean flag=false;
while ((Line=br.readline ())!=null) {
if (line.equalsignorecase (U)) {
PrintWriter out = Res.getwriter ();
Out.write ("This user name has been registered!");
Out.flush ();
Out.close ();
Flag=true;
}
}
Br.close ();
if (!flag) {
PrintWriter out = Res.getwriter ();
Out.write ("This user name can be used!");
Out.flush ();
Out.close ();
}
}catch (Exception ex) {
Ex.printstacktrace ();
}
}
Publicvoid doPost (httpservletrequest req,httpservletresponse res) {
This.doget (Req,res);
}
}
Simple HTML interacts with the servlet (HTML uses the servlet to read txt)