Thursday, 24th June 2010
The new frontier for learning Java

Set a Cookie from a servlet

From WikiJava

Jump to: navigation, search


This example shows how to set a cookie in the response to a request from a client browser.

Contents

the article

This example uses the Cookie class and the HttpServletRequest to send a cookie.

the fundamental method used is:

HttpServletRequest.addCookie

MyServlet

import javax.servlet.*;
import javax.servlet.http.*;
 
public class MyServlet extends HttpServlet {
    public void doPost(HttpServletRequest req, HttpServletResponse res) {
	res.addCookie(new Cookie("mycookie_name", "mycookie_value"));
    }
 
    public void doGet(HttpServletRequest req, HttpServletResponse res) {
	doPost(req, res);
    }
 
}

See Also

Real's Java How To

Comments from the users

To be notified via mail on the updates of this discussion you can login and click on watch at the top of the page


Title (required):

Website:

Comment: