动态网站项目(Dynamic Web Project)登录功能的实现(mvc(五层架构)+jdbc+servlet+tomcat7.0+jdk1.8)(js验证+cookie)

1.index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link type="text/css" href="css/index.css" rel="stylesheet"></link>
</head>
<body>
    <!-- 使用Cookie保存密码 -->
    <%
        //获取所有的Cookie
        Cookie[] cookies = request.getCookies();
        //如果cookie中存有数据,说明曾有登陆成功的用户名=和密码保存在cookie中
        String username = null;
        String password = null;
        if(cookies != null){
            for(int i=0;i<cookies.length;i++){
                //如果key和username相等,则有登陆成功地用存储在cookie中,我们获取用户名和密码
                if("username".equals(cookies[i].getName())){
                    username=cookies[i].getValue();
                }
                if("password".equals(cookies[i].getName())){
                    password=cookies[i].getValue();
                }
            }
        }
        //System.out.println(username);
        //System.out.println(password);
        //如果有这样的用户名和密码,那么跳转到servlet页面进行操作判断
        if(username!=null&&password!=null){
            request.getRequestDispatcher("LoginServlet?username="+username+"&password="+password).forward(request, response);
        }
        
    
    
    %>
    
    <div id="login_frame">
        <p id="image_logo"><img src="images/t.png" ></p>
        <!-- 提示信息 -->
        <%String msg =(String)request.getAttribute("msg");
           if(msg!=null&&!"".equals(msg)){%>
          <font color="red"><%= msg%></font>
        <%}%>
        
        
        
        <form method="post" id="form1">
            <p><lable class="label_input">用户名:</lable><input type="text" id="uname" name="username" class="text_field"><span id="span1" class="span_field"></span></p>
            <p><lable class="label_input">密码:</lable><input type="text" id="pwd" name="password" class="text_field"><span id="span2" class="span_field"></span></p>
            <select name="time" class="select_field">
                <option value="0">不保存密码</option>
                <option value="1">保存密码1分钟</option>
                <option value="3">保存密码3分钟</option>
            </select>
            <input type="button" value="登录" id="btn_login" onclick="fun()">
    </form>
    </div>
    
    
    <script type="text/javascript">
        function fun(){
            
            //flag用于标记用户名和密码是否为空
            var flag=1;
            var uname = document.getElementById("uname").value;
            var pwd = document.getElementById("pwd").value;
            //alert(uname);
            //用户名为空时
            if(uname==null||uname==""){
                document.getElementById("span1").innerHTML="用户名不能为空";
                document.getElementById("span1").style.color="red";
                flag=2;
            }
             //密码为空时
            if(pwd==null||pwd==""){
                document.getElementById("span2").innerHTML="密码不能为空";
                document.getElementById("span2").style.color="red";
                flag=2;
            }
            //用户名和密码都不为空时,进行逻辑判断
            if(flag==1){
                var f1=document.getElementById("form1");
                f1.action="LoginServlet";
                f1.submit();
            }
        }
    </script>
</body>
</html>

2.Vo层:User995.java

 1 package com.icss.vo;
 2 
 3 public class User1995 {
 4     private String username;
 5     private String password;
 6     private String status;
 7     public String getUsername() {
 8         return username;
 9     }
10     public void setUsername(String username) {
11         this.username = username;
12     }
13     public String getPassword() {
14         return password;
15     }
16     public void setPassword(String password) {
17         this.password = password;
18     }
19     public String getStatus() {
20         return status;
21     }
22     public void setStatus(String status) {
23         this.status = status;
24     }
25     
26 
27 }

3.controller层:LoginServlet(使用servlet的注解方式)

 1 package com.icss.controller;
 2 
 3 import java.io.IOException;
 4 import java.sql.SQLException;
 5 
 6 import javax.servlet.ServletException;
 7 import javax.servlet.annotation.WebServlet;
 8 import javax.servlet.http.Cookie;
 9 import javax.servlet.http.HttpServlet;
10 import javax.servlet.http.HttpServletRequest;
11 import javax.servlet.http.HttpServletResponse;
12 
13 import com.icss.service.UserService;
14 
15 /**
16  * Servlet implementation class LoginServlet
17  */
18 @WebServlet("/LoginServlet")
19 public class LoginServlet extends HttpServlet {
20     private static final long serialVersionUID = 1L;
21        
22     /**
23      * @see HttpServlet#HttpServlet()
24      */
25     public LoginServlet() {
26         super();
27         // TODO Auto-generated constructor stub
28     }
29 
30     /**
31      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
32      */
33     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
34         doPost(request, response);
35     }
36 
37     /**
38      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
39      */
40     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
41         //接受请求
42         String username = request.getParameter("username");
43         String password = request.getParameter("password");
44         //处理请求
45         UserService us = new UserService();
46         try {
47             //判断用户的状态,进行跳转
48             int flag = us.userService(username, password);
49             if(flag==0){
50                 //正在审核
51                 request.setAttribute("msg", "该用户正在审核中");
52                 request.getRequestDispatcher("index.jsp").forward(request, response);
53             }else if(flag==1){
54                 //登陆成功
55                 //************************cookie简化登录*****************************
56                 String time = request.getParameter("time");
57                 int days = 0;
58                 if(time!=null){
59                     //将time强制转换int类型
60                     days = Integer.parseInt(time);
61                     //创建Cookie对象
62                     Cookie usernameCookie = new Cookie("username", username);
63                     Cookie passwordCookie = new Cookie("password", password);
64                     //设置Cookie的有效时间
65                     usernameCookie.setMaxAge(days*60);
66                     passwordCookie.setMaxAge(days*60);
67                     //使Cookie生效
68                     response.addCookie(usernameCookie);
69                     response.addCookie(passwordCookie);
70                 }
71                 //************************cookie简化登录*****************************
72                 request.setAttribute("msg", username);
73                 request.getRequestDispatcher("loginsuccess.jsp").forward(request, response);
74             }else if(flag==2){
75                 //审核未通过
76                 request.setAttribute("msg", "该用户审核未通过");
77                 request.getRequestDispatcher("index.jsp").forward(request, response);
78             }else if(flag==3){
79                 //用户名或密码错误
80                 request.setAttribute("msg", "用户名或密码错误,请重新输入");
81                 request.getRequestDispatcher("index.jsp").forward(request, response);
82             }
83             
84         } catch (ClassNotFoundException e) {
85             // TODO Auto-generated catch block
86             e.printStackTrace();
87         } catch (SQLException e) {
88             // TODO Auto-generated catch block
89             e.printStackTrace();
90         }
91     }
92 
93 }

4.service层:UserService.java

 1 package com.icss.service;
 2 
 3 import java.sql.SQLException;
 4 
 5 import com.icss.dao.UserDao;
 6 import com.icss.vo.User1995;
 7 
 8 public class UserService {
 9     public int userService(String username,String password) throws ClassNotFoundException, SQLException{
10         //status:0-正在审核,1-审核通过,登陆成功,2-审核未通过,3-用户名或密码不正确
11         //flag用来标记
12         int flag=3;
13         UserDao ud= new UserDao();
14         User1995 user = ud.userDao(username, password);
15         if(user!=null){
16             String status = user.getStatus();
17             if("0".equals(status)){
18                 //正在审核
19                 flag=0;
20             }else if("1".equals(status)){
21                 flag=1;
22             }else if("2".equals(status)){
23                 flag=2;
24             }
25         }else{
26             flag=3;
27         }
28         return flag;
29     }
30 
31 }

5.util层:DBUtil.java

 1 package com.icss.util;
 2 
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.sql.SQLException;
 6 
 7 public class DBUtil {
 8     public static Connection connection;
 9     public static Connection getConnection() throws ClassNotFoundException, SQLException{
10         Class.forName("oracle.jdbc.driver.OracleDriver");
11         Connection connection = DriverManager.getConnection
12                 ("jdbc:oracle:thin:@localhost:1521:XE","scott","tiger");
13         return connection;
14     }
15 
16 }

6.dao层:UserDao.java

 1 package com.icss.dao;
 2 
 3 import java.sql.PreparedStatement;
 4 import java.sql.ResultSet;
 5 import java.sql.SQLException;
 6 
 7 import com.icss.util.DBUtil;
 8 import com.icss.vo.User1995;
 9 
10 public class UserDao {
11     public User1995 loginDao(String username,String password) throws SQLException, ClassNotFoundException{
12         PreparedStatement pStatement = DBUtil.getConnection().prepareStatement("select * from user1995 where username=?and password=?");
13         pStatement.setString(1, username);
14         pStatement.setString(2, password);
15         //结果集
16         ResultSet rs= pStatement.executeQuery();
17         User1995 user = new User1995();
18         while(rs.next()){
19             user.setUsername(rs.getString(1));
20             user.setPassword(rs.getString(2));
21             user.setStatus(rs.getString(3));
22         }
23         rs.close();
24         pStatement.close();
25         DBUtil.getConnection().close();
26         return user;
27     }
28 
29 }

 

posted @ 2020-03-20 00:15  無笙  阅读(310)  评论(0编辑  收藏  举报