JSP introduces html through the include tag, resulting in garbled code

Posted by Hamish on Tue, 30 Nov 2021 21:06:01 +0100

Today, when using jsp's include tag to import html files, it was found that there was a garbled code problem. It was solved after Baidu. Write a small hydrological record

jsp file

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>Sign in</title>
  </head>
  <body>
 
<!--
    <%@include file="title.html"%>
-->
    Login succeeded
 
    Lao Wang
    123456
 
  </body>
</html>

 


html file

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>navigation bar</title>
<style>
*{
margin: 0px;
}
#title1{
width: 100%;
height: 50px;
background-color:coral;
}
.li1{
text-decoration: none;
display: inline;
margin-left: 70px;
margin-right: 70px;
font-weight: bold;
}
#ul1{
height: 50px;
padding-top: 16px;
text-align: center;
}
#search{
height: 25px;
position: absolute;
left: 20px;
top: 10px;
}
</style>
</head>
<body>
<div id="title1">
<ul id="ul1">
<li class="li1">start</li>
<li class="li1">option</li>
<li class="li1">edit</li>
<li class="li1">tool</li>
<li class="li1">sign out</li>
<input type="text" id="search">
</ul>
<div>

</div>
</div>
</body>
</html>

 


Both files are encoded in UTF-8, and there is no problem when they are displayed separately, but there is garbled code when the html page is introduced through the include tag of JSP

 

 

 

JSP is not garbled, and the introduced html is garbled

First look at the jsp translated java file

 

 

 

The part enclosed by the red line is the imported html file. It can be found that there is garbled code when translating into java files, but there is no garbled code in the jsp file enclosed by the green line. Both files have been set with UTF-8 coding. Why is there only one garbled code?

How are the two coding methods I set on two pages recognized and processed when translating jsp to java

In the jsp, I specified the contentType as text/html;charset=utf-8. Indicates that when the jsp translated servlet sends html code to the client, it tells the client that the format of the file is utf-8 encoded html file

ps: there is also a pageEncoding attribute in jsp. The differences between the two attributes are as follows:

pageEncoding is the encoding of the jsp file itself. It specifies the encoding used by the web container to read the jsp file when compiling the jsp into a java file.

The code set by charset of contentType refers to the content code when the server sends it to the client.

In html, I specify charset=utf8 to tell the client to use utf-8 encoding when displaying the html file

 

Is there something missing?

In addition, html specifies the code specified when translating from jsp to java file, and the attribute pageEncoding is specified in jsp (my compiler is set to UTF-8 by default). However, html lacks this configuration, so the jsp file containing html does not know how to deal with this html code when translating to java file, but the original code of jsp file can be translated normally, This leads to the final result that the jsp is normal and the html part is abnormal

solve

Add this line of code under the html tag of the html file

<%@page pageEncoding="UTF-8"%>

HTML itself does not recognize this instruction, so it does not affect the use of HTML files. Moreover, when the html is imported into jsp files and translated into java files, this code can make the HTML files be translated in utf-8 encoding, so there will be no previous errors

 

 

 

Another way is to uniformly configure the pageEncoding code in web.xml, and add the following configuration in the web app tag:

<jsp-config>
<jsp-property-group>
<description>html encoding</description>
<display-name>JSPConfiguration</display-name>
<url-pattern>*.html</url-pattern>
<el-ignored>true</el-ignored>
<page-encoding>UTF-8</page-encoding>
<scripting-invalid>false</scripting-invalid>
<include-prelude></include-prelude>
<include-coda></include-coda>
</jsp-property-group>
</jsp-config>