Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8522209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:03:36+00:00 2026-06-11T07:03:36+00:00

I am beginner to jsp & servlet. I am learning session handling in it.

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T07:03:39+00:00Added an answer on June 11, 2026 at 7:03 am

    Unless you use the directive

    <%@ page session="false" %>
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Beginner to assembly programming for x86. I have a simple asm file which I
Beginner programmer here. So bear with me. I have a simple python program. print
I have written a servlet java program. During the execution of one particular loop
Beginner in Android development. My code crashes. I have made a simple Java method
Beginner level question Scenario: Have simple string cocantation tool, that I might expand later
Beginner here, I have a simple question. In Android what would be the best
I am beginner at using JSP and am following a tutorial. I have a
Beginner page building question: I've got a simple /div container defined via a CSS
Beginner rails/javascript question: Let's say that I have a simple Circle model in a
Beginner iOS dev here. I have a problem in my array. Im making an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.