?
Solved

HttpSession

Posted on 2001-06-07
5
Medium Priority
?
516 Views
Last Modified: 2013-11-24
Hi all,

I have 2 servlets:

one servlet sets an attribute like so:

req.setAttribute("Password", password);


Another servlet gets this attribute from the first servlet when it has been called via an URL
the code to get the attribute is:

String Password = (String)req.getAttribute("Password");

this is then output to the screen like so:

out.println(Password);

The question is: when I set the attribute in the first servlet I get an error saying that password is
not defined, how can I get around this as I have nothing that I want to set password to??

Also, I when it is output to screen I get null wrote??

I have been told to use HttpSession for this, has anyone any idea how to use HttpSession for this??
0
Comment
Question by:macca_26
[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
  • 3
5 Comments
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 6163952
Servlet 1:

HttpSession httpsession = httpservletrequest.getSession(true);
httpsession.putValue("Password",password);




Servlet 2:

HttpSession httpsession = httpservletrequest.getSession(true);
out.println(httpsession.getValue("Password"));

should work
Cheers
sudhakar
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 6163972
Servlet 2:

HttpSession httpsession = httpservletrequest.getSession(true);
if(httpsession.getValue("Password")!=null)
   out.println(httpsession.getValue("Password"));

0
 
LVL 14

Accepted Solution

by:
sudhakar_koundinya earned 100 total points
ID: 6164004
Servlet 1:

String password=null;
password=resultset.getString("password");//say
if(password==null)
   password="";
HttpSession httpsession = httpservletrequest.getSession(true);
if(httpsession.getValue("Password")==null)
   httpsession.putValue("Password",password);



Servlet 2:

HttpSession httpsession = httpservletrequest.getSession(true);
if(httpsession.getValue("Password")!=null)
  out.println(httpsession.getValue("Password"));

should work
Cheers
sudhakar
0
 

Expert Comment

by:lingababu
ID: 6164037
above comment should work.
Accept his answer
Cheers
0
 

Author Comment

by:macca_26
ID: 6164063
Thanks Sudhakar,

I have tried this and it does exactly what I want.

Regards.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

719 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