5、在LoginServlet
中实现登录逻辑,处理用户输入的用户名和密码,验证用户身份,如果用户名和密码正确则生成一个记住登录状态的Cookie保存到浏览器中,然后重定向到需要登录才能访问的页面。
@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String password = request.getParameter("password"); User user = userDao.findByUsernameAndPassword(username, password); if (user != null) { HttpSession session = request.getSession(); session.setAttribute("user", user); // 创建一个记住登录状态的Cookie,有效期为一天 Cookie cookie = new Cookie("user", username); cookie.setPath("/"); cookie.setMaxAge(maxAge); response.addCookie(cookie); response.sendRedirect("index.jsp"); } else { request.setAttribute("error", "用户名或密码错误"); request.getRequestDispatcher("login.jsp").forward(request, response); }}
6、创建RegisterServlet
类,处理注册请求。
@WebServlet("/register")public class RegisterServlet extends HttpServlet { private static final long serialVersionUID = 1L; private UserDao userDao = new UserDaoImpl(); @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String password = request.getParameter("password"); User user = new User(); user.setUsername(username); user.setPassword(password); if (userDao.insert(user)) { request.setAttribute("tip", "注册成功,请登录!"); request.getRequestDispatcher("login.jsp").forward(request, response); } else { request.setAttribute("error", "用户名已被占用,请重新输入!"); request.getRequestDispatcher("register.jsp").forward(request, response); } }}
7、在web.xml
文件中配置Servlet映射和路径。
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <!-- Servlet映射和路径 --> <servlet> <servlet-name>login</servlet-name> <servlet-class>com.example.LoginServlet</servlet-class> </servlet> <servlet> <servlet-name>register</servlet-name> <servlet-class>com.example.RegisterServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>login</servlet-name> <url-pattern>/login</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>register</servlet-name> <url-pattern>/register</url-pattern> </servlet-mapping></web-app>
8、创建login.jsp
和register.jsp
页面,用于展示登录和注册表单,并处理输入的数据。
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>登录</title> <link rel="stylesheet" href="/css/style.css"></head><body> <div class="container"> <div class="title">登录</div> <form action="/login" method="post"> <div class="input-container"> <label>用户名:</label> <input type="text" name="username" required> </div> <div class="input-container"> <label>密码:</label> <input type="password" name="password" required> </div> <c:if test="${not empty error}"> <div class="error"><%=error %>></div> </c:if> <button type="submit">登录</button> </form> <div class="footer"> <a href="/register">立即注册</a> </div> </div></body></html>
register.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>注册</title> <link rel="stylesheet" href="/css/style.css"></head><body> <div class="container"> <div class="title">注册</div> <form action="/register" method="post"> <div class="input-container"> <label>用户名:</label> <input type="text" name="username" required> </div> <div class="input-container"> <label>密码:</label> <input type="password" name="password" required> </div> <c:if test="${not empty error}"> <div class="error"><%=error %>></div> </c:if> <c:if test="${not empty tip}"> <div class="tip"><%=tip %>></div> </c:if> <button type="submit">注册</button> </form> <div class="footer"> <a href="/login">返回登录</a> </div> </div></body></html>
9、创建index.jsp
页面,用于展示登录后的欢迎信息。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>首页</title> <link rel="stylesheet" href="/css/style.css"></head><body> <div class="container"> <div class="title">欢迎您,<%=user.getUsername() %>></div> <p>您已经成功登录,可以查看网站的数据信息内容。</p> </div></body></html>
10、编写style.css
样式表,用于控制页面的布局和风格。
body { margin: 0; padding: 0; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;}a { color: #0073aa; text-decoration: none;}.container { display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100vh; background-color: #f5f5f5;}.title { margin-bottom: 40px; font-size: 40px; font-weight: bold; text-align: center;}.input-container { display: flex; align-items: center; margin-bottom: 20px;}.input-container label { margin-right: 20px;}.input-container input[type=text], input[type=password] { flex: 1; padding: 10px; border: 1px solid #dbdbdb; border-radius: 4px;}.error { margin-bottom: 20px; color: #f44336;}.tip { margin-bottom: 20px; color: #4caf50;}.footer { margin-top: 20px; text-align: center;}.footer a { font-size: 14px;}
通过使用Java Servlet和JDBC,就可以很方便的实现登录注册功能了。开发这样的Web应用不仅需要对Java EE技术有深入了解,同时还需要熟练掌握HTML、CSS、JavaScript等前端技术。
除了基本的功能实现之外,还需要注意安全性和用户体验方面的问题,例如密码加密、防止恶意攻击、提示信息友好等。只有在增强安全性和用户体验的前提下,这样的Web应用才能得到更好的推广和使用。
感谢观看,如有问题欢迎留言。