Processing of character sets in Servlets

Source: Internet
Author: User

Steps for the servlet to run

The servlet's supplemental functionality as a Web server needs to be managed by the servlet container at run time, with the following process:

    1. The browser establishes a connection to the container based on IP
    2. The browser packages the request data
    3. Container parsing request packets, encapsulating requests and response objects
    4. Container finds servlet creation object based on request resource path
    5. The container passes the request and response objects as arguments into the service method and invokes the
    6. Containers Package response data to the browser
    7. The browser takes out the results and generates the page

Solve the garbled problem of output content

Add the following code to the location of the first line in the service () method

Response.setcontenttype ("Text/html;charset=utf-8")

Where CharSet can use other character sets that support Chinese, such as GBK. setContentType () has two functions:

    1. Notifies the container, using the specified character set when invoking the Out.println method output
    2. Generates a message header (Content-type) that notifies the browser that the server returns the data type and character set

When you use this code to modify the default encoding, be sure to write before you call print, so the code is positioned as far as possible on the first line of the service method. A semicolon is used before CharSet, and if you write it wrong, the interface to save the file appears because the browser does not recognize the value of the message header, so the user can handle

1  PackageCom.yunhua.servlet;2 3 Importjava.io.IOException;4 ImportJava.io.PrintWriter;5 6 ImportJavax.servlet.http.HttpServlet;7 Importjavax.servlet.http.HttpServletRequest;8 ImportJavax.servlet.http.HttpServletResponse;9 Ten  Public classServlettestextendsHttpServlet { One      Public voidService (HttpServletRequest request, httpservletresponse response) { AResponse.setcontenttype ("Text/html;charset=utf-8");//put it on the first line -PrintWriter out =NULL; -         Try { theout =Response.getwriter (); -}Catch(IOException e) { -             //TODO auto-generated Catch block - e.printstacktrace (); +}finally { -Out.print ("Chinese"); + out.close (); A         } at     } -}

Why the form submitted in Chinese will appear garbled

When the form is submitted, the browser encodes the Chinese parameter values in the form, and the encoding used is the character set used to open the page, such as the UTF-8 character set used by the current page, and the data submitted by the form is encoded in a UTF-8 manner. The Web server by default, the submitted form data will use the Iso-8859-1 character set to decode, encoding and decoding inconsistent way to produce the form submitted when the Chinese garbled problem.

How to solve the problem of Chinese garbled characters when submitting forms

Step one, make sure the page on which the form is located is opened according to the specified character set

Using the META tag in an HTML page ensures that the browser decodes the page according to the specified character set and limits how the data is encoded when the form is submitted

1 <meta http-equiv= "Content-type" content= "Text/html;charset=utf-8" >


The server side needs to tell the browser how to decode before calling the GetParameter method to read the parameters, which can be done using the following code:

1 request.setcharacterencoding ("Utf-8");

Note that this method must be placed before all request.getparameter methods.

This method is valid only for post requests.

Solve the problem of Chinese garbled characters when get mode is submitted

Step one, make sure the page on which the form is located is opened according to the specified character set

Using the META tag in an HTML page ensures that the browser decodes the page according to the specified character set and limits how the data is encoded when the form is submitted

<meta http-equiv= "Content-type" content= "Text/html;charset=utf-8" >

Step two, complete the conversion of iso-8859-1 to UTF-8 format

1 String username = Request.getparameter ("username"); 2 New String (Username.getbytes ("iso-8859-1"), "UTF-8");

Since any data submitted by get is bound to be decoded on the server side in a iso-8859-1 manner, the server side can first get the byte array by iso-8859-1, transforming the array into UTF-8 corresponding string form.

1  PackageCom.yunhua.servlet;2 3 Importjava.io.IOException;4 ImportJava.io.PrintWriter;5 6 ImportJavax.servlet.http.HttpServlet;7 Importjavax.servlet.http.HttpServletRequest;8 ImportJavax.servlet.http.HttpServletResponse;9 Ten  Public classServlettestextendsHttpServlet { One      Public voidService (HttpServletRequest request, httpservletresponse response) { AResponse.setcontenttype ("Text/html;charset=utf-8");//put it on the first line -PrintWriter out =NULL; -         Try { theRequest.setcharacterencoding ("UTF-8");//put the data in front of intercept POST request -             //Replace the iso-8859-1 with the specified character set -String param =NewString (Request.getparameter (""). GetBytes ("Iso-8859-1"), "UTF-8"); -out =Response.getwriter (); +}Catch(IOException e) { -             //TODO auto-generated Catch block + e.printstacktrace (); A}finally { atOut.print ("Chinese"); - out.close (); -         } -     } -}

Processing of character sets in Servlets

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.