Friday, 30 October 2015

How to Handle Session in JSP (Java) with Example

                In this post, we will see how the session is managed / handled in Java-JSP webpage.
                Here I have given a simple example. I have created four webpages. First webpage (jspsession_firstpage.jsp) creates a HTML form and takes input from user. These values will be passed to second webpage (jspsession_secondpage.jsp). In second webpage, we will bind these values to a session. These bounded values will be accessed in third webpage (jspsession_thirdpage.jsp) and fourth webpage (jspsession_fourthwebpage.jsp). In fourth webpage, we will remove the binding of values to session by calling session.invalidate() method.
              To know more methods(functions) for session management, go through the link https://docs.oracle.com/cd/E19857-01/819-6518/gcxvp/index.html.
              Go through the following programs:




Program 1: (jspsession_firstpage.jsp)


<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>

<html>
<head>
</head>
<body>
<font color=blue>

<h1> Enter Details into the following Form: </h1>

<form action="jspsession_secondpage.jsp" method="post">
First Name <input type=text name=firstname /> <br />
Last Name <input type=text name=lastname /> <br />
Email Id <input type=text name=emailid /> <br  /><br />
<input type=submit value=Submit>

</font>
</form>
</body>
</html>



Output: 





 Program 2: (jspsession_secondpage.jsp)
 
<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>

<html>
<head>
</head>
<body>

<font color=blue>

<%
String firstname="";
String lastname="";
String emailid="";

firstname=request.getParameter("firstname");
lastname=request.getParameter("lastname");
emailid=request.getParameter("emailid");

out.println("<h1>"+"Values got from jspsession_firstpage.jsp"+"<br />"+" via HTML Form are:"+"</h1>"+"<br /><br />");
out.println("First Name="+firstname+"<br />");
out.println("Last Name="+lastname+"<br />");
out.println("Email Id="+emailid+"<br />");

session.setAttribute("firstname",firstname);
session.setAttribute("lastname",lastname);
session.setAttribute("emailid",emailid);

%>
<br />
<a href="jspsession_thirdpage.jsp">Click Here To Move To jspsession_thirdpage.jsp</a>

</font>
</body>
</html>



Output:



Program 3: (jspsession_thirdpage.jsp)
<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>

<html>
<head>
</head>
<body>

<font color=blue>

<%
String firstname=(String)session.getAttribute("firstname"); 
String lastname=(String)session.getAttribute("lastname");
String emailid=(String)session.getAttribute("emailid");

out.println("<h1>"+"Values got from jspsession_secondpage.jsp"+"<br />"+" via session are:"+"</h1>"+"<br /><br />");
out.println("First Name="+firstname+"<br />");
out.println("Last Name="+lastname+"<br />");
out.println("Email Id="+emailid+"<br />");
%>

<br />
<a href="jspsession_fourthpage.jsp">Click Here To Move To jspsession_fourthpage.jsp</a>

</font>
</body>
</html>


Output:





Program 4: (jspsession_fourthpage.jsp)

<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>

<html>
<head>
</head>
<body>

<font color=blue>

<%
String firstname=(String)session.getAttribute("firstname"); 
String lastname=(String)session.getAttribute("lastname");
String emailid=(String)session.getAttribute("emailid");

out.println("<h1>"+"Values got from jspsession_thirdpage.jsp"+"<br />"+" via session are:"+"</h1>"+"<br /><br />");
out.println("First Name="+firstname+"<br />");
out.println("Last Name="+lastname+"<br />");
out.println("Email Id="+emailid+"<br />");

//To invalidate the session
session.invalidate();

%>

</font>
</body>
</html>


Output: 




Check Other Posts on Web Designing

No comments:

Post a comment