JSP form submit data garbled problem

Source: Internet
Author: User
JSP form submission data appears garbled

The question was really tangled for many days. It's too complicated. Said is related to the JSP file storage format, and decoding format, as well as the browser's decoding format.

1. The internet said a lot of methods, I think the change of server.xml file This method should be relatively simple, after the change is once and for all. So it will

<connector port= "protocol=" http/1.1 "

maxthreads= "minsparethreads=" maxsparethreads= "75"

Enablelookups= "false"

connectiontimeout= "20000"

Redirectport= "8443" uriencoding= "GBK"/>

Change into:

<connector port= "8080" maxhttpheadersize= "8192"

maxthreads= "minsparethreads=" maxsparethreads= "75"

Enablelookups= "false" redirectport= "8443" acceptcount= "100"

connectiontimeout= "20000" disableuploadtimeout= "true" uriencoding= "GBK" >

I've tried, no. At first I thought it was the wrong word of my own, and then I took this copy of the net over here. It's still not working. Later, Kanggan said, she did it, and it made perfect sense. I have no words, it is a probability event, restart tried many times or not. Also said that can add usebodyencodingforuri= "true", but still not. Now I don't know what the reason is. Why a lot of people say it's OK ...

2. There is no way to try other methods. In the Servlet class, the following processing is done on the submitted data.

Request.setcharacterencoding ("GBK");

Response.setcharacterencoding ("GBK");

This will correctly display the Chinese language. But this method is also troublesome, each servlet class should be written inside. But the strange thing is that when I use the new JSP page again, this method doesn't work. I even submitted the page to the Servelt class that was in the front line. But the first JSP page is OK. Look carefully to know that the two JSP pages submitted in a different way, the front is my own set up the Post method, and the second I did not set the way to submit, is the default get method.

Is it not just the difference between the address bar and the amount of data submitted when the post and get are submitted? Later search the difference between the two at the time of submission. To say that Tomcat is not the same as the processing of post-get-get, they are coded differently when they pass the parameter. Tomcat's internal encoding format is iso-8856-1,tomcat the default encoding of Get is iso-8859-1,get submit way no more encoding, so the receiving page passes the request object's GetParameter () The resulting string is converted to iso-8859-1, so the Chinese will appear garbled.  So using the above conversion method can not restore the Chinese. The post submission mode is encoded by the encoding format of the page setup, and there is no special word Fu error problem. I have tried to use the above method is OK. For a Get Commit method, the following processing is done before the receiving page takes care of the submitted data

String usename=request.getparameter ("UName");

byte bytes[]=usename.getbytes ("iso-8859-1");

String username=new string (bytes, "gb2312");

can also solve the garbled problem, but this method is even more trouble, for each submitted data to do so complex conversion. Many people on the internet say they can use the filter, do it by the method, but also on the post submission method, or not on get submit methods. And I don't know how this filter works, it's weird, I didn't call it.

So in general, post is much better than get submit, convenient to use, safe, and no data limit.

3. For this method, modify the preferences in the

Window--preferences--general--workspace--other--utf-8

JSP page right-click--preferences--javascript--enconding--utf-8

I tried, the JSP page submitted in Chinese, the servlet can indeed receive, also can display the information in the database. But there is a problem, is my eclipse in the editing area of all Chinese are garbled, even the console of the Chinese are garbled, including other Java projects are so, scared I reset back. Is this the problem with the encoding format that was saved by my previous file?

4. Understanding of several directives

<%@ page language= "java" pageencoding= "GBK"%>

This is the storage format for the file that represents the JSP.

<%@ page contenttype= "text/html; CHARSET=GBK "%>

This is the decoding format, so the two must be consistent, otherwise there will be garbled. If the second article does not, the default is to use the ISO8859-1 encoding format.

The difference of two instructions

<%@ page language= "java" contenttype= "text/html; CHARSET=GBK "

pageencoding= "GBK"%> is the encoding that is provided in the JSP when it is compiled into HTML, allowing Java to "read" the string in the expression

<meta http-equiv= "Content-type" content= "text/html; The role of CHARSET=GBK "> is to provide an encoding option for IE browsers, which is used to" display "the final data.

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.