java登录页面的实现步骤
开发工具与关键技术:java
作者:#33
撰写时间:撰写时间:2020年08月08日
初学java登录
学习java一段时间了,现在对之前mvc项目的用java语言作为练习(登录的部分)。
首先准备好对应的资料,之前的页面、连接数据库的配置文件和日志的配置文件。
实体类,登录用到的用户表字段:设置get和set方法:
private Integer UserID;private Integer EmployeeID;// 员工ID private String UserName;// 用户名private String Password;// 密码 private String UserCode;// 用户编码private String UserState;// 用户状态 private String UnifiedVerificationCode;// 统一验证码 public Integer getUserID() { return UserID; } public void setUserID(Integer userID) {UserID = userID;}...... |
封装实现类继承接口后实现,用于实现登录分方法,查询登录的用户名、密码和验证码:
Sql查询语句:
private String findByName = "select *from s_user where UserName=?"; |
@Override public User findByName(String name) { User user = null; try {conn =DBUtils.getConnection();ps = conn.prepareStatement(findByName);//调用sql语句 ps.setString(1, name);rs = ps.executeQuery(); while (rs.next()) { user = new User();//调用实体类的set、get方法获取用户名和用户信息 user.setUserID(rs.getInt("UserID")); user.setUserName(rs.getString("UserName")); user.setPassword(rs.getString("Password")); ...... }} catch (SQLException e) {e.printStackTrace();} finally {DBUtils.close(rs, conn, ps);}//关流return user;} |
在创建一个用于Servlet调用的Service实现类匹配获取加密的密码数据库对比(使用封装好的加密的工具类)MD5Util:
@Overridepublic User login(String name, String password) { User user=null; User dbUser=userDao.findByName(name); if (dbUser!=null) {password=MD5Util.getMD5(password); //设置为MD5密码与数据库对比等于则返回user=dbUser if (password.equals(dbUser.getPassword().trim())) {user=dbUser;} } return user;} |
Servlet调用用户登录的方法:
doGet方法调用doPost使用doPost请求数据
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response);} |
doPost方法:判断要登录的页面或请求数据进行跳转的方法
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//解决乱码问题 request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); String strfun=request.getParameter("fun"); if ("login".equals(strfun)) {login(request, response);//登录页面 }else if ("identity".equals(strfun)) {identity(request, response);//验证码 }else if ("userLogin".equals(strfun)) {userLogin(request, response);//登录请求数据} else {login(request, response);//重定向}} |
获取验证码:
private void identity(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//调用生成验证码工具类方法 GifCaptcha captcha=new GifCaptcha(290, 35, 5); ServletOutputStream out= response.getOutputStream(); String identityKey= captcha.out(out); request.getSession().setAttribute("identityKey", identityKey); out.flush(); out.close();} |
跳转登录页面:
private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.getRequestDispatcher("/jsp/login.jsp").forward(request, response);} |
//登录验证private void userLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { JsonReturn jsonReturn=new JsonReturn(); IUserService userService=new UserServiceImpl();//调用登录方法的实现类 String userName=request.getParameter("username");//用户名 String password=request.getParameter("password");//密码 String identityKey=request.getParameter("identityKey");//验证码 HttpSession session=request.getSession(); try { String sessionIdentityKey=session.getAttribute("identityKey").toString(); if(Tools.isNotNull(identityKey)&&identityKey.equalsIgnoreCase(sessionIdentityKey)) {User user=userService.login(userName, password); if (user!=null) { session.setAttribute("SESSION_USER", user); jsonReturn.setState(true);jsonReturn.setMsg("登录成功"); }else {jsonReturn.setState(false); jsonReturn.setMsg("输入用户或密码错误");} }else {jsonReturn.setState(false); jsonReturn.setMsg("验证码错误"); }} catch (Exception e) { jsonReturn.setState(false); jsonReturn.setMsg("参数错误");} PrintWriter out=response.getWriter(); JSONObject jObject=JSONObject.fromObject(jsonReturn);//转成json字符串 out.write(jObject.toString()); out.close(); } |
本文地址:https://blog.csdn.net/weixin_44484621/article/details/107896366