Solved

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

Posted on 2004-08-19
6
264 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article describes how to import Lotus Notes Contacts into Outlook 2016, 2013, 2010 and 2007 etc. with a few manual steps. You can easily export and migrate Lotus Notes contacts into Microsoft Outlook without having to use any third party tools.
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

630 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