I am beginner to jsp & servlet. I am learning session handling in it.
I have done a simple program which have 3 jsp pages in which one jsp page have hyperlink to jsp page 2.
jsp page 2 checks that is there any existing session exists if yes then it dispatches control to jsp page 3 using dispatcher. But if the session object is null then it creats the new session and sets attributes to it and then dispatches control to jsp page 3 using dispatcher.
Following is code of all 3 jsp pages;
test1.jsp (Code for jsp page 1)
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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>Insert title here</title>
</head>
<body>
<a href="test2.jsp"> start here</a>
</body>
</html>
test2.jsp (Code for jsp page 2)
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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>Insert title here</title>
</head>
<body>
<%
HttpSession ses=request.getSession(false);
if(ses==null){
System.out.println("Session is null creating new session ");
%>
Session is null creating new session.
<%
//Usersession objusersession=new Usersession();
ses=request.getSession(false);
request.setAttribute("a", "This");
ses.setAttribute("name", "sandip");
System.out.println("Session created and attributes are set now dispatching");
%>
Session created and attributes are set now dispatching
<%
response.sendRedirect("test3.jsp");
//dispatch.forward(request, response);
}else{
System.out.println("Session is old then dispatching");
%>
Session is old then dispatching
<%
response.sendRedirect("test3.jsp");
//RequestDispatcher dispatch=request.getRequestDispatcher("test3.jsp");
//dispatch.forward(request, response);
}
%>
<a href="test.jsp"> Click here</a>
</body>
</html>
test3.jsp (Code for jsp page 3)
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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>Insert title here</title>
</head>
<body>
<%
HttpSession ses=request.getSession();
if(!session.isNew()){
//Usersession objusersession=new Usersession();
//ses.setAttribute("name", "sandip");
//request.getAttribute("a");
//System.out.println(request.getAttribute("a"));
System.out.println(ses.getAttribute("name"));
%>
printed the session attribute value on console.
<%
}else{
response.sendRedirect("test1.jsp");
}
%>
</body>
</html>
In above code when we directly call sequence is as follows
1)Invoke test1.jsp which have hyper link to test2.jsp
2)When we click hyperlink then it calls test2.jsp. In test2.jsp file3 it checks for preexisting session. If it finds it then it should directly call test3.jsp but if the preexixting session is not exists then it should create new session and set a attribute to it and call test3.jsp which prints this attributes value on console.
In my case when I call test1.jsp first time and click on hyper link it calls test2.jsp and founds that session is already exists and directly call test3.jsp. But in actual case the session is neither started on test1.jsp nor on test2.jsp unless it enters the if block in test2.jsp.
My query is then how the session is automatically created in my application?
I am dam sure either I am doing some wrong coding or I am understanding the concept wrongly.
I have also replaced the test2.jsp page with the servlet which does the same task as test2.jsp page dose but still I get the same result.
I want to ask experts please tell me what exactly wrong is going on.
Thank You!
Unless you use the directive
in a JSP, a session is created (unless it already exists, obviously) as soon as you hit this JSP.
I don’t know what you try to achieve, but 99% of the times, using the default (i.e. a session is created as soon as you hit a JSP) is an acceptable solution. Unless you have good reasons to not want a session to be created, you shouldn’t care.