How Do I Send A Cookie in Servlet
How Do I Send A Cookie in Servlet
Bookstore
RSS
Follow us
Become a fan
J a v a E x a m p e ls
Home
You are here: Home
1. Java SE API
Servlet
2. Java EE API
3. Frameworks
4. Other Libraries
5. Design Patterns
6. IDE / Tools
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 { } }
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?
Leave a Reply
Name (Required) Mail (will not be published) (Required) Website
$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!
converted by Web2PDFConvert.com