This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes session handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "session handling" Watch "session handling" New topic
Author

session handling

Santanu Mahato
Greenhorn

Joined: Jan 03, 2013
Posts: 3

I have a web application where I am using session to remember the username throughout the session

HttpSession session =request.getSession();
session.setAttribute("LoginName",request.getParameter("username"));
request.setAttribute("uname",request.getParameter("username") );
but then when I am accessing the username in a jsp one jsp file is getting the data but another jsp file is not able to get data and also a servet file is not able to get the session value??

-------------------------------------------------------------------
addUser.jsp ///the file that dosn't recive the session value.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page import="java.sql.*,java.util.*,mypackage.*" %>
<%@ page isErrorPage="true" import="java.io.*" %>


<%out.print(session.getAttribute("LoginName")); %>
-------------------------------------------------------------------
addUserController.java // the servlet file that doesn't receive session

HttpSession session=request.getSession();
System.out.print(session.getAttribute("LoginName"));
//System.out.println("hello user adder");
String loginNam=(String)session.getAttribute("LoginName"); //Set the user id in session
System.out.println(loginNam);
whenever I thyed to access the session it always giving null.



what can be the reason???

Can you please tell me the full code for session.?

thanks in advance.
Ayan mallick
Ranch Hand

Joined: Nov 17, 2012
Posts: 44
could you post your web.xml or could you tell us the flow control of your app or post your code so that we
can have idea about what's going wrong or whether session is being invalidated anywhere.
Santanu Mahato
Greenhorn

Joined: Jan 03, 2013
Posts: 3
I am having some problem in attaching the files so I am pasting here only..
I am postings three files
LoginController.java
dashboard.jsp
addUser.jsp

the problem lies there only...
I am expaning the flow later

LoginController.java
-------------------------------------

package mypackage;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
* Servlet implementation class LoginController
*/
public class LoginController extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public LoginController() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

PrintWriter out=response.getWriter();
HttpSession session =request.getSession();
session.setAttribute("LoginName",request.getParameter("username"));
request.setAttribute("uname",request.getParameter("username") );
LoginBean log=new LoginBean();
log.setUserName(request.getParameter("username"));
log.setPassword(request.getParameter("password"));
LoginBSL lbsl=new LoginBSL();
int role=lbsl.validateData(log); //call a method validateData and return role tagged
if (role==1)//For Admin
request.getRequestDispatcher("dashboard.jsp").forward(request,response);
if (role==2)//Inventory Manager
request.getRequestDispatcher("DashBoardInventory.jsp").forward(request,response);
if (role==3) {//Retailer

int flagValue=lbsl.checkFirst(log);
if (flagValue==1)//update retailer info
request.getRequestDispatcher("RetailerDeltailControler").forward(request,response);
if (flagValue==0)//retailer dashboard
request.getRequestDispatcher("dashboardRetailer.jsp").forward(request,response);
}
if (role==0) {//Other User
out.print("<script type='text/javascript'>alert('Error: Login Not Possible...Invalid username or password!!!');" +
"window.location.href='login.html';</script>");
//out.print("invalid login");
//request.getRequestDispatcher("login.html").forward(request,response);
}

}

}
------------------------------------------------------------------------------
Dashboard.jsp


-----------------------------------------------------------------------------
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page import="java.sql.*,java.util.*,mypackage.*" %>
<%@ page isErrorPage="true" import="java.io.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Dashboard - Admin</title>
<link rel="stylesheet" type="text/css" href="css/theme4.css" />
<link rel="stylesheet" type="text/css" href="css/style.css" />
<SCRIPT type="text/javascript">
window.history.forward();
function noBack() { window.history.forward(); }
</SCRIPT>
<!--[if IE]>
<link rel="stylesheet" type="text/css" href="css/ie-fix.css" />
<![endif]-->
</head>
<body onload="noBack();" onpageshow="if (event.persisted) noBack();" onunload="">




<div id="container">
<div id="header">
<h2>Telecom Store Admin area</h2>
<div id="topmenu">
<ul>
<li class="current"><a href="dashboard.jsp">Dashboard</a></li>
<li><a href="LogoutController" class="logout">Logout</a></li>
</ul>
</div>
</div>

<div id="wrapper">
<div id="content">
<div id="box">
<!-- FORM STARTS HERE-->


<table style="text-align:center;">
<tr><b><font size=4 color="black">

<%out.print(session.getAttribute("LoginName")); %>


</tr>
</font> </b><tr>
<td width="33%">
<h3>Add New User</h3><br>
<a href="addUser.jsp"><img src="img/adduser.png" width="128px" height="128px" alt="Add New User" title="Add New User" /></a>
</td>
<td width="33%">
<h3>Update User</h3><br>
<a href="UpdateUser.jsp"><img src="img/updateuser.png" width="128px" height="128px" alt="Add New User" title="Add New User" /></a>
</td>
<td width="33%">
<h3>View User</h3><br>
<a href="DisplayUser.jsp"><img src="img/viewuser.png" width="128px" height="128px" alt="View Users" title="View Users" /></a>
</td>
</tr>
<tr>
<td width="33%"></td>
<td width="33%">
<h3>View Retailer not tagging</h3><br>
<a href="FirstTaggedController?Id=<%=session.getAttribute("LoginName") %>"><img src="img/priviledge.png" width="128px" height="128px" alt="View User Priviledges" title="View User Priviledges" /></a>
</td>
<td width="33%"></td>
</tr>

</table>

<!-- FORM ENDS HERE-->
</div>
</div>
<!-- <div id="sidebar">
<ul>
<li><h3><a href="#" class="house">Dashboard</a></h3>
<ul>
<li><a href="#" class="report">Sales Report</a></li>
<li><a href="#" class="report_seo">Stock Report</a></li>
<li><a href="#" class="search">Search</a></li>
</ul>
</li>
<li><h3><a href="#" class="folder_table">Orders</a></h3>
<ul>
<li><a href="#" class="addorder">New order</a></li>
<li><a href="#" class="shipping">Shipments</a></li>
<li><a href="#" class="invoices">Invoices</a></li>
</ul>
</li>
<li><h3><a href="#" class="manage">Manage</a></h3>
<ul>
<li><a href="#" class="product">Products</a></li>
<li><a href="#" class="folder">Product categories</a></li>
<li><a href="#" class="stock">Stock</a></li>
<li><a href="#" class="promotions">Promotions</a></li>
</ul>
</li>
<li><h3><a href="#" class="user">Users</a></h3>
<ul>
<li><a href="#" class="useradd">Add user</a></li>
<li><a href="#" class="group">User groups</a></li>
<li><a href="#" class="search">Find user</a></li>
<li><a href="#" class="online">Users online</a></li>
</ul>
</li>
</ul>
</div>
</div>
<div id="footer">
<div id="credits">
Telecom Store Inventory
</div>
<br />

</div>
</div>-->
</body>
</html>

----------------------------------------------------------------------------------------

addUser.jsp


--------------------------------------------------------------------------------------------

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page import="java.sql.*,java.util.*,mypackage.*" %>
<%@ page isErrorPage="true" import="java.io.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Add New User</title>
<link rel="stylesheet" type="text/css" href="css/theme4.css" />
<link rel="stylesheet" type="text/css" href="css/style.css" />

<script type="text/javascript">
window.history.forward();
function noBack() { window.history.forward(); }

function validate()
{

var name=document.getElementById("name").value;
var address=document.getElementById("txtaddress").value;
var contact=document.getElementById("txtcontact").value;
var emailid=document.getElementById("txtemail").value;
var roletag=document.getElementById("tagrole").value;
var acontact="[^0-9]";
var x=document.getElementById("txtemail").value;
var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");

if(name=="" && address=="" && contact=="" && emailid=="" && roletag=="-1"){
alert("All field are mandatory.");
return false;
}
else if(name=="")
{
document.getElementById("blankname").innerHTML="Provide user number";
return false;
}

else if(address=="")
{
document.getElementById("blankname").innerHTML="";
document.getElementById("blankaddress").innerHTML="Provide your address";
return false;
}

else if(contact=="")
{
document.getElementById("blankname").innerHTML="";
document.getElementById("blankaddress").innerHTML="";
document.getElementById("blankcontact").innerHTML="Provide contact number";
return false;
}

else if(contact.length<10 ||contact.length>10)
{
document.getElementById("blankname").innerHTML="";
document.getElementById("blankaddress").innerHTML="";
document.getElementById("blankcontact").innerHTML="Contact have exactly 10 digits";
return false;
}

else if(contact.match(acontact))
{
document.getElementById("blankname").innerHTML="";
document.getElementById("blankaddress").innerHTML="";
document.getElementById("blankcontact").innerHTML="Contact field contain only numbers";
return false;
}

else if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
{
document.getElementById("blankname").innerHTML="";
document.getElementById("blankaddress").innerHTML="";
document.getElementById("blankcontact").innerHTML="";
document.getElementById("blankemail").innerHTML="Not a valid Email ID ";
return false;
}

else if(roletag==-1)
{
document.getElementById("blankname").innerHTML="";
document.getElementById("blankaddress").innerHTML="";
document.getElementById("blankcontact").innerHTML="";
document.getElementById("blankemail").innerHTML="";
document.getElementById("blankroletag").innerHTML="Choose a role";
return false;
}


}
</script>
</head>

<body onload="noBack();" onpageshow="if (event.persisted) noBack();" onunload="">
<div id="container">
<div id="header">
<h2>Telecom Store Inventory Admin area</h2>
<div id="topmenu">
<ul>
<li><a href="dashboard.jsp">Dashboard</a></li>

<li class="current"><a href="#">Users</a></li>


</ul>
</div>
</div>
<div id="top-panel">
<div id="panel">
<ul>
<li><a href="add_new_user.html" class="useradd">Add New User</a></li>
<li><a href="DisplayUser.jsp" class="group">View User Records</a></li>

</ul>
</div>
</div>

<div id="wrapper">
<div id="content">
<div id="box">
<!-- FORM STARTS HERE-->
<h3>Add New User</h3>
<%out.print(session.getAttribute("LoginName")); %>
<form name="myForm" id="form" action="AddUserController" method="post" onsubmit="return validate()">
<table width="100%">
<!-- generate USER_ID -->
<tr>

<td width="33%">Enter User Name:</td>
<td width="33%"><input type="text" name="name" id="name" /></td>
<td width="33%"><div style="color:black" id="blankname"></td>
</tr>

<tr>
<td width="33%">Address :</td>
<td width="33%"><textarea name="txtaddress" id="txtaddress"></textarea></td>
<td width="33%"><div style="color:black" id="blankaddress"></td>
</tr>
<tr>
<td width="33%">Contact Number :</td>
<td width="33%"><input type="text" name="txtcontact" id="txtcontact"></td>
<td width="33%"><div style="color:black" id="blankcontact"></td>
</tr>
<tr>
<td width="33%">Email Id :</td>
<td width="33%"><input type="text" name="txtemail" id="txtemail">
<td width="33%"><div style="color:black" id="blankemail"></td>
</tr>

<tr>
<td width="33%">Role Tagged:</td>
<td width="33%">
<select name="tagrole" id="tagrole">
<option value="-1">Choose Role</option>
<option value="0">Admin</option>
<option value="1">Inventory Manager</option>
<option value="2">Retailer</option>
</select>
</td>
<td width="33%"><div style="color:black" id="blankroletag"></td>
</tr>
<!--TAG PRODUCT WITH RETAILER-->
<!--<tr>
<td>Tag Product id:</td>
<td>
<select name="tagpid" id="tagpid">
<option value="0">Choose Product Id</option>
<option value="1">p1</option>
<option value="2">p2</option>
</select>
</td>
<td><div style="color:white" id="blankubar"></td>
</tr>-->
<!--SEND THE DATE OF TAGGING TO DB FOR TAGGED DATE FIELD-->

<tr>
<td colspan="3" align="center">
<input type="SUBMIT" name="submit" id="submit" />
<input type="RESET" name="reset" id="reset" />
</td>
</tr>
</table>
</form>
<!-- FORM ENDS HERE-->
</div>
</div>
<div id="sidebar">
<ul>
<li><h3><a href="dashboard.jsp" class="house">Dashboard</a></h3>
<ul>
<li><a href="add_new_user.html" class="useradd">Add New User</a></li>
<li><a href="DisplayUser.jsp" class="group">View User Records</a></li>

<li><a href="LogoutController" class="logout">Logout</a></li>
<!--<li><a href="#" class="search">Find User/a></li>-->
</ul>
</li>
</div>

<!--<div id="footer">
<div id="credits">
Telecom Store Inventory
</div>
<br />

</div>-->
</div>

</body>
</html>

<!--[if IE]>
<link rel="stylesheet" type="text/css" href="css/ie-fix.css" />
<![endif]-->


</head>
<body>
-------------------------------------------------------------
Flow:
using username and password I am logging in and according to the user dashboard is being displayed
suppose I am loging in as admin and admin dashboard is displayed where I am having the session.
but in admin dashboard when I am adding new user there I am losing the session and later in whole application session is lost.


please help
Ayan mallick
Ranch Hand

Joined: Nov 17, 2012
Posts: 44
I don't see any problem.Are you sure that in web.xml there is no session time out limit.or else add one HttpSessionListener to see when the session is being timed out.
For me it is working fine.
Santanu Mahato
Greenhorn

Joined: Jan 03, 2013
Posts: 3
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Demoproj</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>LoginController</display-name>
<servlet-name>LoginController</servlet-name>
<servlet-class>mypackage.LoginController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginController</servlet-name>
<url-pattern>/LoginController</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>AddUserController</display-name>
<servlet-name>AddUserController</servlet-name>
<servlet-class>mypackage.AddUserController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AddUserController</servlet-name>
<url-pattern>/AddUserController</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>TaggedController</display-name>
<servlet-name>TaggedController</servlet-name>
<servlet-class>mypackage.TaggedController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TaggedController</servlet-name>
<url-pattern>/TaggedController</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>UpdateUserController</display-name>
<servlet-name>UpdateUserController</servlet-name>
<servlet-class>mypackage.UpdateUserController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UpdateUserController</servlet-name>
<url-pattern>/UpdateUserController</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>SearchUpdateController</display-name>
<servlet-name>SearchUpdateController</servlet-name>
<servlet-class>mypackage.SearchUpdateController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SearchUpdateController</servlet-name>
<url-pattern>/SearchUpdateController</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>DeleteController</display-name>
<servlet-name>DeleteController</servlet-name>
<servlet-class>mypackage.DeleteController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DeleteController</servlet-name>
<url-pattern>/DeleteController</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>UntaggedController</display-name>
<servlet-name>UntaggedController</servlet-name>
<servlet-class>mypackage.UntaggedController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UntaggedController</servlet-name>
<url-pattern>/UntaggedController</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>UpdateRetailerController</display-name>
<servlet-name>UpdateRetailerController</servlet-name>
<servlet-class>mypackage.UpdateRetailerController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UpdateRetailerController</servlet-name>
<url-pattern>/UpdateRetailerController</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>LogoutController</display-name>
<servlet-name>LogoutController</servlet-name>
<servlet-class>mypackage.LogoutController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LogoutController</servlet-name>
<url-pattern>/LogoutController</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>FirstTaggedController</display-name>
<servlet-name>FirstTaggedController</servlet-name>
<servlet-class>mypackage.FirstTaggedController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FirstTaggedController</servlet-name>
<url-pattern>/FirstTaggedController</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>ViewUserController</display-name>
<servlet-name>ViewUserController</servlet-name>
<servlet-class>mypackage.ViewUserController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ViewUserController</servlet-name>
<url-pattern>/ViewUserController</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>UpdateInventoryController</display-name>
<servlet-name>UpdateInventoryController</servlet-name>
<servlet-class>mypackage.UpdateInventoryController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UpdateInventoryController</servlet-name>
<url-pattern>/UpdateInventoryController</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>SingleUserViewController</display-name>
<servlet-name>SingleUserViewController</servlet-name>
<servlet-class>mypackage.SingleUserViewController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SingleUserViewController</servlet-name>
<url-pattern>/SingleUserViewController</url-pattern>
</servlet-mapping>
<error-page>
<error-code>404</error-code>
<location>/errorPage.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/errorPage.jsp</location>
</error-page>
<servlet>
<description></description>
<display-name>SearchUpdateInventoryController</display-name>
<servlet-name>SearchUpdateInventoryController</servlet-name>
<servlet-class>mypackage.SearchUpdateInventoryController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SearchUpdateInventoryController</servlet-name>
<url-pattern>/SearchUpdateInventoryController</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>RetailerDeltailControler</display-name>
<servlet-name>RetailerDeltailControler</servlet-name>
<servlet-class>mypackage.RetailerDeltailControler</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RetailerDeltailControler</servlet-name>
<url-pattern>/RetailerDeltailControler</url-pattern>
</servlet-mapping>
</web-app>



the web.xml file that you have asked for.
please cheak if there is any problem or not.
Ayan mallick
Ranch Hand

Joined: Nov 17, 2012
Posts: 44
santanu , web.xml seems to be fine.no session time out mapping.Don't know it looks strange to me.It should maintain session.did you try with HttpSessionListener to check when it is being invalidated.
or else for both the jsp you are getting the session object right.You can get both session's id to see whether it matches or not.Then you can guess that same session or diff session's are being created.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: session handling
 
Similar Threads
pass session in other application
change password
Problem in SessionManagement in Struts
java.lang.NullPointerException
Getting data in Servlet and passing to jsp but could not display in jsp