Page encoding issues involving JSPs and Servlets

Source: Internet
Author: User

1. What is the difference between character encodings in two places in a JSP page?

<%@ page contenttype= "Text/html;charset=utf-8"%>
is the output encoding of the server-side Java program Runtime, which is the encoding used by the server to output HTML to the client.


Refers to what encoding the client browser displays the Web page and directs the browser to parse the incoming HTML stream on the server side. It also has a role in guiding the submission of the form to what encoding to use for incoming request.

Request.setcharacterencoding ("GBK")//the page parameter is GBK encoded after the incoming server side

Similarly:

2. There are three ways to encode the output content in a servlet using the response #3# type:

1, response.setcharacterencoding ("UTF-8")

2, Response.setcontenttype ("Text/html;charset=utf-8")

       相当于服务端解析:<%@   page   contentType="text/html;charset=UTF-8"   

3, Response.setlocale (New Java.util.Locale ("zh", "CN"))

  
Here's a look at the differences between the three methods:

The first method can only be used to set the encoding used in the out output stream, but he has the highest priority and can override the settings in the latter two methods;

The second method is to set the encoding of the characters in the out output stream, but also to set the browser to receive these characters in what encoding to decode, its priority is lower than the first method, but higher than the third method;

The third method can only be used to set the encoding of the characters in the out output stream, but it has the lowest priority, and after it has been coded in one of the first two methods, it is overwritten and does not work.

Page encoding issues involving JSPs and Servlets

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.