JSP- Session Management Using URL Rewriting
Introduction:
Someone disable cookies in their browser, when we can't use cookies to manage session in jsp. at that time, we use URLRewriting to manage session in jsp.
Struts 2.x
Flex Framework
JSP-Java Server Pages
Hibernate 3.2.5
Android
Friday, October 29, 2010
Wednesday, October 27, 2010
JSP Session Mgmt Using Servlet API
JSP- SERVLET API
Introduction:
We can also maintain session using Servlet API. All browser support servlet API for session management. HttpSession interface support session management. That mean, Servlet API provides HttpSession interface to implement session tracking. The HttpSession interface have many methods.
1). Get creating time: getCreationTime()- it returns the creation time of the session.the time is returned as millisecongs since midnight of January 1, 1970.
2). Get session ID: getId()- it returns session ID for each session.
3). Get last accessed time:getLastAccessedTime(): it return the client;s last request using current session ID.
4). Get values of the session: getValueNames(): it returns the array of all values that are bound into the current session.
5). Get specific value:getValue()- it returns the specific object that are bound to the current session.
Problem:
They are four files: 1) session1.jsp 2). session2.jsp 3). session3.jsp 4). display.jsp
Screen shot:
session1.jsp
Session2.jsp
Session3.jsp
Procedure:
Step 1:
Create Project (Already You have not Project).
Step 2:
Create Folder for storing jsp
Step 3:
Create jsp file session1.jsp
Include following coding into the session1.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 > JSP-Session API < /title >
< /head >
< body >
< form action="http://localhost:8084/SessionAPI/jsp/session2.jsp" method="post" >
FirstName:< input type="text" name="fname"/ > < br >
< input type="submit" value="OK"/ >
< /form >
< /body >
< /html >
Create another jsp file session2.jsp
Include following coding into the session2.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 > JSP-Session API < /title >
< /head >
< body >
< %
String fname=request.getParameter("fname");
% >
< %
HttpSession s=request.getSession();
s.setAttribute("f1",fname);
% >
< form action="http://localhost:8084/SessionAPI/jsp/session3.jsp" method="post" >
MiddleName > input type="text" name="mname"/ >
< input type="submit" value="OK"/ >
< /form >
< /body >
< /html >
Create another jsp file session3.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 > JSP-Session API < /title >
< /head >
< body >
< %
String mname=request.getParameter("mname");
% >
< %
HttpSession s=request.getSession();
s.setAttribute("f2",mname);
% >
< form action="http://localhost:8084/SessionAPI/jsp/display.jsp" method="post" >
LastName < input type="text" name="lname"/ >
< input type="submit" value="OK"/ >
< /form >
< /body >
< /html >
Create another jsp file display.jsp
Include following coding into the display.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 > JSP-Session API < /title >
< /head >
< body >
< %
String lname=request.getParameter("lname");
% >
< %
HttpSession s=request.getSession(true);
s.setAttribute("f3",lname);
out.println(s.getAttribute("f1"));
out.println(s.getAttribute("f2"));
out.println(s.getAttribute("f3"));
% >
< /body >
< /html >
Step 4:
Run the session1.jsp
Give input for Firstname as Suresh
OK control goes to session2.jsp
Tuesday, October 26, 2010
JSP Session Mgmt-Using Cookie
JSP-Session Management using Cookies
Introduction:
Cookies are small information that is send to the server from browser for later use. When we manage session in jsp, cookies are used to manage session. There are four ways to manage the session. In this lesson, we will learn how to create cookies and how to use cookies in jsp. Cookie is encapsulated in javax.servlet.http.Cookie.
Create cookies:
Cookie c=new Cookie(“CookieName”,”CookieValue”);
response.addCookie(c);
Retrieve cookies:
Cookie[ ] display=request.getCookies();
Kill cookies:
Problem:
There are three files. 1). cookie.jsp 2). cookie2.jsp 3). cookie3.jsp
cookie.jsp have one text box to get firstname and click OK the control goes to cookie2.jsp. cookie2.jsp have one text box to get lastname and click OK the control goes to cookie3.jsp. where we will display first name and last name .
Screen shot:
cookie3.jsp
Step 3:
3.1). Create jsp file named as cookie.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 > JSP-Cookie Example < /title >
< /head >
< body >
< b > Cookie Example < /b >
< br > < br >
< form action="http://localhost:8084/CookiesPro/jsp/cookie2.jsp" method="post" >
First Name: < input type="text" name="fname"/ > < br >
< input type="submit" value="OK"/ >
< /form >
< /body >
< /html >
3.2). Create another jsp file named as cookie2.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 > JSP-Cookie Example < /title >
< /head >
< body >
< b > Cookie Example < /b > < br >
< %
String fname=request.getParameter("fname");
%>
< %
Cookie c=new Cookie("firstname",fname);
response.addCookie(c);
% >
< form action="http://localhost:8084/CookiesPro/jsp/cookie3.jsp" method="post" >
Last Name < input type="text" name="lname"/ > < br >
< input type="submit" value="OK"/ >
< /form >
< /body >
< html >
3.3). Create another jsp file named as cookie3.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 > JSP-Cookie Example < /title >
< /head >
< body >
< b > Cookie Example < /b >
< %
String lname=request.getParameter("lname");
% >
< %
Cookie c1=new Cookie("lastname", lname);
response.addCookie(c1);
% >
< form action="http://localhost:8084/CookiesPro/jsp/display.jsp" method="post" >
< input type="submit" value="OK"/ >
< /form >
< /body >
< /html >
3.4). Create another jsp file named as display.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 > JSP-Display all Cookie < /title >
< /head >
< body >
< %
Cookie[] dis=request.getCookies();
for(int i=0;i
{
out.println(dis[i].getName()+":"+dis[i].getValue());
out.println("");
% >
< br >
< %
}
% >
< /body >
< /html >
Step 4:
Run the cookie.jsp
Suresh as Firstname
Kumar as Lastname
Step 5:
Get output
Structure of CookiePro
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:
< input type=”Hidden” name=”username” value=”sureshkumar”/ >
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
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. |
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. |
Thursday, October 14, 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
Subscribe to:
Posts (Atom)