0

HttpServletRequestjsp中的基本对象。哪个更喜欢使用?

request.getSession().setAttribute(myObjectId, myObject);
request.setAttribute("myObjectId", myObjectId);

在同一个地方会话中使用这两个语句的实现?

4

2 回答 2

1

It depends what you want. In the first case myObject will have session scope (it will be available for the lifetime of the session). In the second it will have request scope (it will be available for the lifetime of the request).

There is also application scope.

I'd recommend reading section JSP.1.8.2 of the JSP specification for more details.

http://jcp.org/aboutJava/communityprocess/mrel/jsr245/index.html

于 2013-05-28T10:48:07.857 回答
0

两种说法都有不同的用途。

第一种方法分为两部分。

第一个是request.getSession()

返回与此请求关联的当前会话,或者如果请求没有会话,则创建一个。

然后setAttribute("myObjectId", myObject);到会话对象。存储在此范围中的值将在当前会话中持续存在。阅读有关session.setAttribute的更多信息:

第二种request.setAttribute("myObjectId", myObject)方法——

在此请求中存储一个属性。在请求之间重置属性。此方法最常与 RequestDispatcher.

阅读更多关于request.setAttribute

于 2013-05-28T11:23:37.097 回答