Simple HTML interacts with the servlet (HTML uses the servlet to read txt)

Source: Internet
Author: User

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)

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.