hello阿诚

有兴趣留言交流

15-session 对象-设置,获取,清除session

package yucheng;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/se02")
public class HttpSession02 extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("获取Session对象 02");
// 获取 session 对象 如何没有 session 对象 则新增一个
HttpSession session = request.getSession();

// 设置 session 对象
session.setAttribute("name","yucheng");
session.setAttribute("age","19");
// 请求session 对象
session.removeAttribute("age");

// request 域对象
request.setAttribute("bill","iiiii");
// 请求转发
// request.getRequestDispatcher("index.jsp").forward(request,response);
// 重定向 可以拿到 session 对象 但是拿不到 request 对象
response.sendRedirect("index.jsp");

}
}

posted on 2021-07-19 12:58  陆秋明v  阅读(38)  评论(0编辑  收藏  举报

导航