简体   繁体   中英

why <meta http-equiv=“content-type” content=“text/html; charset=UTF-8”> does not have any impact in Jsp file?

Ok, say, I got a .html file. If the file has <meta http-equiv="content-type" content="text/html; charset=UTF-8"> then if the file contain other languages then it will show correctly

   <html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <title></title>
  </head>

  <body>
    <h1>xxxx other language xxxx !</h1>

On the contrary, if the html file does not contain <meta http-equiv="content-type" content="text/html; charset=UTF-8"> then it won't show the Unicode correctly.

Ok, now, I got a jsp file. Even if I got <meta http-equiv="content-type" content="text/html; charset=UTF-8"> , it does not show unicode correctly. It only shows correctly If I have <%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>     
<html>
          <head>
            <title></title>
          </head>

          <body>
            <h1>xxxx other language xxxx !</h1>

Can anyone explain why?

When you visit a jsp page,the web server should translate the jsp to a Servlet at first. The translate program should read the jsp from disk, if you do not set <%@ page contentType="text/html;charset=UTF-8" language="java" %> it will read file in iso8859-1. So, an utf-8 character will be wrong in this step even it is not send to browser yet.

<%@ page contentType="text/html;charset=UTF-8" language="java" %> tells the translate program which charset use to read the jsp file and when the web server sends this jsp page data to browser will also use this charset. So use this grammar tell the web server which charset use for reading the file and sending the data to browser.

<meta http-equiv="content-type" content="text/html; charset=UTF-8"> is html grammar which tells the browser which charset to use to parse the data received from the web server. So use this grammar to tell browser how to parse data.

Then, set these two lines together, and ensure the two charsets are the same.

Or you can use just <%@ page contentType="text/html;charset=UTF-8" language="java" %> , you will find it also works because it will set http response header Content-Type:text/html;charset=utf-8 auto.

With <%@ page contentType="text/html;charset=UTF-8" language="java" %> servlet container generate response header:

Content-Type:text/html;charset=UTF-8

Without it is default:

Content-Type:text/html;charset=ISO-8859-1

Meta info doesn't override header.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM