Ajax使用范例

参考:http://wandering192.iteye.com/blog/736136

index.jsp:
<%@ page pageEncoding="UTF-8" contentType="text/html;charset=utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 request.setCharacterEncoding("utf-8");
%>




 
   
    
    My JSP 'index.jsp' starting page


   



 
  
 

 
 
 
 
 
 
 
 
 
 
 
   
   
           
   
   
             
   
 
图书管理系统登陆
用户名

id="usermsg">
密码
用户类型:
                   
       
 
     


   
${msg}


   查看 
 







validate.jsp:


<%@ page pageEncoding="UTF-8" contentType="text/html;charset=utf-8"%>
<%@ page import="dao.*"%>
<% 
   response.setContentType("text/xml");
   response.setHeader("Cache-Control","no-store");
   response.setHeader("Pragma","no-cache");
   response.setDateHeader("Expires",0);
   String name=(String)request.getParameter("id");
   name = java.net.URLDecoder.decode(name,"UTF-8");
   UserDao a=new UserDao();
   if(a.exists(name)){
        response.getWriter().write("invalid");
    }
   else{
       response.getWriter().write("valid");
    }
%>




dao.UserDao:


 public boolean exists(String name){
       Connection conn=null;
PreparedStatement stat=null;
ResultSet rst=null;
String sql="";
sql="select * from user where name='"+name+"'";
try {
conn=DB.getConnection();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try{
stat=conn.prepareStatement(sql);
rst=stat.executeQuery();
if(rst.next()){
return true;
}
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}

你可能感兴趣的:(Java,web)