Home | | Web Technology | Cookies

Chapter: Web Technology : Host Objects

Cookies

We've already used Cookies indirectly through Sessions. While session data is stored on the server side and only an index to the server-side data (the session ID) is transmitted to the client, Cookies can also be used directly to store data on the client side.

COOKIES

 

We've already used Cookies indirectly through Sessions. While session data is stored on the server side and only an index to the server-side data (the session ID) is transmitted to the client, Cookies can also be used directly to store data on the client side.

 

The Servlet API provides the class javax.servlet.http.Cookie for a convenient object-oriented representation of Cookies so you don't need to compose and decompose Cookie and Set-Cookie HTTP headers yourself.

 

Example. Imagine an authentication Servlet which receives a username and password from a login form via doPost and verifies them against a central authentication database. It then computes an authentiation string (e.g. a Base64-encoded "user:password" combination, as used by HTTP Basic Authentication). This string is now put into a Cookie and sent back to the client:

 

 

Cookie authCookie = new Cookie("xyz-Auth", credentials); authCookie.setVersion(1); authCookie.setDomain(".xyz.com"); res.addCookie(authCookie);

 

 

Request and Response Objects

throws ServletException, IOException {

 

response.setContentType("text/html"); PrintWriter out = response.getWriter();

 

//       Print the HTML header out.println("<HTML><HEAD><TITLE>") ; out.println("Request info"); out.println("</TITLE></HEAD>");

 

//       Print the HTML body out.println("<BODY><H1>Request info</H1><PRE>");

 

out.println("getCharacterEncoding: " + request.getCharacterEncoding());

 

out.println("getContentLength: " + request.getContentLength()); out.println("getContentType: " + request.getContentType()); out.println("getProtocol: " + request.getProtocol()); out.println("getRemoteAddr: " + request.getRemoteAddr()); out.println("getRemoteHost: " + request.getRemoteHost());

 

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Web Technology : Host Objects : Cookies |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.