完成一个IDEA web项目(二)登录功能实现

2021-02-13 04:17

阅读:500

YPE html>

标签:ror   ted   size   log   http   trace   OLE   domain   成功   

(二)登录功能实现


1. 编写前端页面

若jsp页面中文乱码:在jsp中指定页面显示的编码为GBK

添加page命令


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

*其他所有出现编码的地方也要改掉


2. 编写login.jsp,并设置为首页

  • login.jsp

    
    
    用户登录
    
    ${error}

    学号:

    密码:

  • 设置为首页:

    web.xml添加

    
        login.jsp

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();
            }
        }
    }
    

完成一个IDEA web项目(二)登录功能实现

标签:ror   ted   size   log   http   trace   OLE   domain   成功   

原文地址:https://www.cnblogs.com/musecho/p/13021568.html

上一篇:制作网站的基本流程

下一篇:httpclient


评论


亲,登录后才可以留言!