Solved

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

Posted on 2004-08-19
6
250 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
  • 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

In this article, I will show you HOW TO: Suppress Configuration Issues and Warnings Alert displayed in Summary status for ESXi 6.5 after enabling SSH or ESXi Shell.
Find out what the Office 365 disclaimer function is, why you would use it and its limited ability to create Office 365 signatures.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now