Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 287
  • Last Modified:

JSP, Java bean, Servlets: P'se instruct on how to use a session object

What am I doing wrong?
Is this the way to store and retreave an object from a session object?

      // Storing:
       HttpSession session = request.getSession(true);
      session.setAttribute("firstname", fname);

      // Retreaving:
      HttpSession session = request.getSession(true);
      firstname = (String) session.getAttribute(firstname);

The above doesn't work.  What am I missing? I'd imagined it is this easy!

Q. How does one declare a session object? Is the session object for storage and transport so to speak?
Q. How does one get a session object id?
Q. On retreaving, how do I ask from the session object what I stored in it? Or am I totally off the mark with these questions?

Please instruct briefly but succinctly. I'd also appreciate being directed to a tutorial.

 
0
Rrugemalira
Asked:
Rrugemalira
  • 4
3 Solutions
 
ldbkuttyCommented:
First,
>> firstname = (String) session.getAttribute(firstname);
should be:
firstname = (String) session.getAttribute("firstname");
0
 
pat5starCommented:
1) To store something you get a reference to the existing session or create a new one.

HttpSession session = request.getSession();

Then you store objects in the session by using the method setAttribute:

String someValueToBeStoredInSession = "blah";
session.setAttribute("sessionObjectName", someValueToBeStoredInSession);

2) You can get the session id by calling:

session.getId();

but normally you don't need it as request.getSession() returns a reference to the current users session.

3) You retrieve a session object by using the method getAttribute:

session.getAttribute("sessionObjectName");

This method returns an Object, so you have to cast it to it's correct type. Since we know sessionObjectName is a String, this will work:

String someValueRetrievedFromSession = (String)session.getAttribute("sessionObjectName");

System.out.println(someValueRetrievedFromSession);

will print "blah".


You have it pretty much correct in your example and ldbkutty's example will correct your error assuming that fname is a valid String.

Hope that helps,

-Pat
0
 
sompol_kiatkamolchaiCommented:
Here is briefly and cover all basic and advance in session management

You can use session in both JSP or Servlet.

1.In servlet you have to explicitly retrieve session object from request object.

HttpSession session = request.getSession();

and you can pass boolean to getSession(false) that mean if session object does not exist in request object it will not create a new one. The default parameter is true.

2.HttpSession is an interface that allow you to set or get Object, here is an api

void setAttribute(String name, Object value)
Object getAttribute(String name)

3.You can expunge session by

void invalidate()

4.You can configure timeout of session in deployment descriptor (web.xml)

<session-config>
  <session-timeout>60</session-timeout>
</session-conifg>

This example, container will terminate session after client doen not have any activity for 60 seconds
Note.If the value is 0 or negative value means session will never expire

4.1 You can also set session timeout or get its value by programming

void setMaxInactiveInterval(int seconds) // a negative value mean the session will not expire
int getMaxInactiveInterval()

5.You can get session id by

String getId()

6.You can check that session is just create by

boolean isNew()

That's all.

Regards,
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
sompol_kiatkamolchaiCommented:
Something more..

7.In jsp the session object will depend on page directive, the default value is true

<%@page session="true"%>

If this declare, so in the jsp page you can use it via implicit object session

<%
// you can use session without declaration.
session.setAttribute("abc", abc);
%>
0
 
sompol_kiatkamolchaiCommented:
Some typo error

<session-config>
  <session-timeout>60</session-timeout>
</session-conifg>

it's 60 minutes, not in second.

sorry.
0
 
sompol_kiatkamolchaiCommented:
Another advance on session management is listener.
You can create listener that can trap event such as session create, object bound to session.
Request me, I will explain in details.

Regards,
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now