Cookies10/31/2012   R.V.S.Lalitha.,M.Tech(Ph.D)   1
Cookie•   Class Name: javax.servlet.http.Cookie•   Super class: java.lang.Object•   Immediate subclasses: None•   Interfac...
Purpose•     The Cookie provides an easy way for servlets to  read, create and manipulate HTTP-style cookies,  which allow...
Class summarypublic class Cookie implements java.lang.Cloneable {//constructorpublic Cookie(String name,String value)l//in...
Class summarypublic void setComment(String purpose);public void setDomain(String pattern);public void setMaxAge(int Expiry...
Class summary• Cookie()Constructs a new cookie with a initial name and value• clone()To return a copy of object(duplicate ...
• getName()Returns name of the cookie• getValue()Returns value of the cookie in String format• getVersion()Returns version...
Sample servlet to implement cookie• cookiesform.html<html><head><title>Cookies</title></head><body><form action="http://lo...
• cookie.javaimport java.io.*;import javax.servlet.*;import javax.servlet.http.*;public class cookie extends HttpServlet {...
• cookieview.javaimport java.io.*;import javax.servlet.*;import javax.servlet.http.*;public class cookieview extends HttpS...
• web.xml<web<servlet>    <servlet-name>cookie</servlet-name>    <servlet-class>cookie</servlet-class>  </servlet>  <servl...
Compiling servlets..C:Program FilesApache Software FoundationTomcat   5.5webappslalithaWEB-INFclasses>javac -classpath "C:...
Running Html file• C:Program FilesApache Software  FoundationTomcat  5.5webappslalithacookiesform.html10/31/2012        R....
10/31/2012   R.V.S.Lalitha.,M.Tech(Ph.D)   14
10/31/2012   R.V.S.Lalitha.,M.Tech(Ph.D)   15
10/31/2012   R.V.S.Lalitha.,M.Tech(Ph.D)   16
10/31/2012   R.V.S.Lalitha.,M.Tech(Ph.D)   17
Upcoming SlideShare
Loading in …5
×

Cookies

262 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
262
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Cookies

  1. 1. Cookies10/31/2012 R.V.S.Lalitha.,M.Tech(Ph.D) 1
  2. 2. Cookie• Class Name: javax.servlet.http.Cookie• Super class: java.lang.Object• Immediate subclasses: None• Interfaces implemented: java.lang.Cloneable10/31/2012 R.V.S.Lalitha.,M.Tech(Ph.D) 2
  3. 3. Purpose• The Cookie provides an easy way for servlets to read, create and manipulate HTTP-style cookies, which allow servlets to store small amounts of data on the client.• Cookies are generally used for session tracking or storing small amounts of user-specific configuration information. 10/31/2012 R.V.S.Lalitha.,M.Tech(Ph.D) 3
  4. 4. Class summarypublic class Cookie implements java.lang.Cloneable {//constructorpublic Cookie(String name,String value)l//instance methodspublic Object Clone();public String getComment();public String getDomain();public String getMaxAge();public String getName();public String getPath();public String getSecure();public String getValue();public String getVersion(); 10/31/2012 R.V.S.Lalitha.,M.Tech(Ph.D) 4
  5. 5. Class summarypublic void setComment(String purpose);public void setDomain(String pattern);public void setMaxAge(int Expiry);public void setPath(String uri);public void setSecure(boolean flag);public void setDomain(String pattern);public void setValue(String newValue);public void setVersion(int v);}10/31/2012 R.V.S.Lalitha.,M.Tech(Ph.D) 5
  6. 6. Class summary• Cookie()Constructs a new cookie with a initial name and value• clone()To return a copy of object(duplicate cookie)• getComment()Returns comments associated with cookie• getDomain()Returns domain limitation associated with cookie• getMaxAge()Returns max age allowed for this cookie• getPathReturns path limitation for this servlet• getSecure()Returns true if the cookie requires a secure connection, false otherwise 10/31/2012 R.V.S.Lalitha.,M.Tech(Ph.D) 6
  7. 7. • getName()Returns name of the cookie• getValue()Returns value of the cookie in String format• getVersion()Returns version of the cookie• setComment()Sets the comment field of the cookie• setDomain()Specifies domain restriction pattern• setMaxAge()Specifies the maximum age of the cookie• setPath()Specifies path for cookie• setSecure()The secure flag indicates whether the cookie should be sent only over a secure channel, such as SSL• setValue()Assigns new value to a cookie• setVersion()Servlets can send and receive cookies formatted to match either Netscape persistent cookies or the newer 10/31/2012 R.V.S.Lalitha.,M.Tech(Ph.D) 7
  8. 8. Sample servlet to implement cookie• cookiesform.html<html><head><title>Cookies</title></head><body><form action="http://localhost:8080/lalitha/cookie">Enter name<input type="text" name="t1"/><br>Enter qualification<input type="text" name="t2"/><br><input type="submit" name="submit" value="Submit"/></form></body></html>10/31/2012 R.V.S.Lalitha.,M.Tech(Ph.D) 8
  9. 9. • cookie.javaimport java.io.*;import javax.servlet.*;import javax.servlet.http.*;public class cookie extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { res.setContentType("text/html"); PrintWriter out = res.getWriter();String name=req.getParameter("t1");String qual=req.getParameter("t2");Cookie c=new Cookie(name,qual);res.addCookie(c);out.println("cookies are added");}}10/31/2012 R.V.S.Lalitha.,M.Tech(Ph.D) 9
  10. 10. • cookieview.javaimport java.io.*;import javax.servlet.*;import javax.servlet.http.*;public class cookieview extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { res.setContentType("text/html"); PrintWriter out = res.getWriter();Cookie[] c=req.getCookies();for(int i=0;i<c.length;i++){out.println(c[i].getName());}}}10/31/2012 R.V.S.Lalitha.,M.Tech(Ph.D) 10
  11. 11. • web.xml<web<servlet> <servlet-name>cookie</servlet-name> <servlet-class>cookie</servlet-class> </servlet> <servlet-mapping> <servlet-name>cookie</servlet-name> <url-pattern>/cookie</url-pattern> </servlet-mapping><servlet><servlet-name>cookieview</servlet-name> <servlet-class>cookieview</servlet-class> </servlet> <servlet-mapping> <servlet-name>cookieview</servlet-name> <url-pattern>/cookieview</url-pattern> </servlet-mapping></web-app>10/31/2012 R.V.S.Lalitha.,M.Tech(Ph.D) 11
  12. 12. Compiling servlets..C:Program FilesApache Software FoundationTomcat 5.5webappslalithaWEB-INFclasses>javac -classpath "C:Program FilesApache Software oundationTomcat 5.5commonlibservlet- api.jar" cookie.javaC:Program FilesApache Software FoundationTomcat 5.5webappslalithaWEB-INFclasses>javac -classpath "C:Program FilesApache Software FoundationTomcat 5.5commonlibservlet- api.jar" cookieview.java 10/31/2012 R.V.S.Lalitha.,M.Tech(Ph.D) 12
  13. 13. Running Html file• C:Program FilesApache Software FoundationTomcat 5.5webappslalithacookiesform.html10/31/2012 R.V.S.Lalitha.,M.Tech(Ph.D) 13
  14. 14. 10/31/2012 R.V.S.Lalitha.,M.Tech(Ph.D) 14
  15. 15. 10/31/2012 R.V.S.Lalitha.,M.Tech(Ph.D) 15
  16. 16. 10/31/2012 R.V.S.Lalitha.,M.Tech(Ph.D) 16
  17. 17. 10/31/2012 R.V.S.Lalitha.,M.Tech(Ph.D) 17

×