Solved

HttpSession

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
custom annotations 9 38
why cannot we forward request once the response is committed 2 42
Windows 10 IE Certificate Issue 10 45
tomcat startup error 5 59
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

786 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