Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

Session Tracking in Servlet

Thread view: 
kssk - 28 Aug 2006 06:47 GMT
I have got a jsp form which consist of name, phone, email, etc.
I want to use session tracking in servlet:

In servlet my code are like:

String gn=session.setAttribute("gn",request.getParameter("givenname"));
String email=session.setAttribute("em","email");
.
.
.
.
and so on and In jsp

I get session value by

String given=session.getAttribute("gn");

My program work properly but I would like to reduce the code as I have
to write setAttribute for all the detail.

Does anyone help me?
Babu Kalakrishnan - 28 Aug 2006 08:21 GMT
> I have got a jsp form which consist of name, phone, email, etc.
> I want to use session tracking in servlet:
[quoted text clipped - 15 lines]
> My program work properly but I would like to reduce the code as I have
> to write setAttribute for all the detail.

The usual practice is to use an Object that encapsulates all these
details, and store it in the session. For instance in your case you
could have a User class with the parameters "name", "email" etc as its
instance variables - and store an instance of "User" in the session
using a single attribute. e.g. Your servlet code could be :

User user = new User();
user.setName(request.getParameter("name"));
user.setEmail(request.getParameter("email"));
..... // populate all fields of user from parameters received
request.getSession().setAttribute("user",user);

Now in your JSP you could get the "user" object from the session once,
and then access its instance variables (using getter methods that you
provide). Or if you're using tag libraries (which is generally a good
thing to do in JSPs), you could access them using expressions such as
"${user.name}" or "${user.email}"

BK


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.