session problem
In XXX servlet i have defined the session by saying
HttpSession session=request.getSession(true);
then i put an object in session.setAttribute
after that in YYY servlet, i m calling the session by request.getSession() and i even tried by calling with the code request.getSession(true) but i cant seem to get the object from the session i placed in the before servlet XXX, i found that the session in YYY servlet is created new instead of calling the old one from the another servlet. so how to access the old session which was created in XXX servlet?
[575 byte] By [
Bhavika] at [2007-10-3 0:52:05]

[nobr]Can you post some code showing this? Just a simple one, showing the two servlets, how you have them configured, and how you invoke them (from JSPs?)
Are both of these servlets are in the same web application?
Are you refreshing the page/clicking the link immediately or is there a session timeout?
Try this as a JSP page. It basically shows some basic info about your system. You can see the current session id, and if it is new/retained.
<h1>Test page</h1>
<h2> Server Info </h2>
Server info = <%= application.getServerInfo() %> <br>
Servlet engine version = <%= application.getMajorVersion() %>.<%= application.getMinorVersion() %><br>
Java version = <%= System.getProperty("java.vm.version") %><br>
Java home = <%= System.getProperty("java.home") %><br>
Session id = <%= session.getId() %><br>
From cookie = <%= request.isRequestedSessionIdFromCookie() %> <br>
Is new = <%= session.isNew() %> <br>
[/nobr]
thanks evnafets, i will check the session info by the code you gave me...ok basically i m calling the servlet through jsp by calling it in <form tag and putting the URL in action...there the servlet connects with the database and if the username and passwd is correct then
session=request.getSession(true);
ConnectionToDatabase connectionToDatabase = new ConnectionToDatabase();
session.setAttribute("Connection",connectionToDatabase);
then by response.sendRedirect i go to the another JSP. after that i fill up the form and after submitting the info i go to another servlet. it is called the same way in the form tag and URL is in action...there
session=request.getSession(false);
ConnectionToDatabase connection=(ConnectionToDatabase) session.getAttribute("Connection");
Connection conn=connection.getConnection();
i try to get the object by calling the getAttribute but i get null and i checked and it is a new session not the old one....cause when i check if its isNew() and its new..so help me how come it is creating a new session and where does my old session go?
Both of the servlets are in the same web application and in same folder.
i dont think there is session time out and i m not refreshing or anything.>
Hi Bhavik, I have given the sample code of servlet for creating session. You can check with it. Also I have given a Jsp page, where I am checking the session. If the session is null redirect to Relogin page.
/*
* LoginValidateServlet.java
*
* Created on May 31, 2006, 2:30 PM
*/
package ss_com.servlets;
import ss_com.beans.UserLoginBean; //Simple java bean with get and set methods for userid and password.
import ss_com.javas.LoginConnection; //I have created this class to establish database connection.
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
*
* @author suresh
* @version 2
*/
public class LoginValidateServlet extends HttpServlet {
private String usercode;
private String password;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
usercode = request.getParameter("usercode");
password = request.getParameter("password");
try
{
//setting the properties to the UserLoginBean bean.
UserLoginBean ulb = new UserLoginBean();
ulb.setUsercode(usercode);
ulb.setPassword(password);
//Validating the user. I have created seperate java class called 'LoginConnection' to establish database connection and calling its method 'isValidUser(UserLoginBean bean)'. This method return true if the user is valid.
If it returns true, then I am creating new session.
//It is the best idea to create a seperate bean for session object, which contains set and get method for userid and password.
LoginConnection lc = new LoginConnection();
RequestDispatcher rd;
if(lc.isValidUser(ulb))
{
//setting session attribute for the user.
HttpSession hs = request.getSession();
hs.setAttribute("UserLoginBean",ulb);
hs.setMaxInactiveInterval(300); //This method is to set the time so that how long the session can wait before invalidating the session. After the specified time, the session will get expired.
//Instead of using sendRedirect(), check with this forward() method.
rd = request.getRequestDispatcher("/jsp/SS_HomePage.jsp");
rd.forward(request, response);
}
else
{
//Redirect to loginerror page.
rd = request.getRequestDispatcher("/SS_LoginError.jsp");
rd.forward(request, response);
}
}
catch(Exception ee){}
}
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>SS_HomePage</title>
</head>
<%
if(session.getAttribute("UserLoginBean") == null)
response.sendRedirect("/SS_BackOffice/SS_ReLogin.jsp");
%>
<frameset rows="23%,*">
<frame target="contents" marginwidth="20" marginheight="1" src="/SS_BackOffice/jsp/Banner.jsp" name="banner" scrolling="no" noresize="noresize" >
<frameset cols="30%,*">
<frame src="/SS_BackOffice/jsp/Menu.jsp" name="contents" target="main">
<frame src="/SS_BackOffice/jsp/Welcome.jsp" name="main">
</frameset>
</html>
}