SlideShare uma empresa Scribd logo
1 de 17
Cookies




10/26/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
•   Interfaces implemented: java.lang.Cloneable




10/26/2012          R.V.S.Lalitha.,M.Tech(Ph.D)   2
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/26/2012        R.V.S.Lalitha.,M.Tech(Ph.D)   3
Class summary
public class Cookie implements java.lang.Cloneable {
//constructor
public Cookie(String name,String value)l
//instance methods
public 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/26/2012           R.V.S.Lalitha.,M.Tech(Ph.D)     4
Class summary
public 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/26/2012         R.V.S.Lalitha.,M.Tech(Ph.D)   5
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
• getPath
Returns path limitation for this servlet
• getSecure()
Returns true if the cookie requires a secure connection, false
   otherwise
   10/26/2012                R.V.S.Lalitha.,M.Tech(Ph.D)         6
• 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/26/2012                      R.V.S.Lalitha.,M.Tech(Ph.D)                7
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/26/2012                R.V.S.Lalitha.,M.Tech(Ph.D)   8
• cookie.java
import 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/26/2012                 R.V.S.Lalitha.,M.Tech(Ph.D)   9
• cookieview.java
import 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/26/2012                  R.V.S.Lalitha.,M.Tech(Ph.D)   10
• 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/26/2012                    R.V.S.Lalitha.,M.Tech(Ph.D)   11
Compiling servlets..
C:Program FilesApache Software FoundationTomcat
   5.5webappslalithaWEB-INFclasses>javac -classpath "C:Program
   FilesApache Software oundationTomcat 5.5commonlibservlet-
   api.jar" cookie.java

C:Program FilesApache Software FoundationTomcat
   5.5webappslalithaWEB-INFclasses>javac -classpath "C:Program
   FilesApache Software FoundationTomcat 5.5commonlibservlet-
   api.jar" cookieview.java




   10/26/2012               R.V.S.Lalitha.,M.Tech(Ph.D)         12
Running Html file

• C:Program FilesApache Software
  FoundationTomcat
  5.5webappslalithacookiesform.html




10/26/2012        R.V.S.Lalitha.,M.Tech(Ph.D)   13
10/26/2012   R.V.S.Lalitha.,M.Tech(Ph.D)   14
10/26/2012   R.V.S.Lalitha.,M.Tech(Ph.D)   15
10/26/2012   R.V.S.Lalitha.,M.Tech(Ph.D)   16
10/26/2012   R.V.S.Lalitha.,M.Tech(Ph.D)   17

Mais conteúdo relacionado

Semelhante a Cookies

Top5 scalabilityissues withappendix
Top5 scalabilityissues withappendixTop5 scalabilityissues withappendix
Top5 scalabilityissues withappendixColdFusionConference
 
J2EE : Java servlet and its types, environment
J2EE : Java servlet and its types, environmentJ2EE : Java servlet and its types, environment
J2EE : Java servlet and its types, environmentjoearunraja2
 
Strut2-Spring-Hibernate
Strut2-Spring-HibernateStrut2-Spring-Hibernate
Strut2-Spring-HibernateJay Shah
 
Server side programming bt0083
Server side programming bt0083Server side programming bt0083
Server side programming bt0083Divyam Pateriya
 
Cluster your application using CDI and JCache - Jonathan Gallimore
Cluster your application using CDI and JCache - Jonathan GallimoreCluster your application using CDI and JCache - Jonathan Gallimore
Cluster your application using CDI and JCache - Jonathan GallimoreJAXLondon_Conference
 
02 servlet-basics
02 servlet-basics02 servlet-basics
02 servlet-basicssnopteck
 
Unit Testing in SharePoint 2010
Unit Testing in SharePoint 2010Unit Testing in SharePoint 2010
Unit Testing in SharePoint 2010Chris Weldon
 
Level Up Your Integration Testing With Testcontainers
Level Up Your Integration Testing With TestcontainersLevel Up Your Integration Testing With Testcontainers
Level Up Your Integration Testing With TestcontainersVMware Tanzu
 
Storage Plug-ins
Storage Plug-ins Storage Plug-ins
Storage Plug-ins buildacloud
 
20151010 my sq-landjavav2a
20151010 my sq-landjavav2a20151010 my sq-landjavav2a
20151010 my sq-landjavav2aIvan Ma
 
Resthub framework presentation
Resthub framework presentationResthub framework presentation
Resthub framework presentationSébastien Deleuze
 
Mobicents Summit 2012 - George Vagenas - Testing SIP Applications with Arquil...
Mobicents Summit 2012 - George Vagenas - Testing SIP Applications with Arquil...Mobicents Summit 2012 - George Vagenas - Testing SIP Applications with Arquil...
Mobicents Summit 2012 - George Vagenas - Testing SIP Applications with Arquil...telestax
 
CloudStack Meetup Santa Clara
CloudStack Meetup Santa Clara CloudStack Meetup Santa Clara
CloudStack Meetup Santa Clara NetApp
 
PuppetConf 2016: The Long, Twisty Road to Automation: Implementing Puppet at ...
PuppetConf 2016: The Long, Twisty Road to Automation: Implementing Puppet at ...PuppetConf 2016: The Long, Twisty Road to Automation: Implementing Puppet at ...
PuppetConf 2016: The Long, Twisty Road to Automation: Implementing Puppet at ...Puppet
 
Java REST API Framework Comparison - UberConf 2021
Java REST API Framework Comparison - UberConf 2021Java REST API Framework Comparison - UberConf 2021
Java REST API Framework Comparison - UberConf 2021Matt Raible
 

Semelhante a Cookies (20)

Cookies
CookiesCookies
Cookies
 
Top5 scalabilityissues withappendix
Top5 scalabilityissues withappendixTop5 scalabilityissues withappendix
Top5 scalabilityissues withappendix
 
Top5 scalabilityissues
Top5 scalabilityissuesTop5 scalabilityissues
Top5 scalabilityissues
 
J2EE : Java servlet and its types, environment
J2EE : Java servlet and its types, environmentJ2EE : Java servlet and its types, environment
J2EE : Java servlet and its types, environment
 
Strut2-Spring-Hibernate
Strut2-Spring-HibernateStrut2-Spring-Hibernate
Strut2-Spring-Hibernate
 
Server side programming bt0083
Server side programming bt0083Server side programming bt0083
Server side programming bt0083
 
Cluster your application using CDI and JCache - Jonathan Gallimore
Cluster your application using CDI and JCache - Jonathan GallimoreCluster your application using CDI and JCache - Jonathan Gallimore
Cluster your application using CDI and JCache - Jonathan Gallimore
 
Servlets
ServletsServlets
Servlets
 
02 servlet-basics
02 servlet-basics02 servlet-basics
02 servlet-basics
 
Unit Testing in SharePoint 2010
Unit Testing in SharePoint 2010Unit Testing in SharePoint 2010
Unit Testing in SharePoint 2010
 
Hadoop Hive
Hadoop HiveHadoop Hive
Hadoop Hive
 
Level Up Your Integration Testing With Testcontainers
Level Up Your Integration Testing With TestcontainersLevel Up Your Integration Testing With Testcontainers
Level Up Your Integration Testing With Testcontainers
 
Storage Plug-ins
Storage Plug-ins Storage Plug-ins
Storage Plug-ins
 
20151010 my sq-landjavav2a
20151010 my sq-landjavav2a20151010 my sq-landjavav2a
20151010 my sq-landjavav2a
 
JDBC.ppt
JDBC.pptJDBC.ppt
JDBC.ppt
 
Resthub framework presentation
Resthub framework presentationResthub framework presentation
Resthub framework presentation
 
Mobicents Summit 2012 - George Vagenas - Testing SIP Applications with Arquil...
Mobicents Summit 2012 - George Vagenas - Testing SIP Applications with Arquil...Mobicents Summit 2012 - George Vagenas - Testing SIP Applications with Arquil...
Mobicents Summit 2012 - George Vagenas - Testing SIP Applications with Arquil...
 
CloudStack Meetup Santa Clara
CloudStack Meetup Santa Clara CloudStack Meetup Santa Clara
CloudStack Meetup Santa Clara
 
PuppetConf 2016: The Long, Twisty Road to Automation: Implementing Puppet at ...
PuppetConf 2016: The Long, Twisty Road to Automation: Implementing Puppet at ...PuppetConf 2016: The Long, Twisty Road to Automation: Implementing Puppet at ...
PuppetConf 2016: The Long, Twisty Road to Automation: Implementing Puppet at ...
 
Java REST API Framework Comparison - UberConf 2021
Java REST API Framework Comparison - UberConf 2021Java REST API Framework Comparison - UberConf 2021
Java REST API Framework Comparison - UberConf 2021
 

Mais de vamsi krishna

Mais de vamsi krishna (14)

Software project management
Software project managementSoftware project management
Software project management
 
Network programming
Network programmingNetwork programming
Network programming
 
Mobile computing
Mobile computingMobile computing
Mobile computing
 
Data warehousing and data mining
Data warehousing and data miningData warehousing and data mining
Data warehousing and data mining
 
Advanced computer architecture
Advanced computer architectureAdvanced computer architecture
Advanced computer architecture
 
Web technologies
Web technologiesWeb technologies
Web technologies
 
Xml
XmlXml
Xml
 
Servletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,postServletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,post
 
Javax.servlet,http packages
Javax.servlet,http packagesJavax.servlet,http packages
Javax.servlet,http packages
 
Servletand sessiontracking
Servletand sessiontrackingServletand sessiontracking
Servletand sessiontracking
 
Unit4wt
Unit4wtUnit4wt
Unit4wt
 
Unit3wt
Unit3wtUnit3wt
Unit3wt
 
Unit2wt
Unit2wtUnit2wt
Unit2wt
 
Unit 1wt
Unit 1wtUnit 1wt
Unit 1wt
 

Cookies

  • 1. Cookies 10/26/2012 R.V.S.Lalitha.,M.Tech(Ph.D) 1
  • 2. Cookie • Class Name: javax.servlet.http.Cookie • Super class: java.lang.Object • Immediate subclasses: None • Interfaces implemented: java.lang.Cloneable 10/26/2012 R.V.S.Lalitha.,M.Tech(Ph.D) 2
  • 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/26/2012 R.V.S.Lalitha.,M.Tech(Ph.D) 3
  • 4. Class summary public class Cookie implements java.lang.Cloneable { //constructor public Cookie(String name,String value)l //instance methods public 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/26/2012 R.V.S.Lalitha.,M.Tech(Ph.D) 4
  • 5. Class summary public 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/26/2012 R.V.S.Lalitha.,M.Tech(Ph.D) 5
  • 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 • getPath Returns path limitation for this servlet • getSecure() Returns true if the cookie requires a secure connection, false otherwise 10/26/2012 R.V.S.Lalitha.,M.Tech(Ph.D) 6
  • 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/26/2012 R.V.S.Lalitha.,M.Tech(Ph.D) 7
  • 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/26/2012 R.V.S.Lalitha.,M.Tech(Ph.D) 8
  • 9. • cookie.java import 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/26/2012 R.V.S.Lalitha.,M.Tech(Ph.D) 9
  • 10. • cookieview.java import 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/26/2012 R.V.S.Lalitha.,M.Tech(Ph.D) 10
  • 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/26/2012 R.V.S.Lalitha.,M.Tech(Ph.D) 11
  • 12. Compiling servlets.. C:Program FilesApache Software FoundationTomcat 5.5webappslalithaWEB-INFclasses>javac -classpath "C:Program FilesApache Software oundationTomcat 5.5commonlibservlet- api.jar" cookie.java C:Program FilesApache Software FoundationTomcat 5.5webappslalithaWEB-INFclasses>javac -classpath "C:Program FilesApache Software FoundationTomcat 5.5commonlibservlet- api.jar" cookieview.java 10/26/2012 R.V.S.Lalitha.,M.Tech(Ph.D) 12
  • 13. Running Html file • C:Program FilesApache Software FoundationTomcat 5.5webappslalithacookiesform.html 10/26/2012 R.V.S.Lalitha.,M.Tech(Ph.D) 13
  • 14. 10/26/2012 R.V.S.Lalitha.,M.Tech(Ph.D) 14
  • 15. 10/26/2012 R.V.S.Lalitha.,M.Tech(Ph.D) 15
  • 16. 10/26/2012 R.V.S.Lalitha.,M.Tech(Ph.D) 16
  • 17. 10/26/2012 R.V.S.Lalitha.,M.Tech(Ph.D) 17