Question

I am new to Spring and I need to set a custom cookie when "Login" button is clicked, and then that cookie will be red inside the webapp. What is the best practice to do exactly that? Also, this brings question how to read it later on every page in webapp?

I thought I can set cookies with JavaScript, and read it later via custom filter (which would read the cookie from request, set it to the attribute and send it to controller.

Is this thought correct? Or should I set the cookie somewhere else (if so where and why?)

UPDATE 1:

What I want to achieve: I have dropdown box (which is language selector) on login page that has some values (language code, e.g. "en"), and selected value needs to be set as cookie (e.g. "lang") and that "lang" cookie will be red for i18n later on the pages. I have made i18n to work, but I need to read "lang" cookie to set the selected language.

UPDATE 2:

I have done what I wanted to do, but it is not exactly clean:

I am setting cookie via Javascript, or jQuery to be exact, and when user selects or changes the selection in <select/> then Javascript injects language value as cookie (e.g. en):

HTML:

<select name="language" id="selectLanguage" class="form-control">
   <option val="en">English</option>
</select>

JS:

var cookie = {

    set: function($this) {
        var now = new Date();
        var time = now.getTime();
        var expireTime = time + 1000*36000;
        now.setTime(expireTime);
        document.cookie = 'lang=' + $this.val() +';expires='+now.toGMTString()+';path=/';
    }

}

$('#selectLanguage').change(function(event) {
    cookie.set($(this));
});

Then I created new Filer that I called CookieFilter.java:

public class CookieFilter implements Filter {

    @Override
    public void doFilter(ServletRequest req, ServletResponse res,
            FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;

        Cookie[] cookies = ((HttpServletRequest) req).getCookies();
        if (cookies != null) {
            for (Cookie ck : cookies) {
                if(ck.getName().toString().equals("lang")){
                    req.setAttribute("languageCookie", ck.getValue());
                } else {
                    req.setAttribute("languageCookie", "en");
                };
            }
            chain.doFilter(req, res);
        }
    }

    public void init(FilterConfig config) throws ServletException {
        // TODO Auto-generated method stub
    }
    public void destroy() {
        // TODO Auto-generated method stub
    }
}

Added new filter in web.xml:

<filter>
    <filter-name>CookieFilter</filter-name>
    <filter-class>
        package.path.to.CookieFilter
    </filter-class>
</filter>
<filter-mapping>
    <filter-name>CookieFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

And to finish everything, I got the request from CookieFilter in my controller and red the attribute I sent:

String cookie = request.getAttribute("languageCookie").toString();
model.addAttribute("languageCookie",cookie);

Now I can read attribute from model and set it inside .JSP or do everything I want with it.

That is my solution, but there must be other way ... :)

No correct solution

OTHER TIPS

So, is it a requirement to use a cookie or is that just the approach that you are taking? You can very cleanly handle i18n in Spring MVC using Interceptors. I cover that here. Basically you need to register an interceptor and have that determine your browser requests.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top