Garbled problem encountered by Web project

Source: Internet
Author: User

Problem code: JSP:
<%@ 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" >User name:<input type= "text" name= "username" ><br/>Password:<input type= "password" name= "pwd" ><br/>Gender:<input type= "Radio" name= "Sex" value= "male"/>male<input type= "Radio" name= "Sex" value= "female"/> Female <br/>Hobbies:<input type= "checkbox" name= "hobby" value= "Sleep" >Sleep<input type= "checkbox" name= "Hobby" value= "Game" >Game<input type= "checkbox" name= "hobby" value= "Travel" > Travel <br/>Your country:<select name= "sel" > <option>-----Please select-----</option> <option valu E= "CN" > China </option> <option value= "FA" > France </option> <option VA     Lue= "en" > UK </option> </select> <input type= "Submit" value= "Registration" > </form></body>Servlet:
@WebServlet ("/requestdemo5") Public classRequestDemo5extendsHttpServlet {Private Static Final LongSerialversionuid = 1L;      PublicRequestDemo5 () {Super();     }    //handling the Get mode garbled problem://Get method passed parameter content default encoding method Q Iso-8859-1//1 Self-coding and decoding process//2 Add uriencoding= "UTF-8" to the Tomcat configuration file     Publicstring Dispose (String str) {Try {
Codingbyte[] arr = str.getbytes ("Iso-8859-1"); Decode String ssstring=NewString (arr, "UTF-8"); returnssstring; } Catch(unsupportedencodingexception e) {e.printstacktrace (); } return NULL; } protected voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {System.out.println ("Before decoding:" +request.getparameter ("username") + "" "+request.getparameter (" Sex ")); String name= Dispose (Request.getparameter ("username")); String Sex= Dispose (Request.getparameter ("Sex"))); System.out.println ("After decoding:" +name+ "Sex:" +sex); } protected voidDoPost (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {doget (request, response); }

Operation Result:

Problem Analysis:

Problem solving:
Tell the browser to use the Iso-8859-1 encoding method, you can detect the Get method re-encoding and decoding method can solve the garbled problem

<meta http-equiv= "Content-type" content= "text/html; charset= "Iso-8859-1" >

Summary of issues:
<!--contenttype= "text/html; Charset=utf-8:          JSP saves the file in the editor, using the utf-8 encoding format        problem: Calling the servlet file through the browser, the Chinese garbled --><!--pageencoding = "UTF-8:          jsp saves the file in the editor, using the utf-8 encoding format         problem: Call through the browser jsp,jsp the Chinese garbled --><%@ page language=" Java " Contenttype= "text/html; Charset=utf-8 "    pageencoding=" UTF-8 "%>
<!-- through <meta> Tag simulation response header, control browser with ISO-8859-1 encoding parsing--><meta http-equiv= "Content-type" content= " text/html; Charset=utf-8 ">


Handling the Get mode garbled problem:
The Get method passes the parameter content default encoding method iso-8859-1

1 self-coding and decoding process
2 Add uriencoding= "UTF-8" to the Tomcat configuration file

Handle the Post method to solve the garbled problem:

Sends the data in the response object to the browser in UTF-8 decoded bytes
Request.setcharacterencoding ("Utf-8");

UTF-8 International Code GBK Chinese Code (including GB2312, that is, through the GB2312 encoding can be decoded by GBK, and vice versa)

Tomcat default encoding is Iso-8859-1

GetBytes () encoded by the platform default character set

The Get method passes the parameter content default encoding method iso-8859-1

?? Indicates no encoding

Reference Blog: http://www.cnblogs.com/oldinaction/p/5167481.html

  

Garbled problem encountered by Web project

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.