JSP改造Cookie案例以及Session的快速入门

JSP改造Cookie案例

复制代码
<%@ page import="java.net.URLDecoder" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.net.URLEncoder" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

<%
    //1、获取所有的Cookie
    Cookie[] cookies = request.getCookies();
    boolean flag = false;
    //2、遍历Cookie数组
    if (cookies !=null && cookies.length>0){
        for (Cookie cookie : cookies) {
            //3、获取cookie名称
            String name = cookie.getName();
            //4、判断名称是否是lastTime
            if ("lastTime".equals(name)){
                //有该cookie,不是第一次访问
                flag = true;
                //响应数据
                //获取cookie的value时间
                String value = cookie.getValue();
                //URL解码
                System.out.println("解码前:"+value);
                value = URLDecoder.decode(value, "utf-8");
                System.out.println("解码后:"+value);
%>
                <h1>欢迎回来,您上次访问时间为:<%=value%></h1>
<%
                //设置cookie的value
                //获取当前时间的字符串,重新设置Cookie的值,重新发送cookie
                Date date = new Date();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
                String format = sdf.format(date);

                System.out.println("编码前:"+format);
                //URL编码
                format = URLEncoder.encode(format, "utf-8");
                System.out.println("编码后:"+format);

                cookie.setValue(format);
                //设置cookie的存活时间
                cookie.setMaxAge(60*60*24*30);
                response.addCookie(cookie);
                break;
            }
        }
    }

    if (cookies==null && cookies.length==0 && flag == false){
        //没有,第一次访问
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        String format = sdf.format(date);

        //URL编码
        format = URLEncoder.encode(format, "utf-8");

        Cookie cookie = new Cookie("lastTime",format);
        //设置cookie的存活时间
        cookie.setMaxAge(60*60*24*30);
        response.addCookie(cookie);
%>
        <h1>您好,欢迎您首次访问</h1>
<%
    }
%>

</body>
</html>
复制代码

 

 

 

 

 

 

 

 

 

Session的快速入门

概念:服务器端会话技术,在一次会话的多次请求间共享数据,将数据保存在服务器端的对象中。HttpSession

快速入门:

  1、获取HttpSession对象:

HttpSession session = request.getSession();

  2、使用HttpSession对象:

    Object getAttribute(String name)

    void setAttribute(String name,Object value)

    void removeAttribute(String name)

session存储数据:

复制代码
@WebServlet("/SessionDemo1")
public class SessionDemo1 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //使用session共享数据
        HttpSession session = request.getSession();
        //存储数据
        session.setAttribute("msg","hello,session");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
复制代码

session获取数据:

复制代码
@WebServlet("/SessionDemo2")
public class SessionDemo2 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //使用session共享数据
        HttpSession session = request.getSession();
        //获取数据
        Object msg = session.getAttribute("msg");
        System.out.println(msg);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
复制代码

 

posted @   xjw12345  阅读(45)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示