aspose file tools*
The moose likes Servlets and the fly likes session.getAttribute give null when retreiving session Data in jsps Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "session.getAttribute give null when retreiving session Data in jsps" Watch "session.getAttribute give null when retreiving session Data in jsps" New topic
Author

session.getAttribute give null when retreiving session Data in jsps

Murugan Manickam
Greenhorn

Joined: Jun 04, 2002
Posts: 10
Hi all,
I am using a login page, which inovokes a servlet class, the servlet class get the required data from the properties file and sets a global variable and redirects the response to another jsp.
In the jsp page, when used session.getAttribute("xLogin") gives null.
I am using Apache-tomcat3.3.1 and win2k.
I couldn't retrive the session data in the jsp pages.
Is there any settings to be done for this.
Please help me on this..
thanks in advance.
Murugan
Balaji Loganathan
author and deputy
Bartender

Joined: Jul 13, 2001
Posts: 3150
try to use encodeRedirectURL(java.lang.String url) or encodeURL(java.lang.String url) ,normally it should work.
Check whether you assigned the global variable to the session "xLogin".


Spritle Software Blogs
Murugan Manickam
Greenhorn

Joined: Jun 04, 2002
Posts: 10
Hi ,
Thanks a lot for your response.
I used encodeURL and encodeRedirectURL, but it is not working. the getAttribute gives null.
let me paste the sample code, for this scenario.
I dont think, there is problem in the code,bcoz, its working fine using resin.
using tomcat3.3.1 it gives null.
The session variable setting is thro. I used getAttribute() in the servlet class for testing purpose,it gives right answer. but retreiving the same data thro jsps after sendRedirect(), its now working. Using getRequestDispatcher(),its working.
but it will not work on the entire session..
Expecting ur reply,
bye,
Murugan
<html>
<head>
<title>This is the login page</title>
</head>
<body>
<h1>This is the Login Page</h1>
<form name="frm" method="post" action="/servlet/NC_ValidateLogin">
Userid :
<input type="text" name="userid" >
Password:
<input type="password" name="password" >
<input type="Submit" name="submit" value="Submit">
<input type="Reset" name="reset" value="Clear">
</form>
</body>
</html>
-- Servlet class file --
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class NC_ValidateLogin extends HttpServlet {
public void init (ServletConfig config) throws ServletException {
super.init(config);
}
public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
doPost(req, res);
}
public void doPost (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
String nom = "Toto";
String sUser = null;
String sPass = null;
if (req.getParameter("userid") != null)
sUser = req.getParameter("userid");
if (req.getParameter("password") != null)
sPass = req.getParameter("password");
HttpSession session = null;
try {
session = req.getSession(true);
session.setAttribute("LoginName", nom );
session.setAttribute("user",sUser);
session.setAttribute("pass",sPass);
//res.sendRedirect("/NetCAST/Home/JSPA.jsp");
//getServletConfig().getServletContext().getRequestDispatcher("/NetCAST/Home/JSPA.jsp").forward(req, res);
res.sendRedirect(res.encodeURL("/NetCAST/Home/JSPA.jsp"));
String strUser = (String)session.getAttribute("user");
System.out.println("****"+strUser);
} catch (Exception e) {
System.out.println("Pas cool !");
}
}
}
-----JSP Page ----

<%@ page import="java.util.*"%>
<% String nom = (String)session.getAttribute("LoginName"); %>
<% String user = (String)session.getAttribute("user"); %>
<% String pass = (String)session.getAttribute("pass"); %>
<HTML>
<HEAD><TITLE>Age ?</TITLE></HEAD>
<BODY>
<BR>
<BR>
What is your Name ?
<BR>
My name is Mr. <%= nom %>
<br>
My userid is <%= user %>
<br>
My password is <%= pass %>
</BODY>
</HTML>
Balaji Loganathan
author and deputy
Bartender

Joined: Jul 13, 2001
Posts: 3150
Originally posted by Murugan Manickam:
Hi ,
using tomcat3.3.1 it gives null.
same data thro jsps after sendRedirect(), its now working. Using getRequestDispatcher(),its working.
but it will not work on the entire session..

Is ur code is working or not? for me ur code is working perfectly with tomcat 3.2
I didn't changed ur code.
i used res.sendRedirect(res.encodeURL("/NetCAST/Home/JSPA.jsp"));in servlet for redirecting.
is ur problem sloved ??

Please post ur question at servlet section than here for more replies.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I'll move this thread to servlets.
Murugan Manickam
Greenhorn

Joined: Jun 04, 2002
Posts: 10
Hi balaji,
Sorry balaji for the delay..
Can you send your server.xml file for my reference.
I used context tag only in the server.xml, other things are default.
Is there any settings to be done for session id in server.xml file..
The session id of servlet class and the next jsp page are different..But it should be same for the session.
I dont know how to find here the session expires..
can you please send the server.xml for my reference..
bye,
Murugan
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
If you are in fact doing a redirect instead of forwarding, that is the problem. A redirect essentially aborts processing of one request and causes the browser to create a new request. The cookie that gives the session id won't be available to the JSP. You should forward the request instead of redirecting.
Bill
Murugan Manickam
Greenhorn

Joined: Jun 04, 2002
Posts: 10
Hi,
I used response.sendRedirect("url").forward(request,response), it works for the next page alone. not for otherpages.
But how come the same code response.sendRedirect() works fine using resin.
using tomcat it gives null, when retreiving the session data in the jsp pages..
I dont think problem in the codings..
i am suspecting on settings in the server.xml
Thanks in advance.
bye,
murugan
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: session.getAttribute give null when retreiving session Data in jsps