Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Use of Cookie in Servlet 
 

he cookie class provides an easy way for servlet to read, create, and manipulate HTTP-style cookies, which allows servlets to store small amount of data.

 

Use of Cookie in Servlet

                         

This section illustrates you  how cookie is used in Servlet.

The cookie class provides an easy way  for servlet to read, create, and manipulate HTTP-style cookies, which allows  servlets to store small amount of data. Cookies are small bits of textual information that a Web server sends to a browser and that the browser returns unchanged when visiting the same Web site.

A servlet uses the getCookies() method of HTTPServletRequest to retrieve cookies as request. The addCookie() method of HTTPServletResponse sends a new cookie to the browser. You can set the age of cookie by setMaxAge() method.

Here is the code which  defines cookie and shows how to set the maximum age of cookie.

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class UseCookies extends HttpServlet { 
    public void doGet ( HttpServletRequest request,
    HttpServletResponse response )
throws ServletException, IOException {
  PrintWriter out;
  response.setContentType("text/html");
  out = response.getWriter();
  Cookie cookie = new Cookie("CName","Cookie Value");
  cookie.setMaxAge(100);
  response.addCookie(cookie);
  
  out.println("<HTML><HEAD><TITLE>");
  out.println(" Use of cookie in servlet");
  out.println("</TITLE></HEAD><BODY BGCOLOR='cyan'>");
  out.println(" <b>This is a Cookie example</b>");
  out.println("</BODY></HTML>");
  out.close();
    }
}

In the above example, a servlet class UseCookies defines the cookie class. Here  the age of cookie has been set as
setMaxAge(100). If its value is set to 0, the cookie will delete immediately. After the time provided been expired, cookie will automatically deleted.

Here is the output

Download Source Code

                         

» View all related tutorials
Related Tags: c http com ide session list stl server orm resources process form binding transactions https object io connection processes make

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.