Struts2 JSP form submit to action Chinese garbled problem

Source: Internet
Author: User

Struts2 JSP Tutorial Form submit to Action Chinese garbled problem
Struts2 in the default submission format is Utf-8 format, it is necessary to transfer the Chinese characters after the normal display, the solution:

Add the following code directly to the <struts> tag inside the Struts.xml file:
<constant name= "struts.i18n.encoding" value= "GBK"/>

Note: If you use the UTF-8 format in your JSP page, it is also written as a utf-8 format.

Look at the code below

Below is a submission page (submit.jsp) with the following code:

<TITLE>JSP's Chinese processing </title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">

<body>
<form name= "Form1" method= "Post" action= "process.jsp" >
<div align= "center" >
<input type= "text" name= "name" >
<input type= "Submit" name= "submit" value= "Submit" >
</div>
</form>
</body>


The following is the Process page (process.jsp) code:

<%@ page contenttype= "text/html; charset=gb2312 "%>
<TITLE>JSP's Chinese processing </title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">

<body>
<%=request.getparameter ("name")%>
</body>

If the submit.jsp submit English characters can be displayed correctly, if the submission of Chinese will appear garbled. Reason: Browsers use UTF-8 encoding to send requests by default, while Utf-8 and gb2312 encodings are different when they represent characters, which can result in unrecognized characters. Solution: Through the request.secharacterencoding ("gb2312") to the request for unified coding, the realization of the normal Chinese display. The modified process.jsp code is as follows:


<%@ page contenttype= "text/html; charset=gb2312 "%>
<%
Request.secharacterencoding ("gb2312");
%>
<TITLE>JSP's Chinese processing </title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">

<body>
<%=request.getparameter ("name")%>
</body>

Related Article

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.