在这里,我们首先写一个用于登陆的login.jsp,相信很多刚开始进行jsp开发的人,都会很快遇到这个问题。

    

 

  这是一个很常见的界面。具体代码如下

 

 
  
  1. <%@page contentType="text/html;charset=gbk" %>  
  2. <%@page language="java" import="java.util.*"%>  
  3. <%@page language="java" import="gfs04.*"%>  
  4. <%  
  5. String sID = (String)session.getAttribute("ID");  
  6. if ( sID == null )  
  7. {  
  8.     sID = "";  
  9. }  
  10. String stype = (String)session.getAttribute("type");  
  11. if ( stype == null )  
  12. {  
  13.     stype = "0";  
  14. }  
  15. String sname = (String)session.getAttribute("name");  
  16. if ( sname == null )  
  17. {  
  18.     sname = "0";  
  19. }  
  20. %>  
  21. <script language="JavaScript" type="text/JavaScript">  
  22. //表单reset的时候。需要做的特需处理  
  23. function resetForm(){  
  24.     form_login.ID.focus();  
  25.     form_login.password.focus();  
  26. }  
  27.  
  28. //登录前的检查  
  29. function checkInput(){  
  30.     //check the ID  
  31.     if(form_login.ID.value.length<1){  
  32.         form_login.ID.foucs();  
  33.         form_login.ID.select();  
  34.         alert("登陆账号不能为空!");  
  35.         return false;  
  36.     }  
  37.     if(getLength(form_login.sID.value)>12){  
  38.         form_login.ID.focus();  
  39.         form_login.ID.select();  
  40.         alert("账号最大不能超过12位数字!");  
  41.         return false;  
  42.     }  
  43.     //检查密码  
  44.     if(form_login.password.value.length<1){  
  45.         form_login.password.focus();  
  46.         form_login.password.select();  
  47.         alert("请输入密码!!");  
  48.         return false;  
  49.     }  
  50.     return true;  
  51. }  
  52. </script>  
  53. <html>  
  54. <head>  
  55. <title>国防生信息管理系统</title></head>  
  56. <body>  
  57.   <b>请输入用户名密码:</b>  
  58.   <form action="gfs04/Login" method="post" name="form_login">  
  59.     <table>  
  60.       <tr>  
  61.         <td> 登陆账号:</td><td><input type="text" name="ID"></input></td>  
  62.       </tr>  
  63.       <tr>  
  64.         <td>密码:</td><td><input type="password" name="password"></input></td>  
  65.       </tr>  
  66.       <tr>  
  67.         <td height=10></td>  
  68.       </tr>  
  69.       <tr>  
  70.         <td><input type="submit" value="登 录" style="FONT-WEIGHT: bold; 
  71. FONT-SIZE: 12px; WIDTH: 82px; COLOR: #000000; 
  72. HEIGHT: 27px; BACKGROUND-COLOR: #E0E0DE"></td>  
  73.       </tr>  
  74.     </table>   
  75.   </form>  
  76. </body>  
  77.  
  78. </html>  
 
  
  1. package gfs04;  
  2.  
  3. import java.io.*;  
  4. import java.sql.*;  
  5. import javax.servlet.ServletException;  
  6. import javax.servlet.http.*;  
  7.  
  8.  
  9. public class Login extends Common {  
  10.     /**  
  11.      *   
  12.      */  
  13.     private static final long serialVersionUID = 1L;  
  14.     public String check(String ID , String password , String type){  
  15.         Connection conn = this.getDBConnection();  
  16.         if(conn==null){  
  17.             return null;  
  18.         }  
  19.         Statement stmt=null;  
  20.         ResultSet rs=null;  
  21.         try{  
  22.             stmt =conn.createStatement();  
  23.             //execute the sql query  
  24.             String sQuery = "select * from user where ID= '" 
  25.                     +ID+"' and password='"+password+  
  26.                     "' and type='"+type+"' ";  
  27.             rs = stmt.executeQuery(sQuery);  
  28.             String sname=null;  
  29.             if(rs.next()){  
  30.                 sname = rs.getString("name");  
  31.             }  
  32.             return sname;  
  33.         }catch (Exception e){  
  34.             e.printStackTrace();  
  35.             return null;  
  36.         }  
  37.         finally{  
  38.             try{  
  39.                 rs.close();  
  40.                 stmt.close();  
  41.                 conn.close();  
  42.             }catch (Exception ex){  
  43.                 ex.printStackTrace();  
  44.                 return null;  
  45.             }  
  46.         }  
  47.     }  
  48.     public void doGet(HttpServletRequest request,   
  49.             HttpServletResponse response)  
  50.         throws ServletException , IOException{  
  51.         //set the code of the form  
  52.         request.setCharacterEncoding("GBK");  
  53.         HttpSession mySession = request.getSession(true);  
  54.         //clear the error message  
  55.         mySession.setAttribute("errMsg", "");  
  56.         //get user info  
  57.         String sID = request.getParameter("sID");  
  58.         String spassword = request.getParameter("spassword");  
  59.         String stype = request.getParameter("stype");  
  60.         //deal with the get  
  61.         if(sID!=null&&sID.length()>0){  
  62.             //check user info  
  63.             String sname=check(sID , spassword , stype);  
  64.             if(sname==null){  
  65.                 //login failed  
  66.                 mySession.setAttribute("errMsg", "登录失败,请重新登陆!");  
  67.                 mySession.setAttribute("ID",sID );  
  68.                 mySession.setAttribute("type", stype);  
  69.                 response.sendRedirect("../login.jsp");  
  70.             }  
  71.             else {  
  72.                 mySession.setAttribute("name", sname);  
  73.                 mySession.setAttribute("ID",sID );  
  74.                 mySession.setAttribute("type", stype);  
  75.                 response.sendRedirect("../index.jsp");  
  76.                 return ;  
  77.             }  
  78.         }  
  79.         else {  
  80.             response.sendRedirect("../login.jsp");  
  81.             return ;  
  82.         }  
  83.     }   
  84.     //deal with post method  
  85.     public void doPost(HttpServletRequest request ,  
  86.             HttpServletResponse response)  
  87.         throws ServletException , IOException{  
  88.         doGet(request , response);  
  89.     }  
  90.       

    这样,用户在login.jsp输入账号密码进行登录,Login.java将传来的参数查询数据库,若匹配,则跳转到首页index.jsp;否则,留在登陆页。可是,当我在部署这个应用时,却发现了很多问题。

    主要有如下:java并没有自动生成class文件,没有在web.xml文件里自动生成servlet配置。解决问题留在下一篇文章那。