<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--访问JSP不允许生成Session对象--%>
<%@ page session="false" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>欢迎页</title>
</head>
<body>
<h1>用户登录</h1>
<hr>
<form action="<%=request.getContextPath()%>/user/login" method="post">
username:<input type="text" name="username"><br>
password:<input type="password" name="password"><br>
<input type="submit" value="login">
</form>
<h1>
<a href="<%=request.getContextPath()%>/cookie/generate">
服务器生成Cookie,然后把Cookie传给浏览器,浏览器收到,再放到客户端上
</a>
</h1>
<%-- 前端发送请求路径的时候,如果请求路径是绝对路径,要以/开始,加项目名--%>
<%-- <h1><a href="<%=(request.getContextPath())%>/dept/list">查看部门列表</a></h1>--%>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--访问JSP不允许生成Session对象--%>
<%@ page session="false" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>欢迎页</title>
</head>
<body>
<h1>用户登录</h1>
<hr>
<form action="<%=request.getContextPath()%>/user/login" method="post">
username:<input type="text" name="username"><br>
password:<input type="password" name="password"><br>
<input type="submit" value="login">
</form>
<h1>
<a href="<%=request.getContextPath()%>/cookie/generate">
服务器生成Cookie,然后把Cookie传给浏览器,浏览器收到,再放到客户端上
</a>
</h1>
<%-- 前端发送请求路径的时候,如果请求路径是绝对路径,要以/开始,加项目名--%>
<%-- <h1><a href="<%=(request.getContextPath())%>/dept/list">查看部门列表</a></h1>--%>
</body>
</html>
package com.bjpowernode.oa.web.action;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/cookie/generate")
public class GenerateCookie extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
//创建Cookie
Cookie cookie = new Cookie("productId","12345678");
//设置cookie生命为一小时
cookie.setMaxAge(0);
//没有设置有效时间默认保存在浏览器运行内存中,断电自动消失
response.addCookie(cookie);
}
}
package com.bjpowernode.oa.web.action;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/cookie/generate")
public class GenerateCookie extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
//创建Cookie
Cookie cookie = new Cookie("productId","12345678");
//设置cookie生命为一小时
cookie.setMaxAge(0);
//没有设置有效时间默认保存在浏览器运行内存中,断电自动消失
response.addCookie(cookie);
}
}