How to solve the problem of inconsistent resolution of Chinese parameters under Windows and Linux __linux

Source: Internet
Author: User

Recently I encountered a problem, that is, the normal Java site under Windows deployment to Linux run after the Chinese request parameter parsing garbled. The method is that I do a double encodeuricomponent () encoding of the Chinese parameters to be requested on the client, and the Request.getparameter () method is invoked directly in the background to get the parameters, which are handled in the same way for getting and post. Today, I accidentally tried the following method to find that this problem will not occur. The method is as follows:

First, to understand that the mechanism of a GET request and a POST request is different, the coding mechanism is not the same. The parameters submitted by get method must be encoded by the code function explicitly in the client, while the whole coding mode can be set when the parameters are submitted by post, and the parameters need not be coded separately. The code for the client JSP page is as follows:

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%> <!
DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" > Second, the server cannot generalize to get requests and post requests. When the client is set to UTF-8, the service-side post method does not need to be decoded. The code for the service-side servlet is as follows:

Package com.smapp.dataexchange.service;
Import java.io.IOException;
Import javax.servlet.ServletException;
Import Javax.servlet.annotation.WebServlet;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;

Import Javax.servlet.http.HttpServletResponse; /** * Servlet Implementation class Test */@WebServlet ("/test") public class Test extends HttpServlet {private static
       
    Final long serialversionuid = 1L;
        /** * @see httpservlet#httpservlet () * */Public Test () {super (); TODO auto-generated Constructor stub}/** * @see httpservlet#doget (httpservletrequest request, HTTPSERVLETRESP Onse response) */protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexceptio
		N, ioexception {String strcontent = request.getparameter ("content"); Strcontent = new String (strcontent.getbytes ("iso-8859-1"), "UTF-8"); <span style= "White-space:pre" > </span >//can see that only get requests are requiredCarry out transcoding response.setcontenttype ("Text/text");
		Response.setcharacterencoding ("UTF-8");
	Response.getwriter (). write (strcontent); /** * @see Httpservlet#dopost (httpservletrequest request, httpservletresponse response) * * protected void DoPost ( HttpServletRequest request, HttpServletResponse response) throws Servletexception, IOException {String strcontent = req
		Uest.getparameter ("content");
		Response.setcontenttype ("Text/text");
		Response.setcharacterencoding ("UTF-8");
	Response.getwriter (). write (strcontent);
 }

}
OK, when you run the JSP page, the test submits the Chinese parameter, whether through get or Post method can parse correctly.


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.