https://bz.apache.org/bugzilla/show_bug.cgi?id=62253

            Bug ID: 62253
           Summary: response.setContentType() changes the content type
                    after response.getWriter() is called
           Product: Tomcat 9
           Version: 9.0.6
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: minor
          Priority: P2
         Component: Servlet
          Assignee: dev@tomcat.apache.org
          Reporter: stiemann...@gmail.com
  Target Milestone: -----

The Servlet 3.1 and 4.0 Specifications state (in section 5.5/5.6
Internationalization):

The setCharacterEncoding, setContentType, and setLocale methods can be called
repeatedly to change the character encoding. Calls made after the servlet
response’s getWriter method has been called or after the response is committed
have no effect on the character encoding.

Steps to Reproduce:

The bug can be reproduced by following the steps below or following the
README.md in this reproducer project:
https://github.com/stiemannkj1/set-content-type-after-get-writer.

1. Create a web application project with the following servlet and web.xml:


SetContentTypeAfterGetWriterServletImpl.java:


```
public class SetContentTypeAfterGetWriterServletImpl extends HttpServlet {

        private static final long serialVersionUID = 374928004261838614L;

        @Override
        protected void doGet(HttpServletRequest request,
                HttpServletResponse response)
                throws ServletException, IOException {

                response.setContentType("text/plain");
                PrintWriter writer = response.getWriter();
                response.setContentType("text/html");
                writer.println("<html>");
                writer.println("<body>");
                writer.print("<h1>");
                writer.print(response.getContentType());
                writer.println("</h1>");
                writer.println("</body>");
                writer.println("</html>");
        }
}
```

web.xml:

```
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; 
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd";>
        <servlet>
               
<servlet-name>SetContentTypeAfterGetWriterServlet</servlet-name>
               
<servlet-class>set.content.type.after.get.writer.SetContentTypeAfterGetWriterServletImpl</servlet-class>
        </servlet>
        <servlet-mapping>
               
<servlet-name>SetContentTypeAfterGetWriterServlet</servlet-name>
                <url-pattern>/*</url-pattern>
        </servlet-mapping>
</web-app>
```

2. Build, deploy, and navigate to the project.

If the bug still exists, "text/html" (along with a charset) will appear in the
browser.

If the bug is fixed, the following text will appear in the browser:

```
<html>
<body>
<h1>text/plain;charset=ISO-8859-1</h1>
</body>
</html>
```

-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to