Friday, October 15, 2010

JSP Session Management-I


JSP Session Management in jsp


Introduction:
                        The HTTP (Hypertext Transfer Protocol) was designed for transferring files over the World Wide Web (WWW). HTTP uses simple communication model. A client who uses browser to send request for some files, the server responds the request. For example, the client request username and the server send response the username to client. After completing the response, the communication connection is complete. And then the client request another resource, the server has no way of distinguishing it from any other client. So HTTP is called Stateless protocol.
                       
Session Tracking:

1). Hidden Form Fields.

2). URL rewriting

3). Cookies

4). HTTP session API

Example:

Letus go for understanding the use of session.

Consider three files A.jsp and B.jsp and Result.jsp

A.jsp file have one textbox to get FirstName as input and OK button.

Screen Shot below

 
B.jsp file have one textbox to get Lastname as input and OK button.

Screen Shot below





















In Result.jsp, we get firstname from A.jsp and lastname from B.jsp and also display the firstname and lastname in Result.jsp..


Procedure:

Step1:

Create Project SessionPro.(Already we have seen how to create Project for web application.

Step 2:

Create Folder ,folder name is jsp to save jsp file

            Jsp---- for jsp files


Step 3:

            Creating A.jsp

Already I have explained how to create jsp file. So you follow that ways to create jsp file.

Include following coding into the A.jsp

< %@page contentType="text/html" pageEncoding="UTF-8"% >
< !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=UTF-8" >
< title > A JSP Page < /title >
< /head &g
< body gt
< h1 > A JSP Page < /h1 >
< form method="post" action="http://localhost:8084/SessionPro/Jsp/B.jsp" >
FirstName < input type="text" name="Fname"/ >
< input type="submit" value="OK"/ >
< /form >
< /body >
< /html >


Step 4:
            Creating B.jsp

< %@page contentType="text/html" pageEncoding="UTF-8"% >
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
http://www.w3.org/TR/html4/loose.dtd >

< html >
< head >
< title > B JSP Page < /title >
< /head &g
< body gt
< h1 > B JSP Page < /h1 >
< form method="post" action="http://localhost:8084/SessionPro/Jsp/Result.jsp" >
LastName < input type="text" name="Lname"/ >
< input type="submit" value="OK"/ >
< /form >
< /body >
< /html >


step 5:

            Creating Result.jsp

< %@page contentType="text/html" pageEncoding="UTF-8"% >
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
http://www.w3.org/TR/html4/loose.dtd >

< html >
< head >
< title > Result JSP Page < /title >
< /head >
< body >
< %
String firstname=request.getParameter("Fname");
String secondname=request.getParameter("Lname");
out.println(“Firstname ”+firstname);
out.println(“ < br >”);
out.println(“Lastname ”+secondname);
% >
< /body >
< /html >


Step 6:
                  Run the A.jsp



You give Firstname as your desired.

I will give Firstname as suresh

OK

The control go to B.jsp Because we specify the path in action.


















In B.jsp I give Kumar as Lastname. And Click OK.

The control goes to Result.jsp.

In Result.jsp,we use predefined objct request and their method getParameter().

Request.getParameter() – used to get Form Field value from one page to another page.


Explain:

            We can see null and Kumar.

            Lastname will be displayed only. Firstname is null.

Why?

We did not use any session management. Because

HTTP is a stateless Protocol. Only It maintains states between two page.

We want to maintain state many pages, we use session tracking.

 There are four type of session tracking.

1). Hidden Form Field

2). URL Rewriting

3). Cookies

4). HTTP Session API

Next post we will use Hidden Form Field to maintain states.



NOTE: We have not used any session management so far. Consider above example without session management.

No comments:

Post a Comment