——————————DescriptionStart——————————
运行环境
Java≥8、MySQL≥5.7、Tomcat≥8
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
注册 登录 跳转 退出





技术框架
JSP Servlet JDBC MySQL CSS JavaScript
基于javaweb的JSP+Servlet登录 注册 跳转 退出(java+jsp+servlet+jdbc+mysql)
——————————CodeStart——————————
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| System.out.println("password=" + password); System.out.println("validationCode=" + validationCode); if (validationCode == null || !validationCode.equals(request.getSession().getAttribute("validationCode"))) { request.getSession().setAttribute("alert_msg", "错误:验证码不正确!"); request.getRequestDispatcher("login.jsp").forward(request, response); return; }
Admin vo; try { Connection c = Util.getConnection(); Statement s = c.createStatement(); String sql = "select * from `t_admin` where username='" + username + "' and password='" + password + "';"; ResultSet rs = s.executeQuery(sql); if (rs.next()) { vo = new Admin(); vo.setId(rs.getLong("id")); vo.setUsername(rs.getString("username")); vo.setPassword(rs.getString("password")); request.getSession().setAttribute("admin", vo); request.getRequestDispatcher("info.jsp").forward(request, response); return; } c.close(); } catch (Exception e) { e.printStackTrace(); }
request.getSession().setAttribute("alert_msg", "错误:用户名或密码错误!"); request.getRequestDispatcher("login.jsp").forward(request, response); } else if ("register".equalsIgnoreCase(action)) { String username = request.getParameter("username"); String password = request.getParameter("password"); System.out.println("username=" + username); System.out.println("password=" + password);
try { Connection c = Util.getConnection(); Statement s = c.createStatement(); String sql = "select * from `t_admin` where username='" + username + "';"; ResultSet rs = s.executeQuery(sql); if (rs.next()) { request.getSession().setAttribute("alert_msg", "错误:用户名已存在!"); request.getRequestDispatcher("register.jsp").forward(request, response); return;
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| var validationCode = document.getElementById("validationCode").value; if (username == "") { alert("用户名不能为空"); return false; } if (password == "") { alert("密码不能为空"); return false; } if (validationCode == "") { alert("验证码不能为空"); return false; } return true; } function refresh() { var img = document.getElementById("img_validation_code") img.src = "AuthServlet?action=validationCode&r=" + Math.random(); } </script> </html> <%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>注册页</title> <link rel="stylesheet" type="text/css" href="css/index.css"/> <script type="text/javascript"> var alert_msg = '${alert_msg}'; if(alert_msg != null && alert_msg.trim() != ''){ window.alert(alert_msg) ; } </script> </head> <body style="padding: 0; margin: 0;background: url(img/1.jpg) no-repeat;background-size: 100% 100%;"> <h1 style="text-align: center;font-size: 40px;padding-top:40px;font-weight: 700;color:#000000;text-shadow: 2px 3px #FFFFFF;">注册登录跳转退出</h1> <form action="AuthServlet?action=register" method="post" onsubmit="return check()"> <div class="login"> <div class="login-top">
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| <head> <meta charset="utf-8"> <title>登录页</title> <link rel="stylesheet" type="text/css" href="css/index.css"/> <script type="text/javascript"> let alert_msg = '${alert_msg}'; if (alert_msg != null && alert_msg.trim() != '') { window.alert(alert_msg); } </script> </head> <body style="padding: 0; margin: 0;background: url(img/1.jpg) no-repeat;background-size: 100% 100%;"> <h1 style="text-align: center;font-size: 40px;padding-top:40px;font-weight: 700;color:#000000;text-shadow: 2px 3px #FFFFFF;">注册登录跳转退出</h1> <form action="AuthServlet?action=login" method="post" onsubmit="return check()"> <div class="login"> <div class="login-top"> <a href="#" style="color:dodgerblue ;text-decoration: none;padding-left: 63px;">登录</a>       <a style="color: black;text-decoration: none;" href="register.jsp">注册</a> </div> <div class="login-center clearfix"> <div class="login-center-img"><img src="img/name.png"/></div> <div class="login-center-input"> <input type="text" id="username" name="username" value="" placeholder="请输入您的账号" onfocus="this.placeholder=''" onblur="this.placeholder='请输入您的账号'"/> </div> </div> <br> <br> <div class="login-center clearfix"> <div class="login-center-img"><img src="img/password.png"/></div> <div class="login-center-input"> <input type="password" id="password" name="password" value="" placeholder="请输入您的密码" onfocus="this.placeholder=''" onblur="this.placeholder='请输入您的密码'"/> </div> </div> <br> <br> <div class="login-center clearfix"> <div class="login-center-img"><img src="img/password.png"/></div> <div class="login-center-input"> <input type="text" id="validationCode" name="validationCode" style="font-size: 10px;width: 90px;margin-top: 0px;" placeholder="请输入验证码"/> <img id="img_validation_code" src="AuthServlet?action=validationCode" onclick="refresh()" style="height: 30px;"/> </div> </div> <br> <button type="submit" class="login-button">登陆</button> </div> </form> </body> <script type="text/javascript">
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
public class AuthServlet extends HttpServlet { @Override public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); String action = request.getParameter("action"); if ("login".equalsIgnoreCase(action)) { String username = request.getParameter("username"); String password = request.getParameter("password"); String validationCode = request.getParameter("validationCode"); System.out.println("username=" + username); System.out.println("password=" + password); System.out.println("validationCode=" + validationCode); if (validationCode == null || !validationCode.equals(request.getSession().getAttribute("validationCode"))) { request.getSession().setAttribute("alert_msg", "错误:验证码不正确!"); request.getRequestDispatcher("login.jsp").forward(request, response); return; }
Admin vo; try { Connection c = Util.getConnection(); Statement s = c.createStatement(); String sql = "select * from `t_admin` where username='" + username + "' and password='" + password + "';"; ResultSet rs = s.executeQuery(sql); if (rs.next()) { vo = new Admin(); vo.setId(rs.getLong("id")); vo.setUsername(rs.getString("username")); vo.setPassword(rs.getString("password")); request.getSession().setAttribute("admin", vo); request.getRequestDispatcher("info.jsp").forward(request, response); return; } c.close(); } catch (Exception e) {
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| response.setCharacterEncoding("UTF-8"); session.removeAttribute("alert_msg"); String uri = request.getRequestURI(); String action = request.getParameter("action"); if (uri.endsWith("login.jsp") || uri.endsWith("register.jsp") || "register".equalsIgnoreCase(action) || "validationCode".equalsIgnoreCase(action) || "login".equalsIgnoreCase(action) || uri.endsWith(".css") || uri.endsWith(".js") || uri.endsWith(".png") || uri.endsWith(".jpg")) { chain.doFilter(request, response); return; } else if (session.getAttribute("admin") == null) { session.setAttribute("alert_msg", "错误:请先登录!"); response.sendRedirect("login.jsp"); return; } chain.doFilter(request, response); }
@Override public void init(FilterConfig arg0) throws ServletException { } } package com.demo.vo;
public class Admin implements Serializable { private Long id; private String username; private String password;
public Long getId() { return id; }
public void setId(Long id) { this.id = id; } public String getUsername() { return username; }
public void setUsername(String username) {
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| if (rs.next()) { vo = new Admin(); vo.setId(rs.getLong("id")); vo.setUsername(rs.getString("username")); vo.setPassword(rs.getString("password")); request.getSession().setAttribute("admin", vo); request.getRequestDispatcher("info.jsp").forward(request, response); return; } c.close(); } catch (Exception e) { e.printStackTrace(); }
request.getSession().setAttribute("alert_msg", "错误:用户名或密码错误!"); request.getRequestDispatcher("login.jsp").forward(request, response); } else if ("register".equalsIgnoreCase(action)) { String username = request.getParameter("username"); String password = request.getParameter("password"); System.out.println("username=" + username); System.out.println("password=" + password);
try { Connection c = Util.getConnection(); Statement s = c.createStatement(); String sql = "select * from `t_admin` where username='" + username + "';"; ResultSet rs = s.executeQuery(sql); if (rs.next()) { request.getSession().setAttribute("alert_msg", "错误:用户名已存在!"); request.getRequestDispatcher("register.jsp").forward(request, response); return; } c.close(); } catch (Exception e) { e.printStackTrace(); }
Admin vo = new Admin(); vo.setUsername(username); vo.setPassword(password);
String sql = "insert into `t_admin` (`username`,`password`) values(?,?);";
|
——————————PayStart——————————
项目链接:
https://javayms.github.io?id=072021211101102aa
https://javayms.pages.dev?id=072021211101102aa