Simply solve the problem of jsp Chinese garbled

Simply solve the problem of jsp Chinese garbled

Beginner JSP makes a simple response page The
specific code is as follows:

<form action="test.jsp">
    username : <input type="text" name="username" />
    <br />
    <input type="submit">
</form>
 Hello <%=request.getParameter("username")%>

Save it as a test.jsp file, start tomcat access, the following picture appears:

Insert picture description here
Type in the box: vae The following picture appears:

Insert picture description here
But "username" looks really uncomfortable, so I decided to change it to "username"

<form action="test.jsp">
    用户名 : <input type="text" name="username" />
    <br />
    <input type="submit">
</form>
 Hello <%=request.getParameter("username")%>

At this time the problem appeared:
Insert picture description here
a string of garbled characters appeared where the "user name" should appear:

The so-called garbled code in the response is the garbled code on the display page, because the page data is put into the response from the server and then sent to the browser. If the data in the response cannot be parsed normally, the garbled problem will occur.
Why is there no problem in English? Because in iso-8859-1, gb2312, utf-8 and any one of the encoding formats, the English encoding format is the same, each character occupies 8 digits, and Chinese is troublesome, and the next Chinese occupies 16 digits under gb2312 , Two bytes, and under utf-8, a Chinese takes up 24 digits, three bytes. When the browser does not know the encoding method, it will cut off these characters from the middle, and then they will be messed up when displayed. Therefore, to solve the garbled problem, we have to tell the browser what encoding method we use.

Under Windows, the default encoding format of the file is gb2312. The
solution is as follows:
(1) Add encoding information to the http response (response)

<%@ page contentType="text/html; charset=gb2312"%>

This paragraph should be placed on the first line of the jsp page to specify the response type and encoding format. The contentType is text/html, which means html content, and charset means that the encoding is gb2312. In this way, the browser can obtain the encoding format from the response.

(2) Specify the encoding format in html

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>title</title>
</head>

The meta part is used to specify the encoding format of the current html. Note that this paragraph should be placed in the head tag and placed at the top of the head tag. If it is not at the top, problems may occur under ie, especially if there is Chinese in the title. under.

The above two inspections are to ensure that the output of the jsp web page is Chinese. The
modified code is as follows:

<%@ page contentType="text/html; charset=gb2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
        <title>title</title>
    </head>
    <body>
        <form action="test.jsp">
            用户名 : <input type="text" name="username" />
            <br />
            <input type="submit">
        </form>
    </body>
</html>

Run again:

Insert picture description here

Guess you like

Origin blog.csdn.net/ws15168689087/article/details/109412317