java-181218155624
java-181218155624
TOPIC :JSP Session and Cookie Handling, JSP Session tracking, JSP database
access and JSP standard Tag libraries
1
Content:
• JSP Session and Cookie Handling
• JSP Session tracking
• JSP database access
• JSP standard Tag libraries
JSP Session and Cookie Handling
• Maintaining Session Between Web Client And Server
• Let us now discuss a few options to maintain the session between the
Web Client and the Web Server −
What is Cookie?
• Cookies are text files stored on the client computer and they are kept
for various information tracking purposes. JSP transparently supports
HTTP cookies using underlying servlet technology.
JSP Session and Cookie Handling
• There are three steps involved in identifying and returning users −
• Server script sends a set of cookies to the browser. For example, name,
age, or identification number, etc.
• Browser stores this information on the local machine for future use.
• When the next time the browser sends any request to the web server
then it sends those cookies information to the server and server uses
that information to identify the user or may be for some other purpose
as well.
JSP Session and Cookie Handling
• HTTP is a "stateless" protocol which means each time a client retrieves a
Webpage, the client opens a separate connection to the Web server and the
server automatically does not keep any record of previous client request.
• We can create cookies using Cookie class in the servlet API. We can add
cookies to response object using addCookie () method. We can access
cookies using getCookies () method. For Example: If we want to store
user’s name on our web site then we can use following simple code to store
that cookie.
String name=request.getParameter (“username”);
Cookie c=new Cookie (“yourname”, name);
response.addCookie(c);
JSP Session and Cookie Handling
• We can use HTTP cookies to perform session management. The web
container stores the session ID on the client machine. When the getSession
() method is called, the web container uses session ID cookie information to
find the session object.
• The cookie mechanism is the default HttpSession strategy. Some browsers
do not support cookies or some users turn off cookies on their browsers. If
that happens then cookie based session management fails.
• We can detect if cookies are being used for session management
using isRequestSessionIdFromCookie method in the HttpServletRequest
interface. It returns true if cookies are used.
• A similar method isRequestSessionIdFromURL method returns true if
URL-rewriting is used for session management.
JSP Session and Cookie Handling
Step 1: Creating a Cookie object
• You call the Cookie constructor with a cookie name and a cookie value, both of
which are strings.
• Cookie cookie = new Cookie("key","value");
Step 2: Setting the maximum age
• You use setMaxAge to specify how long (in seconds) the cookie should be valid.
The following code will set up a cookie for 24 hours.
• cookie.setMaxAge(60*60*24);
JSP Session and Cookie Handling
Step 3: Sending the Cookie into the HTTP response headers
• You use response.addCookie to add cookies in the HTTP response header as
follows
• response.addCookie(cookie);
JSP Session and Cookie Handling
Step 3: Sending the Cookie into the HTTP response headers
• You use response.addCookie to add cookies in the HTTP response header as
follows
• response.addCookie(cookie);
JSP Session tracking and database access
• JSP makes use of the servlet provided HttpSession Interface. This interface
provides a way to identify a user across.
• a one page request or
• visit to a website or
• store information about that user
• By default, JSPs have session tracking enabled and a new HttpSession object is
instantiated for each new client automatically. Disabling session tracking requires
explicitly turning it off by setting the page directive session attribute to false as
follows −
• <%@ page session = "false" %>
JSP Session tracking and database access
• The JSP engine exposes the HttpSession object to the JSP author through the
implicit session object. Since session object is already provided to the JSP
programmer, the programmer can immediately begin storing and retrieving data
from the object without any initialization or getSession().
JSP Session tracking and database access
• EXAMPLE:
public class SessionId extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException
{
res.setContentType("text/html");
PrintWriter pw = res.getWriter();
HttpSession ssn = req.getSession();
if(ssn != null){
pw.println("Your session Id is : ");
String ssnId = ssn.getId();
pw.println(ssnId);
}
else { pw.println("Your session is not created yet"); }
}}
JSP Session tracking and database access
Accessing a Database: Processing Steps
Internationa
Lization and tag library Message fmt
formatting
Number and date
formatting