Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2004-08-19
6
Medium Priority
?
277 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 640 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 680 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 680 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
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!

 
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

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!

Question has a verified solution.

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

Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
What we learned in Webroot's webinar on multi-vector protection.
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…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

670 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