• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

session.getAttribute give null when retreiving session Data in jsps

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author and deputy
Posts: 3150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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".
 
Murugan Manickam
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll move this thread to servlets.
 
Murugan Manickam
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic