【问题标题】:Can I access a cookie created by a servlet in a html page?我可以在 html 页面中访问由 servlet 创建的 cookie 吗?
【发布时间】:2019-06-28 05:57:39
【问题描述】:

我正在尝试创建一个在所有页面中都需要用户 ID 的预订表单。我想知道当我将页面重定向为 [HTML->servlet->HTML]

时如何处理会话

【问题讨论】:

  • 请分享,你到目前为止所尝试的。

标签: java html servlets


【解决方案1】:

您可以使用 HttpSession.setAttribute & HttpSession.getAttribute 例如:

JSP 页面:

       //getting id value 
        String id= request.getParameter("user_id");
        //the variable which is set in session scope you can use anywhere 
         request.getSession().setAttribute("id",id);
          request.getRequestDispatcher("/yourServleturl").forward(request,response);

现在要获得session 属性,在servlet 中写如下:

   HttpSession session=request.getSession();
      //getting value in session
    String id=session.getAttribute("id").toString();
    //do further processing

希望这会有所帮助!

【讨论】:

  • 即使我浏览各种 html 页面,此会话是否仍然有效?
  • 是的,直到你没有unset它,它只会保持设置。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-12
  • 2021-09-12
  • 1970-01-01
  • 1970-01-01
  • 2017-06-19
  • 2016-03-24
  • 2018-10-20
相关资源
最近更新 更多