Encoding the response as Gzip, causing the use of cookies in the browser

I am trying to reduce network traffic with Gzipping all responses from the backend, but I ran into a problem when the cookie is not set in the browser, if the I / O call is executed the same way, and I don’t know why.

This is the internal code in Java.

Cookie cookie = new Cookie("cookie_name", cookieToken); cookie.setPath("/"); cookie.setMaxAge(Integer.MAX_VALUE); response.addCookie(cookie); response.setContentType("application/json; charset=utf-8"); response.setStatus(HttpServletResponse.SC_OK); response.setHeader("Content-Encoding", "gzip"); String json=serviceOutput.toString(); byte[] gzip = Utils.gzip(json.getBytes("UTF-8")); response.setContentLength(gzip.length); response.getOutputStream().write(gzip); 

Any pointer to why this is not working? I am sure that I did the same thing earlier without any problems.

-

Gzip method added just in case it matches

 public static byte[] gzip(byte[] content) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); GZIPOutputStream g = new GZIPOutputStream(baos); if (content != null && content.length > 0) { g.write(content, 0, content.length); g.close(); } return baos.toByteArray(); } 
+7
java browser cookies gzip
source share
2 answers

I changed the code for the bullying data as

 package com.mytests.pack; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.time.LocalDateTime; import java.util.zip.GZIPOutputStream; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class HomeServlet extends HttpServlet{ private static final long serialVersionUID = -2638972063792556071L; private Cookie[] cookies; @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { cookies = request.getCookies(); System.out.println("----------------------======================-------------------"); if(null!=cookies){ for(Cookie c : cookies){ System.out.println(c.getValue()); } }else { System.out.println("First request"); } System.out.println("----------------------======================-------------------"); Cookie cookie = new Cookie("cookie_name", "cookiecontentishereofname"+LocalDateTime.now().toString()); cookie.setPath("/"); cookie.setMaxAge(Integer.MAX_VALUE); response.setContentType("application/json; charset=utf-8"); response.setStatus(HttpServletResponse.SC_OK); response.addCookie(cookie); response.setHeader("Content-Encoding", "gzip"); String json="{\"keyone\":\"Somestring is here in the point\"}"; ByteArrayOutputStream obj=new ByteArrayOutputStream(); GZIPOutputStream gzipstream = new GZIPOutputStream(obj); byte[] gzip = json.getBytes("UTF-8"); gzipstream.write(gzip); gzipstream.close(); response.setContentLength(obj.toByteArray().length); response.getOutputStream().write(obj.toByteArray()); } } 

with above code and jetty-distribution-9.4.0.v20161208

  • Verified Browsers

    - Google Chrome - Version 58.0.3029.110 (64-bit) RESULT :cookie was set

    screenshot

    - Opera - 45.0.2552.812 (PGO) RESULT :cookie was set

    screenshot

    - Mozilla Firefox - 53.0.3 (32-bit)

It is important to note that FIREFOX
When you open the developer tools it does not show the cookie set , but it works almost perfectly to check the screenshots firefox cookie view

Where

Jetty console output

The Jetty console exit indicates that the cookie was successfully set!

another configuration change I would like to show you

added POM dependency snippet as

  <dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>compile</scope> </dependency> </dependencies> 
+9
source share

Your code looks pretty good. I suggest you show the response text headers here using the curl -v http://your.host/under/test terminal command curl -v http://your.host/under/test . If your cookie appears there, then this is the browser.

+3
source share

All Articles