0% found this document useful (0 votes)
114 views2 pages

How Do I Send A Cookie in Servlet

The document discusses how to send a cookie in a servlet by creating a new Cookie object, setting its name and value, and adding it to the HTTP response. It provides an example Java code to demonstrate this by creating a cookie named 'username' and adding it to the response.

Uploaded by

kerbel
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
114 views2 pages

How Do I Send A Cookie in Servlet

The document discusses how to send a cookie in a servlet by creating a new Cookie object, setting its name and value, and adding it to the HTTP response. It provides an example Java code to demonstrate this by creating a cookie named 'username' and adding it to the response.

Uploaded by

kerbel
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

About

Bookstore

RSS

Email

Follow us

Become a fan

J a v a E x a m p e ls
Home
You are here: Home

Learn Java Programming by Examples

1. Java SE API
Servlet

2. Java EE API

3. Frameworks

4. Other Libraries

5. Design Patterns

6. IDE / Tools

Search this site...


January 27, 2014 2:56 pm

How do I send a cookie in Servlet?

How do I send a cookie in Servlet?


Posted by Wayan Saryada on January 31, 2007

A cookie is a piece of information sent to a browser by a Web Server. The browser then returns that information to the Web server. This is how some Web pages remember your previous visits; for example, an E-Commerce site might use a cookie to remember which items youve placed in your online shopping cart. Cookies can also store user preference information, log-in data, etc. Here is an example to send a cookie in the http response object to the client browser.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 package org.kodejava.example.servlet; import import import import import import java.io.IOException; javax.servlet.ServletException; javax.servlet.http.Cookie; javax.servlet.http.HttpServlet; javax.servlet.http.HttpServletRequest; javax.servlet.http.HttpServletResponse;

Find us on Facebook

Learn by Examples
Like 130 people like Learn by Examples.

public class WriteCookieExample extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); // // Send a cookie named username to the client. There are some others // properties that we can set before we send the cookie, such as comments, // domain name, max age, path, a secure flag, etc. // Cookie cookie = new Cookie("username", "jduke"); response.addCookie(cookie); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }

F acebook social plugin

Related Posts
How do I set the maximum age of a cookie? How do I delete a cookie in Servlet? How do I read cookie in Servlet? How do I get client IP and hostname in Servlet? How do I read servlet init parameter? How do I check if parameter is exists in servlet request? How do I get parameter names from servlet request?

Filed in: Servlet

About Wayan Saryada


I am a programmer, a runner, an open water diver and currently living in the island of Bali, Indonesia. View all posts by Wayan Saryada

Leave a Reply
Name (Required) Mail (will not be published) (Required) Website

Work from Home


converted by Web2PDFConvert.com

Work from Home


Recent Projects Beb based NBT editor java, php, software architecture

$30 - 250
Simple game with ranking for Android android, j2ee, java, mobile phone

View

$30 - 250
Submit Comment

View

Facebook software to send private message java, mysql, php, software architecture, softwa...

250 - 750
Find more projects

View

Post a Project
Powered by

Advertise Here

Pages

Recent Posts

Recent Comments

Java Resources

About Bookstore

How do I create a simple mail client program in Swing? What are the system properties used for sending email? How do I send an HTML email? How do I read last n characters from a file? Java SE Installation Tutorial

Semih eker on How do I include a page fragment into JSP? Pierre on How do I send email using Gmail via TLS? Preethi on How do I move focus from JTextArea using TAB key? Amit on How do I get operating system temporary directory / folder? Srin on How to monitor file or directory changes?

Java Language Specification Java SE Technical Documentation Java Tutorials Java EE Technical Documentation Java EE 7 Tutorials

Affiliate Links

Shop Tech Titles on oreilly.com 50% off hosting plans from GoDaddy!

2014 Java Examples. All rights reserved.

converted by Web2PDFConvert.com

You might also like