Friday, 14th October 2011
Follow WikiJava on twitter now. @Wikijava

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


Comments on wikijava are disabled now, cause excessive spam.