Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Servlets
|
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Servlets section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 10th, 2005, 02:50 PM
Registered User
 
Join Date: Oct 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default Tracking variables in servlets

Hi
   Can somebody lemme know how can i track the userId and password in related java servlets...or how should I pass the same to further servlets after I login. I am new to servlets so don't have much hold over it..I have been adviced to use beans for the same...but I am again new to the concept of java Beans..can someone guide through..as to how to accomplish the task..?
Thanks in advance for that!!

ans4u
 
Old January 16th, 2006, 12:40 AM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 198
Thanks: 0
Thanked 0 Times in 0 Posts
Default

A simple way is to use session attributes. For example, in one servlet you can set a session attribute like this:

request.getSession().setAttribute("user", "simplyAns");

Then in another servlet, you can retrieve the value like this:

String user = (String)request.getSession().getAttribute("simplyA ns");

If you want to get a bit more complicated, you can look into defining security roles in your web.xml and having the container (e.g. Tomcat) enforce authentication automatically. But I personally find this approach cumbersome and I use something similar to what I laid out above.

Jon Emerson
Adobe Systems, Inc.
http://www.jonemerson.net/
 
Old January 19th, 2006, 02:09 AM
Registered User
 
Join Date: Dec 2005
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to arun_sunmic
Default




    It is very easy to track variables using session option.


          once a session object is set,it is available to all servlets/jsp running in the same context.


    The code for creating session is as foolws:

   HttpSession session = request.getSession(true);

        To create a session object:

      session.setAttribute("obj","wrox");

     Here "obj" is the session object and "wrox" is the value for the session object "obj".

    u can get the session attribute value using in other jsp or servlets:

   session.getAttribute("obj");

    This returns the value for the session object "obj" i.e, "worx".


   Note in JSP session is an implicit object,so by enabling session="true" in the page directive session object is available.

     Try this!

V.S.Arun





Similar Threads
Thread Thread Starter Forum Replies Last Post
Inner classes in Servlets vikkiefd Servlets 1 April 24th, 2008 04:30 AM
Servlets achukuttan Servlets 1 March 26th, 2007 02:04 AM
Swings and servlets sharadagarwal BOOK: Beginning Java 2 3 September 12th, 2006 04:39 AM
Filters in servlets Ayusman Servlets 0 July 19th, 2005 07:46 AM
Email using servlets sonurijs Pro JSP 3 March 11th, 2005 02:37 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.