Solved

HttpSession

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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

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…
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…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

729 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