• 完成一个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();
              }
          }
      }
      
  • 相关阅读:
    c语言数组指针
    (4)activiti工作流引擎之uel表达式
    (3)activiti流程的挂起和激活
    (2)java程序走一遍工作流activiti
    (1)activiti认识以及数据库和插件配置
    linux 下路由配置
    lvs-dr+keepalived
    LVS-DR 配置测试
    简单认识TCP/IP协议
    mysql 主从同步-读写分离
  • 原文地址:https://www.cnblogs.com/musecho/p/13021568.html
Copyright © 2020-2023  润新知