【问题标题】:HttpServletRequest and session associated with itHttpServletRequest 和与之关联的会话
【发布时间】:2015-01-28 16:24:45
【问题描述】:

这是我的控制器类:

public ModelAndView handleRequest(HttpServletRequest request,
        HttpServletResponse response) throws Exception {

    System.out.println("request in controller is " + request);
    request.setAttribute("message", "Stackoverflow");
    request.getSession().setAttribute("name", "testing");
    return new ModelAndView("test1");

}

test1.jsp

<html>
request  in test1 is <%=request%><br> 
the value of request message in test1 is      <%=request.getAttribute("message")%><br>
session value in test1 is <%=request.getSession().getAttribute("name")%>
<br><br>
<a href="test2.jsp" >next</a>
</html>

test2.jsp

<html>
request  in test2 is <%=request%><br> 
the value of request message in test2 is <%=request.getAttribute("message")%><br>
session value in test2 is <%=request.getSession().getAttribute("name")%>

</html>

控制器输出:: 控制器中的请求是 org.apache.catalina.connector.RequestFacade@33480f22

test1.jsp 输出: test1 中的请求是 org.apache.catalina.core.ApplicationHttpRequest@68d91bc4 test1 中请求消息的值为 Stackoverflow test1 中的会话值正在测试中

test2.jsp 输出: test2 中的请求是 org.apache.catalina.connector.RequestFacade@6289cd44 test2中请求消息的值为null

test2 中的会话值正在测试中

这里我的理解是所有 3 种情况下的请求对象都是不同的。但我猜对控制器的请求及其随后传递给 test1.jsp 的请求在某种程度上是相关的。(有人可以解释一下吗) 我完全困惑的是,如果至少 test1 和 test2 之间的请求不同(因为 test1 中显示的请求属性在 test2 中打印为 null),那么它们之间的会话对象如何相同?两个不同的请求对象如何提供相同的会话? 我的理解是会话对于唯一请求对象是唯一的。 有人可以解决我对此的疑问吗?提前谢谢

【问题讨论】:

    标签: jsp session servlets


    【解决方案1】:

    不,你错了。

    一个请求,顾名思义,是一个 HTTP 请求的模型。当用户点击一个链接时,会向服务器发送一个请求,由控制器和 JSP 处理,然后生成响应。然后请求就消失了。

    会话的存在正是为了能够将来自给定浏览器的所有请求“分组”在一起。典型用例:您登录到 Web 应用程序,将用户名存储在会话中,然后能够为该用户的所有后续请求找到该用户名,直到会话超时或失效。所以是的,来自同一个浏览器的多个请求确实共享同一个会话。如果每个请求都有自己的会话,那么会话将完全没用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-08
      • 1970-01-01
      • 2011-01-19
      • 2012-04-19
      • 1970-01-01
      • 2013-09-28
      • 1970-01-01
      • 2015-12-10
      相关资源
      最近更新 更多