Server-side Get form data encoding and decoding problem (servlet)

Source: Internet
Author: User

The first thing to make clear is that the server here refers to Tomcat.

In the case where the page does not explicitly specify the encoding, the client passes two values param1 and param2 to the server via the input tag and string. If you use the Request.getparameter () method directly to get the value, the resulting is definitely garbled, we need to re-encode it, as shown in the following code:

New String (Req.getparameter ("param1"). GetBytes ("Iso-8859-1"), "GBK");

This is because, in the Chinese environment, the browser will be GBK to the data passed to the server encoding, and the server side will default to Iso-8859-1 decoding, if we do not do the corresponding processing, the resulting will be garbled.

It would be cumbersome to encode and decode every time the client's data is fetched, but fortunately there is a good workaround. We can write a filter that tells the server what the encoding of the request is before it reaches the servlet. A simple example is as follows:

 PackageCom.thief.filter;Importjava.io.IOException;ImportJavax.servlet.Filter;ImportJavax.servlet.FilterChain;ImportJavax.servlet.FilterConfig;Importjavax.servlet.ServletException;Importjavax.servlet.ServletRequest;ImportJavax.servlet.ServletResponse; Public classMyfilterImplementsFilter {@Override Public voidInit (filterconfig config)throwsservletexception {} @Override Public voidDoFilter (servletrequest request, servletresponse response, Filterchain arg2)throwsIOException, servletexception {request.setcharacterencoding ("GBK");    Arg2.dofilter (request, response); } @Override Public voiddestroy () {}}

At this time, the data can be obtained directly from the servlet via Request.getparameter (), but this is limited to the data passed by the input tag, and the query string is passed in accordance with the encoding and decoding process described above.

At this point, there are two solutions: one is to declare usebodyencodingforuri= "true" under Connector in Tomcat's server configuration file, and the second is to configure it under connector element Uriencoding to set the URL encoding format, if not set by default is Iso-8859-1.

The two scenarios I checked under the Startup.bat command-line window under Tomcat's bin are correct. But the Eclipse console does not see the effect, this does not know what the reason.

Reference: Http://www.tuicool.com/articles/IbaQza

Server-side Get form data encoding and decoding problem (servlet)

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.