Servlets solve garbled code problems

  • 2020-04-01 03:49:33
  • OfStack

We should be familiar with the servlet, review today, if there is a bad or wrong place to write hope that the majority of netizens criticism. Today, I will only discuss get and post. There are many differences between them, so the way to solve the coding will be different. The messy code problem of post will be easier to solve.

Both the get and post methods are based on the HTTP protocol, and their purpose is to provide a way to publish and receive HTML pages, which are requested by the client side and responded by the server side.

A complete request message consists of: a request line, several headers, and the request entity content

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

Several headers (such as user-agent with the browser's kernel information, who opens its parent page), and so on

There is a blank line between the request entity content and the header, separating the header from the request entity content, which is submitted by the user

A complete response message consists of a status line, one or more reply headers, a blank line, and a response entity

Status line: includes HTTP protocol version number, status code, and cause statement

Common status code: 200 normal

404: the requested resource does not exist

500: server internal error

After a cursory mention of HTTP, we can introduce the differences between get and post

The get method:

Get way is to put the content of the submitted in the back of the url, length is limited, the post and get is based on HTTP protocol, the get method is in the request line, because it is in the url, the parameters of the way a post is on the content of the request entity, relatively safe post way, browsers don't keep cache information, and the get method can wk retain a cache, from viewing the content of the submitted in the browser's history, and the get method length is limited, not in the post.

It is precisely because the get and post methods submit data in different locations in the HTTP protocol that they are encoded differently

Post garbled code problem solved:

In the servlet's service method (or doGet or doPost method), set the request encoding to utf-8


req.setCharacterEncoding("UTF-8");

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


resp.setCharacterEncoding("UTF-8");

In this way, there is no problem with the encoding of response, but there may be problems in the browser after it is written like this, because the browser does not know what your encoding format is, so it will be displayed in the default format of the browser, so the encoding mode of the browser should be set to utf-8, as follows:


resp.setContextType("text/html;charset=utf-8");

At the same time, make sure that the code of your development tool is the same as the code of your project, otherwise you may have the problem of garble code, as shown in utf-8

Get way to solve the problem of garbled code:

In addition to the above operation, add a sentence to the server.xml configuration file in tomcat's conf directory. Find the following sentence.


 <Connector port="8080" protocol="HTTP/1.1"
        connectionTimeout="20000"
        redirectPort="8443" />

Modified for


 <Connector port="8080" protocol="HTTP/1.1"
        connectionTimeout="20000"
        redirectPort="8443" userBodyEncodingForURI="true" />

This method is more flexible, so that the URL encoding format is the same 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 there's basically no problem, and if there's still a problem, you have to use the String method to change the garbled code of the page to the format you want, but if all the above methods are correct, this method is not very useful.

In addition to the above operations, we also need to set the encoding format of the database to the same as that of the project, which is changed to utf-8. Due to the limited time, the writing is rough, and a lot of things have been omitted, but it should be enough to solve the encoding method of the servlet.

Js can be used when calling, such as:


function ceshi() {
	window.location.href = "UserServlet?uname=zhangsan&realname=" + encodeURIComponent(" Hello, everyone ");
}

In this way, you will no longer display Chinese characters to the address bar (bind this sentence to the submit button), and you can submit to the corresponding servlet. This way of dealing with garbled code also applies to JSPS

By the way, have to say something related to the servlet, that is forward and redirect, jump forward can only be in the current project, redirect to jump to go outside, of course, they have other difference, here I only speak a little I think important things, that is, no matter use which kind of, after finished the forward and redirect, must be combined with the return, if not, will be performed at the back of the statements, plus the return, at the back of the statement will be an error, This prevents multiple hops to the same page. Do not like the net friend spurt by mistake, thank the cooperation, thank the net friend that helps me pick out the mistake, because of this I just can make progress ceaselessly, thank you sincerely


Related articles: