Friday, October 15, 2010

JSP Session Mgmt Using Hidden Form in jsp


JSP Hidden Form Field

Introduction:
                        From this post, we will understand the session management using Hidden Form Field. HTML form support input elements with type of Hidden.

Example:

&lt input type=”Hidden” name=”username” value=”sureshkumar”/ &gt

From example, the hidden filed’s value such as sureshkumar is passed with HTTP request. From the next page, we get this value by using request.getParameter() method.

Problem:

A.jsp file have one textbox to get Firstname.

B.jsp file have one textbox to get Lastname.

Result.jsp file display or print Firstname and Lastname.

Solution:

            Already we have seen this program without session. So we only B.jsp form value. We get A.jsp value as null.

Procedure:

Step1:
            Create Project name SessionHidden(Already we have seen how to create project for web development)

Step 2:
Create Folder named jsp to save jsp files.



Step 3:
           
Create jsp file named A.jsp

A.jsp have one textbox and button.

Screenshot

Create another jsp file named B.jsp

Screenshot


Step 4:

            Include following coding into 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 >
< title > A JSP Page < /title >
< /head >
< body >
< form method="post" action=http://localhost:8084/SessionHidden/Jsp/B.jsp >
Firstname < input type="text" name="fname"/ >
< input type="submit" value="OK"/ >
< /form >
< /body >
< /html >

Step 5:

Include following coding into 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 >
< body >
< %
String fname=request.getParameter("fname");
% >
< form method="post" action=http://localhost:8084/SessionHidden/Jsp/Result.jsp >
Lastname < input type="text" name="lname"/ >
< input type="hidden" name="fname" value=" < %= fname % > " / >
< input type="submit" value="OK"/ >
< /form >
< /body >
< /html >

Step 6:

Include following coding into 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 lastname=request.getParameter("lname");
out.println("FirstName: "+firstname);
out.println("< br > ");
out.println("LastName: "+lastname);

% >
< /body >
< /html >


Step 7:

            Run the A.jsp

            Screenshot of A.jsp

 
Control goes to B.jsp

Screenshot of B.jsp


Control goes to Result.jsp

Screenshot of Result.jsp


Successfully we get Firstname from A.jsp and we get Lastname from B.jsp.



Drawback of Hidden Form Field:
Every page must include form.




No comments:

Post a Comment