Html in Tomcat does not display css, Chinese characters are garbled

Yesterday (1024 Programmer's Day), after debugging for a long time, it was very uncomfortable...

Cause:

I wrote a static html page and never started the server. I basically finished the server test and it crashed: the local opening was normal, but on Tomcat, the css was not displayed, and the Chinese font was garbled (u8, changed, big5).
Open locally:Insert picture description here

On Tomcat:Insert picture description here

try:

I asked a lot, but I got everything that should be coded.
html:
Insert picture description here
Filter:
Insert picture description here
Project Settings:
Insert picture description here
Html file:
Insert picture description here

solve:

Until a senior asked me to try to rebuild the project, it was solved somehow, so I wondered if it was a Tomcat cache problem. Because it was too late, I went to bed, and as a result...
Insert picture description here

Finally solved half:

I got up this morning and there was a problem again. Similarly, I checked some blogs and found a way to solve the garbled code . Open the catalina.bat in the bin directory of the Tomcat installation directory and modify (probably on line 211):

set "JAVA_OPTS=%JAVA_OPTS% %JSSE_OPTS%"

for

set "JAVA_OPTS=%JAVA_OPTS% %JSSE_OPTS% -Dfile.encoding=UTF8 -Dsun.jnu.encoding=UTF8"

What I get now:
Local:
Insert picture description here

Tomcat:
Insert picture description here
Yes, css still can't be loaded...
Then I searched a lot and found that the problem was with the filter. I turned the filter off and there was css. No matter, even if the simplest processing was done, there was no css.
Original code (no css):

		req.setCharacterEncoding("utf-8");
        rep.setContentType("text/html;charset=utf-8");
        rep.setCharacterEncoding("utf-8");
        filterChain.doFilter(req, rep);

After the change (still no):

		HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) rep;
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        String requestURI = request.getRequestURI();

        if (requestURI.contains("s/") ||
                requestURI.contains("/js/") ||
                requestURI.contains("/fonts/") ||
                requestURI.contains("/img/") ||
                requestURI.contains("/images/") ||
                requestURI.contains("/error/")) {
    
    
            filterChain.doFilter(request,response);
            return;
        }

There is css:

//@WebFilter("/*")

That's right, as long as you get rid of the filter, there will be css. I really don't know what other methods are available. Anyone who knows can tell me about it in the comment area.Insert picture description here

Guess you like

Origin blog.csdn.net/qq_19265749/article/details/109270704