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