Request and REPONSE requests garbled problem

Source: Internet
Author: User
(doget) Request garbled

Introduced:

The first line in the JSP sets the encoding format

The form is located on the page using the god horse format decoding,
            then the page uses the god Horse format encoding  

<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%>

Garbled Analysis:

The server default encoding format is set iso-8859-1 Dantin, if not set,
    the browser settings encoding format is used Utf-8,
and the server decoding is used iso-8859-1, will garbled will
    produce garbled

Solution:

1. Change the configuration information for the server

Path: E:\apache-tomcat-7.0.42\conf\server.xml

    <connector port= "8080"
               protocol= "http/1.1      
               " uriencoding= "UTF-8"   
               connectiontimeout= "20000"
               redirectport= "8443"/>
    plus uriencoding= "UTF-8"; ok

2. Using the inverse push method

(doPost) Request garbled

Post submission and get commit, difference:

The server decodes the time point differently, the others are the same
Get commit decoding point-in-time ====> request will decode the parameter as soon as it arrives at the server
Post commit decoding point in time =====> call to get parameter method is decoded

Solution:

Simply set the decoding table of the response before obtaining the parameter method

Case:

Request Get ()

protected void Doget (HttpServletRequest req, HttpServletResponse resp)
            throws Servletexception, IOException {
        System.out.println ("---------------get------------");

        String name = Req.getparameter ("name");


        will be garbled using Latin code iso-8859-1----------->byte
        byte[] bytes = name.getbytes ("iso-8859-1");
        Iso-8859-1-------------------->utf-8 Code table
        String name1 = new String (bytes, "UTF-8");

        System.out.println ("=========" + name1);

    }

Req Post ()

Req.setcharacterencoding ("UTF-8");
String name = Req.getparameter ("name");

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.