2

I am setting up my application to test with latest apache-tomcat-8.5.39. When I am trying to load my application on any web browsers few of my resources are not loading properly.

Few resources are not loading properly because of the content-type returned by the web server.

These resources are local resources (taking from servers) : ex:

CSS:

Request URL: http://localhost:8080/workflow/css/wfstyle.css Content-Type: text/html;charset=UTF-8

SVG:

Request URL: http://localhost:8080/workflow/images/svg/Delete.svg Content-Type: text/html; charset=UTF-8

But all CDN are loading properly

Request URL: https://cdn.abc.ocm/assets/1.5.1/css/abc-design-system-ltr.css content-type: text/css

This is how I am adding SVG to JSP pages:

<span class="esg-icon__container">
   <img src="<%=request.getContextPath()%>/images/svg/Delete.svg"></img>
</span>

And CSS to JSP:

<%
    if(request.getLocale().getLanguage().contains("ar")){
%>

<link href="https://cdn.abc.ocm/assets/1.5.1/css/abc-design-system-rtl.css" rel="stylesheet"/>

<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/abc-design-system-rtl-custom.css" />

<% } else { %>

<link href="https://cdn.abc.ocm/assets/1.5.1/css/abc-design-system-ltr.css" rel="stylesheet"/>

<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/abc-design-system-ltr-custom.css" />

<%}%>

web.xml from tomcat:

<mime-mapping>
     <extension>svg</extension>
     <mime-type>image/svg+xml</mime-type>
</mime-mapping>

Expected content-type

For CSS: content-type: text/css

For SVG: content-type: image/svg+xml

SubhenduGN
  • 21
  • 2
  • 14
  • Possible duplicate of [How to set default mime-tipe for any file extension in Tomcat 6?](https://stackoverflow.com/questions/3282867/how-to-set-default-mime-tipe-for-any-file-extension-in-tomcat-6) – Selaron May 07 '19 at 08:32
  • Also, I want to know, why it's happening only with the latest build of tomcat and not with earlier versions? – SubhenduGN May 08 '19 at 07:20

1 Answers1

0

I did something like this:

public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain ) throws IOException, ServletException {
        String charEncoding = "UTF-8";
        String contentType = "image/svg+xml;";

        HttpServletRequest request = (HttpServletRequest) servletRequest;

        String file = request.getServletPath();

        if(file.contains(".svg")){
            if(!contentType.equals(servletResponse.getContentType())) {
                servletResponse.setContentType(contentType);
                servletResponse.setCharacterEncoding(charEncoding);
            }
            if(!charEncoding.equals(servletRequest.getCharacterEncoding())) {
                servletRequest.setCharacterEncoding(charEncoding);
            }
        }

        filterChain.doFilter(servletRequest, servletResponse);
    }

And it worked:

Content-type: image/svg+xml; UTF-8;

Is this a good practice? If no is there any other way of doing the same thing?

I am using Java1.8 and Strust1.2.

SubhenduGN
  • 21
  • 2
  • 14