R4R
Right Place For Right Person TM
 
R4R Right ArrowJavaRight ArrowJSP Example
Previous Home Next

Implementation of the Session in JSP


Introduction:

In this page of the JSP Tutorial we are going to shows you how to track the session between different JSP pages. and how to move user one page to an other page In any web application and it becomes necessary to track the user data and objects throughout the application. JSP is provide an implicit object "session", which can be use to save the data specific the particular to the user.


Descriptions:
 The function session.getAttribute("username") is used to access the user name saved in the session.

 When any user clicks on the submit button, savename.jsp page is called. The JSP savename.jsp page retrieves the user name from request attributes and saves into the user session using the function session.setAttribute("username",username); and next page is showsession.jsp which is show the user name which is input in the savename.jsp page.


SourceCode:

 

===================================savenameform.jsp=========================================

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Input Form</title>
</head>
<body>
<form method="post" action="savenametosesson.jsp">
<p><b>Enter Your Name: </b><input type="text" name="username"><br>
<input type="submit" value="Submit">

</form>

</body>
</html>

====================================savename.jsp===================================

<%@ page language="java" %>
<%
String username=request.getParameter("username");
if(username==null) username="";

session.setAttribute("username",username);
%>

<html>
<head>
<title>Name Saved</title>
</head>
<body>
<p><a href="showsessonvalue.jsp">Next Page to view the session value</a><p>

</body>
</html>

====================================showsession.jsp===================================

<%@ page language="java" %>
<%
String username=(String) session.getAttribute("username");
if(username==null) username="";
%>
<html>
<head>
<title>Show Saved Name</title>
</head>
<body>
<p>Welcome: <%=username%><p>

</body>
</html>




Output of SourceCode:
 

Implementation of the Session in JSP

Previous Home Next
R4R Right ArrowJavaRight ArrowJSP Example

Tutorials,Examples,Questions and Answer

R4R
R4R
R4R
R4R
R4R
R4R
R4R
R4R