'Web application on tomcat sending response as text/html instead of text/css as request is for css loading & page not loading properly
I have put static files outside of war file(Not in Apache2) and accessing by putting context entry in server.xml file & all in ubuntu environment(VM) Like below:
<Context path="/my_app" docBase="my_app.war">
<Resources className="org.apache.catalina.webresources.StandardRoot">
<PostResources className="org.apache.catalina.webresources.DirResourceSet"
base="/opt/tomcat/datacom/virtualwebserver/"
webAppMount="/">
</PostResources>
</Resources>
</Context>
Note:Tomcat web.xml has a mime-type entry for text/CSS already
My Web Application web.xml contains below entries:
<welcome-file-list>
<welcome-file>/jsp/login/welcome.jsp</welcome-file>
</welcome-file-list>
<mime-mapping>
<extension>js</extension>
<mime-type>application/javascript</mime-type>
</mime-mapping>
<mime-mapping>
<extension>css</extension>
<mime-type>text/css</mime-type>
</mime-mapping>
<mime-mapping>
<extension>jpg</extension>
<mime-type>image/jpeg</mime-type>
</mime-mapping>
<filter>
<filter-name>requestLog</filter-name>
<filter-class>com.dci.db4.filter.RequestLog</filter-class>
</filter>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
Solution 1:[1]
Upgradation of tomcat version may raise this type of problem. version 8.5.37 and 9.0.14 seems to serve CSS files embedded in web app as content-type : text/html; charset=UTF-8 instead of content-type : text/css; charset=UTF-8. This cause the browser not to interpret the CSS.
To solve this problem, try to relocate your .css files inside /css directory of web app and load them from there.
Hope this will work for you.
Solution 2:[2]
I had a very similar problem. We upgraded Tomcat version and after that content-type was equal to text/html;charset=UTF-8 for whatever resources I tried to retrieve from the server. Firstly I thought that there is some problem with DefaultServlet and MIME mapping configurations. But it turned out that starting from tomcat version 8.5.36
The default Servlet should not override a previously set content-type.
https://oraweb.slac.stanford.edu/docs/changelog.html
And there was the following filter in our application that was applied to all endpoints:
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
// Setting the character set for the request
request.setCharacterEncoding("UTF-8");
// Setting the character set for the response
response.setContentType("text/html; charset=UTF-8");
// pass the request on
chain.doFilter(request, response);
}
Therefore tomcat couldn't override content-type
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | SHR |
| Solution 2 | Roman Dyndyn |
