This example for you to share the Java form submitted in Chinese garbled solution, for your reference, the specific content as follows
Home Index.xml
<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>
Servletencoding.java
Package cn.hncu.servlet_2;
Import java.io.IOException;
Import Javax.servlet.Servlet;
Import Javax.servlet.ServletConfig;
Import javax.servlet.ServletException;
Import Javax.servlet.ServletRequest;
Import Javax.servlet.ServletResponse;
public class Servletencoding implements Servlet {private String charset=null;
@Override public void Destroy () {} @Override public servletconfig getservletconfig () {return null;
@Override public String Getservletinfo () {return null; @Override public void init (ServletConfig config) throws servletexception {charset=config.getinitparameter ("Cha
R ");
System.out.println ("code:" +charset); * * * Resolve Chinese garbled * 1) Change the character encoding (Server.xml-connector properties) in the Tomcat platform * and Tomcat is the common platform for all projects, so don't, try not to change * 2) back to Iso8859-1 code
: Req.setcharacterencoding ("Utf-8") is set before the parameter is obtained by the garbled solution in string: * 3); * Note: This method must be submitted in post mode, otherwise No. * 4 before getting the parameter set: by setting the character parameter in the Web.xml to 3) (5) To work with the filter 3)--Later realize * * @Override public void ServiCE (servletrequest req, Servletresponse resp) throws Servletexception, IOException {////3) before getting parameters set//Req.setch
Aracterencoding ("Utf-8");
4 before getting the parameters set: by setting the character parameter in the Web.xml to Req.setcharacterencoding (CharSet);
Read information processing Chinese garbled String name=req.getparameter ("name");
String pwd=req.getparameter ("pwd");
System.out.println ("Name:" +name+ ", pwd:" +pwd);
System.out.println ("Code before-name:" +name+ ", pwd:" +pwd);
2) Anti-search Iso8859-1 code: Through string in the garbled solution//byte bs[]=name.getbytes ("Iso-8859-1");
Name=new String (BS, "Utf-8");
Byte bs2[]=pwd.getbytes ("Iso-8859-1");
Pwd=new String (BS2, "utf-8");
SYSTEM.OUT.PRINTLN ("Encoded after-name:" +name+ ", pwd:" +pwd);
Note: This way for a lot of parameters, it is not appropriate to use the Resp.setcontenttype ("Text/html;charset=utf-8");/Set protocol: IE no problem, but some browsers are not compatible//reply to the client String str= "
Configuration file Web.xml
<?xml version= "1.0" encoding= "UTF-8"?> <web-app version=
"3.0" xmlns= "http://java.sun.com/xml/ns/"
Java ee "
xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "
xsi:schemalocation=" http://java.sun.com/ Xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd ">
<display-name></ display-name>
<servlet>
<servlet-name>encode</servlet-name>
<servlet-class >cn.hncu.servlet_2.ServletEncoding</servlet-class>
<init-param>
<param-name> char</param-name>
<param-value>utf-8</param-value>
</init-param>
</ servlet>
<servlet-mapping>
<servlet-name>encode</servlet-name>
< url-pattern>/encode</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.