loginServlet 服务器后端代码
package com.cdy.cookie_login;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/*
* @Project:untitled
* @Author:cdy(缘生)
* @Motto:爱意随风,风平难止
* @Date:2023/2/6 15:58
* */
@WebServlet("/cookieLoginServlet")
public class CookieLoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//防止乱码
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//测试
System.out.println("cookieLoginServlet");
//获取数据
String username = request.getParameter("username");
String password = request.getParameter("password");
//判断用户信息是否正确
if("aaa".equals(username) && "111".equals(password)){
System.out.println("用户信息正确");
String rem = request.getParameter("rem");
//判断是否勾选了复选框
if(rem!=null){
//将值存入cookie
Cookie u = new Cookie("u", username);
Cookie p = new Cookie("p", password);
//设置cookie时效
u.setMaxAge(60*60*24*3);
p.setMaxAge(60*60*24*3);
response.addCookie(u);
response.addCookie(p);
}
request.setAttribute("username",username);
//信息正确,跳转到success页面
request.getRequestDispatcher("success.jsp").forward(request,response);
}else {
//当未输入或不正确时提示用户
request.setAttribute("msg","用户名或密码不正确");
//并且继续跳转到login页面继续登录
request.getRequestDispatcher("login.jsp").forward(request,response);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
login.jsp
这里是登录页面,由于是java后端程序员,前端不是很精通,将就将就吧
<%--
User: cdy
Date: 2023/2/6 15:30
@Version 1.0
@Motto:放下杂念,只为迎接明天更好的自己
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
String username="";
String password="";
//获取cookie
Cookie[] cookies = request.getCookies();
//非空判断
if(cookies!=null){
//遍历cookie集合
for (Cookie cookie:cookies){
String name = cookie.getName();
if("username".equals(name)){
//获取username对应的值
username = cookie.getValue();
}if("password".equals(name)){
//获取password对应的值
password = cookie.getValue();
}
}
}
%>
<div class="container">
<div class="form row" style="height: 300px;">
${msg}
<form action="/jstl/cookieLoginServlet" method="post">
<h3 class="form-title">用户登录</h3>
<div >
<input type="text"name="username" value="${username}"/>
</div>
<div >
<input type="password" name="password"value="${password}"/>
</div>
<div>
<button type="submit" name="submit">登录</button>
<button type="reset" name="submit">重置</button>
</div>
<b>记住密码</b><input type="checkbox" name="rem" value="1">
</form>
</div>
</div>
</body>
</html>
success.jsp
登录成功后,跳转到success.jsp页面,提示登录成功,并且显示哪位用户登录
<%--
User: cdy
Date: 2023/2/6 15:37
@Version 1.0
@Motto:放下杂念,只为迎接明天更好的自己
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
Welcome<h1>${username}</h1>
</body>
</html>
web.xml
图中配置是为了服务器开启后不跳转到index.jsp页面,而是直接跳转到login.jsp页面
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
version="5.0">
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>