Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Could not get session  RSS feed

 
babu krishnan
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!