comments (not for humans)
You may sometimes need to set or retrieve cookies on axis connections to transfer authentication tokens or similar.

Setting a cookie on an axis client request
To set a cookie on an axis client, you can add a handler. Below is source code for a handler, that fetches the currently incoming HttpServletRequest (say to a web site), extracts a cookie and puts the cookie on the axis request (to the web service).
public class CookieHandler extends BasicHandler {
public void invoke(MessageContext context) throws AxisFault {
HttpServletRequest request = someMethodReturningCurrentRequest();
Cookie cookies[] = request.getCookies();
Cookie cookie = null;
for (int i = 0; i < cookies.length; i++) {
String name = cookies[i].getName();
if (name.equals("cookiename")) {
cookie = cookies[i];
if (cookie != null) {
cookie.getName() + "=" + cookie.getValue());

To make the axis client use this cookie, we have to add it (or create) a client-config.wsdd. This file should reside in the classes directory in you war-archive.
<?xml version="1.0" encoding="UTF-8"?>
<deployment xmlns="" xmlns:java="">
<handler type=""/>
<transport name="http" pivot="java:org.apache.axis.transport.http.HTTPSender"/>

To make axis actually use cookies, MaintainSession has to be set to true (I discovered this reading the source code of HttpHandler - search for "don't forget the cookies....mmm..cookies" :-) ):
((MyServiceStub) myService).setMaintainSession(true);
Retrieving a cookie on a web service call
The following code retrieves a cookie from an incoming call to web service (SOAP/HTTP):
MessageContext context = MessageContext.getCurrentContext();
if (context != null) {
HttpServletRequest req = (HttpServletRequest) context
if (req != null) {
Cookie[] cookies = req.getCookies();
if (cookies != null) {
for (int i = 0; i < cookies.length; i++) {
if (cookies[i].getName().equals("cookiename")) {
//do something with cookie

Joe Solinsky

Thank you for writing this

I had to address this exact challenge. Thank you for breaking new ground. Ik zou zonder u scheel zijn!(Dutch)
Lao Kaa

Nice web site

Very many thanks for a good work. Nice and useful. Like it!
Lapsang Klu

Thank you very much for such a good work

Very many thanks for a good work. Nice and useful. Like it!

I don't understand

I don't understand how in method:
i can get HttpServletRequest?
Class CookieHandler is not connected with any Servlet or Filter - how to pass request?

Re: I don't understand

How to get hold of the current HttpServletRequest depends on the framework. Usually there is a context object with a static getter which also contains a reference to the HttpServletRequest.
For WebWork this line should be:
HttpServletRequest request = ServletActionContext.getRequest()

Setting a cookie in HTTP response

I observer the following behaviour. If I enabled cookies in Axis (setMaintainSession set to true) I was still unable to set my own cookie using MessageContext.setProperty(HTTPConstants.HEADER_COOKIE,"name=value") - it was not passed to the underlying HttpServletResponse.

However, it worked when I accessed the HttpServletResponse directly (via MessageContext.getProperty(HTTPConstants.MC_HTTP_SERVLETRESPONSE)) and used addCookie() directly.

When I set session maintenance to false I was unable to use cookies again.

This looks a bit weird to me but as the client worked I leave this issue. However, such a response(with multiple Set-Cookie lines) may be mistreated by some less robust HTTP clients.

know how

Hi Marchant,
I am also facing the same problem. I am not able to set cookies in axis2. could you please elaborate the solution? What should i do after calling getProperty(HTTPConstants.MC_HTTP_SERVLETRESPONSE) ? How to add cookie to it ?
Cedric Boudet
Easy and efficient ...

Thanks mate
HttpServletRequest) context
.getProperty(HTTPConstants.MC_HTTP_SERVLETREQUEST) always returns me null.pls help me
this info might help some people who cannot retrieve the cookie from the web service response:
Comments closed for this post