java - How to add multiple "Set-Cookie" header in servlet response? -
as per rfc http://tools.ietf.org/html/rfc6265#page-7 allowed have 2 headers same key of "set-cookie". example provided in rfc -
set-cookie: sid=31d4d96e407aad42; path=/; secure; httponly set-cookie: lang=en-us; path=/; domain=example.com
how i achieve same jetty(or other servlet container)? when call httpservletresponse.addheader way-
httpservletresponse.addheader("set-cookie", "sid=31d4d96e407aad42; path=/; secure; httponly"); httpservletresponse.addheader("set-cookie", "lang=en-us; path=/; domain=example.com");
i see second addheader() doesn't add new header. according javadoc method-
adds response header given name , value. method allows response headers have multiple values.
so seems multiple values of allowed not sure how go having multiple "set-cookie" in servlet response.
setting cookies directly bit awkward, considering servlet api has methods working cookies.
anyway, tested on jetty 9.3.0.v20150612 , works expected.
example: setcookietest.java
package jetty; import static org.hamcrest.matchers.*; import static org.junit.assert.*; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.io.outputstream; import java.io.printwriter; import java.net.socket; import java.nio.charset.standardcharsets; import javax.servlet.servletexception; import javax.servlet.http.cookie; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import org.eclipse.jetty.server.server; import org.eclipse.jetty.servlet.servletcontexthandler; import org.junit.afterclass; import org.junit.beforeclass; import org.junit.test; public class setcookietest { @suppresswarnings("serial") public static class setcookieaddheaderservlet extends httpservlet { @override protected void doget(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception { resp.setcontenttype("text/plain"); resp.addheader("set-cookie","sid=31d4d96e407aad42; path=/; secure; httponly"); resp.addheader("set-cookie","lang=en-us; path=/; domain=example.com"); printwriter out = resp.getwriter(); out.println("hello from: " + this.getclass().getname()); } } @suppresswarnings("serial") public static class setcookieaddcookieservlet extends httpservlet { @override protected void doget(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception { resp.setcontenttype("text/plain"); // set-cookie: sid=31d4d96e407aad42; path=/; secure; httponly cookie sidcookie = new cookie("sid","31d4d96e407aad42"); sidcookie.setpath("/"); sidcookie.setsecure(true); sidcookie.sethttponly(true); resp.addcookie(sidcookie); // set-cookie: lang=en-us; path=/; domain=example.com cookie langcookie = new cookie("lang","en-us"); langcookie.setpath("/"); langcookie.setdomain("example.com"); resp.addcookie(langcookie); printwriter out = resp.getwriter(); out.println("hello from: " + this.getclass().getname()); } } private static server server; @beforeclass public static void startserver() throws exception { server = new server(9090); servletcontexthandler context = new servletcontexthandler(servletcontexthandler.sessions); context.addservlet(setcookieaddheaderservlet.class,"/test-add-header"); context.addservlet(setcookieaddcookieservlet.class,"/test-add-cookie"); server.sethandler(context); server.start(); } @afterclass public static void stopserver() throws exception { server.stop(); } /** * issue simple request, returning entire response (including payload) * * @param uri * uri request * @return response */ private string issuesimplehttpgetrequest(string path) throws ioexception { stringbuilder req = new stringbuilder(); req.append("get ").append(path).append(" http/1.1\r\n"); req.append("host: localhost\r\n"); req.append("connection: close\r\n"); req.append("\r\n"); // connect try (socket socket = new socket("localhost",9090)) { try (outputstream out = socket.getoutputstream()) { // issue request byte rawreq[] = req.tostring().getbytes(standardcharsets.utf_8); out.write(rawreq); out.flush(); // read response stringbuilder resp = new stringbuilder(); try (inputstream stream = socket.getinputstream(); inputstreamreader reader = new inputstreamreader(stream); bufferedreader buf = new bufferedreader(reader)) { string line; while ((line = buf.readline()) != null) { resp.append(line).append(system.lineseparator()); } } // return response return resp.tostring(); } } } @test public void testaddheader() throws exception { string response = issuesimplehttpgetrequest("/test-add-header"); system.out.println(response); assertthat("response", response, containsstring("set-cookie: sid=31d")); assertthat("response", response, containsstring("set-cookie: lang=en-us")); } @test public void testaddcookie() throws exception { string response = issuesimplehttpgetrequest("/test-add-cookie"); system.out.println(response); assertthat("response", response, containsstring("set-cookie: sid=31d")); assertthat("response", response, containsstring("set-cookie: lang=en-us")); } }
console output
2015-06-25 14:18:19.186:info::main: logging initialized @167ms 2015-06-25 14:18:19.241:info:oejs.server:main: jetty-9.3.0.v20150612 2015-06-25 14:18:19.276:info:oejsh.contexthandler:main: started o.e.j.s.servletcontexthandler@56cbfb61{/,null,available} 2015-06-25 14:18:19.288:info:oejs.serverconnector:main: started serverconnector@1ef05443{http/1.1,[http/1.1]}{0.0.0.0:9090} 2015-06-25 14:18:19.289:info:oejs.server:main: started @270ms http/1.1 200 ok date: thu, 25 jun 2015 21:18:19 gmt content-type: text/plain;charset=iso-8859-1 set-cookie: sid=31d4d96e407aad42;path=/;secure;httponly expires: thu, 01 jan 1970 00:00:00 gmt set-cookie: lang=en-us;path=/;domain=example.com connection: close server: jetty(9.3.0.v20150612) hello from: jetty.setcookietest$setcookieaddcookieservlet http/1.1 200 ok date: thu, 25 jun 2015 21:18:19 gmt content-type: text/plain;charset=iso-8859-1 set-cookie: sid=31d4d96e407aad42; path=/; secure; httponly set-cookie: lang=en-us; path=/; domain=example.com connection: close server: jetty(9.3.0.v20150612) hello from: jetty.setcookietest$setcookieaddheaderservlet 2015-06-25 14:18:19.405:info:oejs.serverconnector:main: stopped serverconnector@1ef05443{http/1.1,[http/1.1]}{0.0.0.0:9090} 2015-06-25 14:18:19.407:info:oejsh.contexthandler:main: stopped o.e.j.s.servletcontexthandler@56cbfb61{/,null,unavailable}
Comments
Post a Comment