This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Servlets and the fly likes Could not get session Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Servlets
Reply Bookmark "Could not get session" Watch "Could not get session" New topic
Author

Could not get session

babu krishnan
Greenhorn

Joined: Jun 09, 2002
Posts: 25
HI all,
I have a login page, on logging i call a servlet1 and it takes me to the home page. On home page i have a hyperlink, onclick of that i have to do a task by calling servlet2. Servlet2 needs a context which is set in session by servet1. But in servelt2 iam not getting context set by servlet1. The session in loginpage is lost in servet2. Why is this happening
Ken Pullin
Ranch Hand

Joined: Jan 29, 2001
Posts: 43
Are you doing a:
HttpSession session = request.getSession(true) in the first servlet? If so, you should be able to get the session in the 2nd servlet the same way. Post your code if you want to and I'll have a look at it.
babu krishnan
Greenhorn

Joined: Jun 09, 2002
Posts: 25
This is my servlet1 does:
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException,IOException{
String user="";
String pass="";
res.setContentType("text/html");
HttpSession objSession = req.getSession(true);
user = req.getParameter("username");
pass = req.getParameter("password");

System.out.println("calling in subhaservlet objSession is"+objSession);
if (objSession == null || (objSession.isNew()))
{
String fwdUrl = Framework.getPropertyValue("ematrix.login.page");
RequestDispatcher aRequestDispatcher=getServletContext().getRequestDispatcher(fwdUrl);
aRequestDispatcher.forward(req,res);
return;
}
Context context = (Context) objSession.getAttribute("ematrix.context");
System.out.println("calling in servlet ematrix.context"+context);
String strPageName=Framework.getPropertyValue("ematrix.forward.page");
System.out.println("calling in strPageName context"+strPageName);
ServletContext application = getServletConfig().getServletContext();
//ServletContext ctx = application.getContext("/tops3/*");
//System.out.println("calling in servlet ctx"+ctx);
RequestDispatcher rd = application.getRequestDispatcher(strPageName);
rd.forward(req, res);
return;
}
This is dispatched to a jsp.
In that jsp i have a hyperlink that calls a servlet.
<a href=http://localhost:7001/LoginServlet>Click here</a>
this calls servlet2, that is
public class LoginServlet extends HttpServlet{
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException,IOException{
HttpSession objSession=req.getSession(false);
System.out.println("=====================================");
System.out.println("calling in loginServlet objSession is"+objSession);
if(objSession == null) {
res.sendRedirect(res.encodeRedirectURL("/tops3/tnl/jsp/subhaLogin.jsp"));
return;
}
LoginBean subha = new LoginBean(req,res,objSession);
subha.callTcl();
System.out.println("before calling LoginBean 2 res==> "+req.getAttribute("MQLResult"));
RequestDispatcher rd = this.getServletContext().getRequestDispatcher("/jsp/myresp.jsp");
rd.forward(req, res);
}
}
here my session itself is failing.If i give system.out.println on session obj it is throwing null pointer exception.

But when i submit the form instaed of giving hyperlink this are working fine.
but when i give hyperlink(not submitting) the problem comes up
Thanx in advance
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Could not get session
 
Similar Threads
HFSJ Chapter 6 - Mock exam Question 7
Servlet - doGet - doPost - parameter
Issue with request.setAttribute(String,obj)
Head First Question
Question regarding Head First book chapter 7 mock question