Solved

HttpSession

Posted on 2001-06-07
5
500 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
  • 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 25 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

743 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

12 Experts available now in Live!

Get 1:1 Help Now