servlet resolves garbled problem _java

Source: Internet
Author: User

For the servlet everyone should be very familiar with, and review it today, if there is no good or wrong place to hope that the vast number of netizens criticized. Today we'll just talk about get and post two w ways, There are many differences between them, so the way to solve the coding will not be the same, post garbled problem to solve a point, the following first simple HTTP protocol, so that I can better memory of the original rational things and their differences between the points.

Both the Get and post methods are based on the HTTP protocol, which is designed to provide a way to publish and receive HTML pages, where the client initiates the request and the server responds.

A complete request message includes a request line, a number of message headers, and the content of the request entity

The request line includes the request method (get or post), the resource path (the address to be accessed), the HTTP version number (http1.1)

Several headers (such as the kernel information containing the browser (user-agent), who opens its parent page (refer), etc.

There is a blank line in the request entity content and message header, separating the message header from the request entity content, with the entity content being submitted by the user

A complete response message includes a status line, one or more answer headers, a blank line, and a response entity

Status line: Includes HTTP protocol version number, status code and reason narration

Common status code: 200 Normal

404: Request resource does not exist

500: Server Internal Error

The difference between get and post can be described after a rough finish of HTTP.

Get Way:

The Get method is to place the submitted content behind the URL, the length is limited, both post and get are based on the HTTP protocol, and got is placed in the request line because its arguments are in the URL, and the post is placed in the content of the request entity, and the Post method is relatively secure. The browser does not keep cached information, and the Get mode wk The cache, can view the submitted content from the browser's history, and the Get method length is limited and the post is not.

It is precisely because of the location of the HTTP protocol that the GET and post methods submit data, the way they are encoded is different

Post mode garbled problem resolution:

In the Servlet's service method (Doget or Dopost method can also), set the request's encoding to UTF-8

  

Req.setcharacterencoding ("UTF-8");

In this way, the request coding is no problem, then the response encoding is also set to UTF-8;

Resp.setcharacterencoding ("UTF-8");

So, response's code is fine, but this is written in the future to the browser may be problematic, because the browser does not know what your coding format, so the browser is the default format to display, so the browser to display the encoding mode to be set to UTF-8, Statement as follows:

Resp.setcontexttype ("Text/html;charset=utf-8");

At the same time also make sure that your development tool coding and project coding consistent, otherwise there may be garbled problems, here are UTF-8 as an example

Get way garbled problem solved:

Get way In addition to the above operation also in Tomcat's Conf directory in the Server.xml configuration file add a word, find the following sentence,

   

 <connector port= "8080" protocol= "http/1.1" connectiontimeout= "20000"
        redirectport= "8443"/>

Modified to

 <connector port= "8080" protocol= "http/1.1" connectiontimeout= "20000"
        redirectport= "8443" Userbodyencodingforuri= "true"/>

This method is more flexible, so that the URL encoding format as the page encoding format, can also be set to a fixed format, such as

 <connector port= "8080" protocol= "http/1.1" connectiontimeout= "20000"
        redirectport= "8443" encodingforuri= "UTF-8"/>

So basically there is no problem, if there is a problem, you have to use string method, the page will be garbled to the format you want, but if the above way is all right, this way is not how to use.

In addition to the above operation, but also to the database encoding format to the same as the project, this is changed to UTF-8, because time is limited, so write more rough, there are a lot of things have been omitted, but the way to solve the servlet code should be enough.

When called, you can use JS, such as:

function Ceshi () {
	window.location.href = "Userservlet?uname=zhangsan&realname=" + encodeuricomponent ("Good for Everyone") ;
}

This will be good for everyone will no longer display Chinese characters to the address bar (this sentence binding to the submission button), you can submit to the corresponding servlet. This method of solving garbled is also applicable to JSP

By the way, I have to say something to do with the servlet, that is forward and redirect,forward can only be in the current project to jump, redirect can jump outside, of course, they have other differences, here I just say a little something I think is important, That is, regardless of which one is used, after writing forward and redirect, must add return, if not add, the following statement will be executed, plus the back of the statement will be an error, so that the same page to prevent multiple jumps. Do not like the Netizen mistakenly sprayed, thank you for your cooperation, thank you for helping me pick the wrong netizens, because so I will continue to progress, sincerely thank you

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.