Solved

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

Posted on 2004-08-19
6
261 Views
Last Modified: 2012-05-05
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
Comment
Question by:Rrugemalira
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 32

Accepted Solution

by:
ldbkutty earned 160 total points
ID: 11846009
First,
>> firstname = (String) session.getAttribute(firstname);
should be:
firstname = (String) session.getAttribute("firstname");
0
 
LVL 3

Assisted Solution

by:pat5star
pat5star earned 170 total points
ID: 11848705
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
 
LVL 11

Assisted Solution

by:sompol_kiatkamolchai
sompol_kiatkamolchai earned 170 total points
ID: 11849669
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
Independent Software Vendors: 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!

 
LVL 11

Expert Comment

by:sompol_kiatkamolchai
ID: 11849678
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
 
LVL 11

Expert Comment

by:sompol_kiatkamolchai
ID: 11850157
Some typo error

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

it's 60 minutes, not in second.

sorry.
0
 
LVL 11

Expert Comment

by:sompol_kiatkamolchai
ID: 11850163
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

Independent Software Vendors: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
web service Rest Client creation 4 102
getting Http 404 error in jsp forms 8 91
listing all functions in JavaScript 19 253
mysql jsp example issue 32 96
This article describes how to import an Outlook PST file to Office 365 using a third party product to avoid Microsoft's Azure command line tool, saving you time.
Configuring Remote Assistance for use with SCCM
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Suggested Courses

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question