Saturday, 28 July 2007

Working with cookies in Struts 2

I have been working with Struts for a very long time now, in fact, I still clearly remember using perform method in the action classes :^) . And then, a few days ago, I got an opportunity to try my hand at Struts 2, the reincarnation of Struts, and at a first glance, it looks impressive, to say the least.

Coming to the subject, when I googled for how to work with cookies in struts 2, I could only find a couple of references, viz. struts-users mail archive link and CookieInterceptor javadoc . So, I'll take this opportunity to share whatever little I have picked up.

Let's divide this into two parts -
- Receiving cookies in an action
- Adding cookies to a response in an action

First, let's see how to receive cookies in an action. I could figure out two options by which it can be achieved. Well, there could possibly be other options available as well, but I'm sure you'll excuse me, as this post is based on my extremely limited knowledge of struts 2 that I could manage to gather in past couple of days. Please feel free to enlighten me if you know more about it. Both options need the CookieInterceptor to be configured for the action that needs to receive cookies. Here are the two options -

Option 1 - Filter cookies in the CookieInterceptor configuration and add properties with the cookie names to the action class
Option 2 - Make the action implement CookiesAware interface and receive a map that contains cookies received

Let's say we have an action class Greeter that expects a cookie named flash in the incoming request. Here is the action configuration for the CookieInterceptor. The param tag under interceptor-ref tag indicates the cookie name that is expected by the action.

<action name="Greet" class="com.omkarpatil.Greeter">
<result>/greet.jsp</result>
<interceptor-ref name="cookie">
<param name="cookiesName">flash</param>
</interceptor-ref>
</action>

To Use the first option, a field named flash with getters/setters needs to be added to Greeter action. The CookieInterceptor does it's magic at runtime to make the cookie available into the action. Here is the code snippet for Greeter action -


public class Greeter extends ActionSupport {
private String message;
private String flash;

public String getFlash() {
return flash;
}

public void setFlash(String flash) {
this.flash = flash;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public String execute() throws Exception {
setMessage("Hello " + getFlash());
return SUCCESS;
}
}

To use the second option, the Greeter action needs to implement CookiesAware interface. CookiesAware interface defines one method void setCookiesMap(Map cookies), which the CookiesInterceptor uses to carry out interface injection into the action at runtime.


public class Greeter extends ActionSupport implements CookiesAware {
private Map cookiesMap;

public Map getCookiesMap() {
return cookiesMap;
}

public void setCookiesMap(Map cookiesMap) {
this.cookiesMap = cookiesMap;
}

public String execute() throws Exception {
// Do whatever you want to with the cookies map
return SUCCESS;
}
}

Well, that covers the "Receiving cookies into action" part. Onto the "Adding cookies to a response" in the next post. Stay tuned.



14 comments:

Shriniwas said...

very well written. Really good, useful information.
-Shriniwas

Ani said...

Cool Stuff..Keep posting..We are waiting n tuned ;)

Anonymous said...

Omkar, please do post about adding a cookie in the struts framework. I am trying to debug an application, and how this is happening is not clear. Your tutorial on how to read was very easy and helped me understand. So I am hoping for something which would shed some light on how to set.

Problem I am facing is that the application does not seem to be setting a cookies on Linux, whereas the same code sets the cookie on the Windows machine.

Jose Antonio said...

Very useful and concise. Anyone got the set part working?

Entertainment Emails said...

All the posts on the struts2 are very informative. Keep posting such a nice articles

Thanks,

Anonymous said...

Very helpful post.

sddssd said...

very Helpful to understand about cookies handling in struts 2. Which struts version you used...

Dinipc said...

The second option that you talk about does not work without the setter methods!

If I want to get hold of all cookies in the action class through the setter method - setCookiesMap() I get an empty map.

I need to configure in the struts.xml the cookies that will get added to the map as in option 1.

How do I get get hold of all cookies without having a name them and without having setters for each of them?

Cookie support in struts2 seems pretty poor in this respect.

Venkat said...

Nice Information. For more articles on Java J2EE Technologies refer

http://java-j2ee-technologies.blogspot.com

Anonymous said...

Its like you read my mind! You appear to know so much about this, like you wrote the book
in it or something. I think that you could do with some pics to drive the message home a little bit, but other than that, this is great blog.
A fantastic read. I'll certainly be back.

Here is my blog post: Muscle Rev Xtreme

Anonymous said...

I believe what you published was very logical. However, consider
this, what if you composed a catchier post
title? I mean, I don't wish to tell you how to run your website, but suppose you added a title to possibly grab a person's attention?
I mean "Working with cookies in Struts 2" is a little plain.
You ought to glance at Yahoo's home page and watch how they create article titles to get people to click. You might add a video or a picture or two to get readers interested about everything've got
to say. In my opinion, it would make your posts a little bit more
interesting.

Have a look at my homepage Male enhancement

Anonymous said...

Wow, superb weblog layout! How long have you been blogging for?
you made running a blog look easy. The full
look of your site is excellent, let alone the content!


Review my weblog ... best anti aging face cream

Anonymous said...

It's awesome to pay a visit this web page and reading the views of all mates about this piece of writing, while I am also eager of getting experience.

my homepage :: direct pay day loan lenders

Anonymous said...

It's an remarkable piece of writing in support of all the internet people; they will obtain advantage from it I am sure.

payday loan online