首页 > 代码库 > 简单html与servlet交互(HTML利用servlet读取txt)

简单html与servlet交互(HTML利用servlet读取txt)

转载至http://blog.sina.com.cn/s/blog_680288f001014uy2.html,等自己测试成功再传自己写的代码。

主要流程:servlet读取txt里的数据,并将数据传入到js中,HTML利用js获取txt的数据并根据事件触发进行判断

1.usercheck.html

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=gbk">
   <linkhref="http://www.mamicode.com/lib/ligerUI/skins/Aqua/css/usertext.css" rel="stylesheet"type="text/css" />
   <script src="http://www.mamicode.com/lib/jquery/jquery-1.3.2.min.js"type="text/javascript"></script>
   <script src="http://www.mamicode.com/lib/ligerUI/js/plugins/useropera.js"type="text/javascript"></script>
</head>
<body>
填写用户名

<input type="text" id="text1" class="userClass"/>
<input type="button" value="http://www.mamicode.com/检验"id="verifyButton"/>
<div id="result"></div>
</body>
</html>

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("用户名不能为空");
   }else{
     //将用户名传到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 =http://www.mamicode.com/$(this).val();
   if(valuehttp://www.mamicode.com/==""){
  $(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("此用户名已经被注册!");
                               out.flush();
                               out.close();        
                   flag=true;  
       }
      }
      br.close();
      if(!flag){
                        PrintWriter out = res.getWriter();
                        out.write("此用户名可以使用!");
                        out.flush();
                        out.close();
     }
              
    }catch(Exception ex){
     ex.printStackTrace();
    }
   } 

    publicvoid doPost(HttpServletRequest req,HttpServletResponse res){
    this.doGet(req,res);
    }
    
}

 

简单html与servlet交互(HTML利用servlet读取txt)