user2427906
user2427906

Reputation: 341

servlet basic object in jsp

HttpServletRequest basic object in jsp. which one prefer to using?

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

using implementing of this two statement in same place session?

Upvotes: 0

Views: 47

Answers (2)

Hardik Mishra
Hardik Mishra

Reputation: 14877

Both statement serves different purpose.

First method is divided into two parts.

First one is request.getSession()

Returns the current session associated with this request, or if the request does not have a session, creates one.

Then setAttribute("myObjectId", myObject); to the session object. The values stored in this scope will persist through out current session. Read More on session.setAttribute:

The second one request.setAttribute("myObjectId", myObject) method --

Stores an attribute in this request. Attributes are reset between requests. This method is most often used in conjunction with RequestDispatcher.

Read More on request.setAttribute

Upvotes: 0

Mark Thomas
Mark Thomas

Reputation: 16635

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

Upvotes: 1

Related Questions