Tetaining session info between jsp page and servlet

Posted on 2005-04-11
Medium Priority
Last Modified: 2010-04-01
I have I website that contains both jsp pages and servlets. In one of my jsp pages I set a session variable using


This information is held fine when I'm just navigating between jsp pages but when I go to a servlet and use

HttpServletRequest req;

I get a null pointer exception. Help greatly needed and hugely appreciated,

Question by:aka_damo
  • 2
  • 2

Author Comment

ID: 13756737
That should be "Retaining session info between jsp page and servlet"
LVL 29

Accepted Solution

bloodredsun earned 500 total points
ID: 13756922
You need this

HttpSession session = request.getSession() ;
String emp_id = session.getAttribute("emp_id");
LVL 29

Expert Comment

ID: 13756966
In your example you are setting data into the session object and then trying to read it from the request object so it wil be null.( and there's a missed cast in my code)

setting JSP

getting JSP
String emp_id = (String)session.getAttribute("emp_id");//notice the cast to String as it actually returns an Object.

getting Servlet
HttpSession session = request.getSession() ;//get session object from the request object
String emp_id = (String)session.getAttribute("emp_id");//now as above

This is because the servlet container, such as tomcat, already provides you with the session object in the JSP so you don't need to worry about it.

Author Comment

ID: 13757069
Cheers bloodredsun,
That works fine. I spotted the missed cast alright but I just used

emp_id = session.getAttribute("emp_id").toString();

Thanks again

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Most folks would know the basics of how Dropbox works, so that’s not the purpose of this article. Security is what it’s all about, so here I’ll share how I choose to secure my Dropbox Account and the Data it contains.
In the below post we have mentioned the best hosting type for startups. Also, check out some of the superlative web hosting companies that are proposing affordable web hosting solutions to host your startup website.
Loops Section Overview
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month17 days, 5 hours left to enroll

864 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