Solved

HttpSession

Posted on 2001-06-07
5
505 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 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
tomcat startup error 5 136
hibernate jars 4 61
hibernate example for saving data 19 80
java mysql insert application 14 48
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

697 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