完成一個IDEA web項目(二)登錄功能實現


(二)登錄功能實現


1. 編寫前端頁面

若jsp頁面中文亂碼:在jsp中指定頁面顯示的編碼為GBK

添加page命令

<%@ page language="java" contentType="text/html; charset=GBK" %>

參考:https://www.cnblogs.com/beijiguangyong/archive/2012/03/31/2437124.html

*其他所有出現編碼的地方也要改掉


2. 編寫login.jsp,並設置為首頁

  • login.jsp

    <%@ page language="java" contentType="text/html; charset=GBK" %>
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="GBK">
        <title>用戶登錄</title>
    </head>
    <body>
    <form action="${pageContext.request.contextPath}/studentServlet" method="post">
    
        <div class="errorInfo">${error}</div>
        <p>學號:<input type="text" size="20" name="stuNo"></p>
        <p>密碼:<input type="password" size="20" name="stuPwd"></p>
        <p><input type="submit" value="登錄"></p>
    
    </form>
    </body>
    </html>
    
    
  • 設置為首頁:

    web.xml添加

    <!--    設置歡迎頁面    -->
        <welcome-file-list>
            <welcome-file>login.jsp</welcome-file>
        </welcome-file-list>
    

3. 編寫dao層用戶登陸的接口

  • StudentDao

    public interface StudentDao {
    
        //得到要登錄的研究生
        public Student getLoginStudent(Connection conn,String stuNo) throws SQLException;
    }
    

4. 編寫dao接口的實現層

  • StudentDaoImpl

    package dao.student;
    
    import dao.BaseDao;
    import domain.Student;
    import org.junit.Test;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    public class StudentDaoImpl implements StudentDao {
    
        @Override
        public Student getLoginStudent(Connection conn, String stuNo) throws SQLException {
            PreparedStatement ps = null;
            ResultSet rs = null;
    
            Student stu = null;
    
            if (conn != null) {
                String sql = "select * from student where stu_no=?";
                Object[] params = {stuNo};
    
    
                    rs = BaseDao.executeQuery(conn, sql, params, ps, rs);
    
    
                    if (rs.next()) {
                        stu = new Student();
    
                        stu.setStuNo(rs.getString(1));
                        stu.setPassword(rs.getString(2));
                        stu.setStuName(rs.getString(3));
                        stu.setSpecialty(rs.getString(4));
                        stu.setSex(rs.getBoolean(5));
                        stu.setBirth(rs.getDate(6));
                        stu.setPhone(rs.getString(7));
                        stu.setEmail(rs.getString(8));
                        stu.setGradSchoolCode(rs.getString(9));
                        stu.setGradSchoolName(rs.getString(10));
                        stu.setGradSpecCode(rs.getString(11));
                        stu.setGradSpecName(rs.getString(12));
                    }
                    BaseDao.closeResource(null, ps, rs);
    
            }
    
            return stu;
        }
    
    }
    

5. 業務層接口

  • StudentService

    public interface StudentService {
    
        /**
         * 驗證是否登錄成功
         *
         * @return 登錄成功則返回用戶對象,無則null
         */
        public Student isLogin(String stuNo, String pwd) throws SQLException;
    }
    

6. 業務層實現類

  • StudentServiceImpl

    public class StudentServiceImpl implements StudentService {
    
        private StudentDao studentDao;
    
        public StudentServiceImpl() {
            studentDao = new StudentDaoImpl();
        }
    
        /**
         * 驗證是否登錄成功
         *
         * @return 有登錄成功則返回用戶對象,無則null
         */
        @Override
        public Student isLogin(String stuNo, String pwd) {
            Connection conn = null;
            Student stu = null;
    
            try {
                conn = BaseDao.getConnection();
                stu = studentDao.getLoginStudent(conn, stuNo);
                if (stu != null) {
                    if (!pwd.equals(stu.getPassword()))
                        stu = null;
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                BaseDao.closeResource(conn, null, null);
            }
    
            return stu;
        }
    
    }
    

7. 編寫Servlet

  • StudentServlet

    記得添加@WebServlet注解

    @WebServlet("/studentServlet")
    public class StudentServlet extends HttpServlet {
    
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doPost(req, resp);
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            //獲取客戶端提交的數據
            String stuNo = req.getParameter("stuNo");
            String stuPwd = req.getParameter("stuPwd");
    
            try {
                StudentService studentService = new StudentServiceImpl();
                Student stu = studentService.isLogin(stuNo, stuPwd);
                if (stu != null) {    //有該用戶,存進session,跳轉到查看導師頁面
                    HttpSession session = req.getSession();
                    session.setAttribute(Constants.STUDENT_SESSION, stu);
                    resp.sendRedirect("stu_check_tutor.jsp");
                } else {    //無該用戶,跳轉至登錄頁面,並提示錯誤信息
                    req.setAttribute("error", "用戶名或密碼不正確");
                    req.getRequestDispatcher("login.jsp").forward(req, resp);
                    System.out.println("結束Servlet....");
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM