During sshd integration, I simply wrote a program, but when I submitted the form data, there was a Chinese garbled problem. I have used several methods to solve the garbled problem, but it is useless. The so-called, fans of the Board, the audience. I asked my friends, oh, it turned out that when the form data was submitted, I wrote less than one parameter. This is the code that garbled during Chinese submission:
<Form action = "user. Do" method = "Post">
This is because the method = "Post" parameter is missing, and garbled characters are displayed when you enter Chinese data for submission. Method = "Post"
This indicates that the current data is transmitted using the POST method.
In order to keep yourself from making the same mistakes in the future, close up the log so that you can remember and summarize the cause of the errors. Then you will know how to handle the errors. For this reason
You can handle Chinese Garbled text as follows:
1. You can write a filter and configure it in Web. xml. The Code is as follows:
import java.io.IOException;import javax.servlet.Filter;import javax.servlet.FilterChain;import javax.servlet.FilterConfig;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;public class EncodingFilter implements Filter{private FilterConfig filterConfig;public void destroy(){}public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain)throws IOException,ServletException{String encoding=filterConfig.getInitParameter("encoding");request.setCharacterEncoding(encoding);response.setCharacterEncoding(encoding);response.setContentType("text/html,charset=UTF-8");chain.doFilter(request, response);}public void init(FilterConfig filterConfig)throws ServletException{this.filterConfig=filterConfig;}}
The configuration in Web. XML is as follows:
<filter><filter-name>encoding</filter-name><filter-class>com.handson.control.filter.EncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>UTF-8</param-value></init-param></filter><filter-mapping><filter-name>encoding</filter-name><url-pattern>/*</url-pattern></filter-mapping>
Write at the beginning of the configuration file.
2. You can also write such a class:
Import Java. io. unsupportedencodingexception; import javax. servlet. HTTP. httpservletrequest; import javax. servlet. HTTP. httpservletresponse; import Org. apache. struts. action. requestprocessor; public class newprocessor extends requestprocessor {protected Boolean processpreprocess (httpservletrequest arg0, httpservletresponse arg1) {try {retry ("GBK");} catch (unsupportedencodingexception e. printstacktrace ();} return Super. processpreprocess (arg0, arg1) ;}then configure in the struts-config.xml:
<controller><set-property property="processorClass"value="com.struts.processor.NewProcessor" /></controller>
Remember to write it before message-resources.
Of course, we can also use other methods to solve the problem of Chinese Garbled text. The above is only a solution to the problem of handling Chinese Garbled text during SSH integration or sshd integration.
But remember one thing, I remind you again: add method = "Post" to the form; otherwise, the garbled problem will always exist!