Recently done in the DRP project, the use of JSP to operate Chinese, often there will be some garbled problems. These problems result in Chinese being unable to enter or are not displayed properly. This involves the setting of the character set and the encoding of the character set.
In Jsp/servlet, there are several places where you can set up code, pageencoding= "GB18030", contenttype= "text/html;charset=gb18030", Request.setcharacterencoding ("GB18030") and Response.setcharacterencoding ("GB18030"), where the first two can only be used in the JSP, The latter two can be used in JSPs and in the servlet.
Here, we'll just talk about how to encode the data that the browser sends back. As we all know, to the browser sent to the data to be recoding, only need a statement on it, very simple.
Programme One: (very simple)
Copy Code code as follows:
<span style= "Font-family:microsoft Yahei; font-size:18px ">request.setcharacterencoding (" GB18030 "); </span><span style=" font-family: ' Microsoft Yahei '; font-size:18px "></span>
But there's a problem here, there are many pages that need to be set at the character level, and there is a lack of flexibility for future maintenance, so I have optimized the solution, added the filter interface, and abstracted a Java class for the statement that sets the character set. The Java class implements the filter interface. Let's take a look at the code below.
Scheme II: (using filter uniform processing character set)
Copy Code code as follows:
<span style= "Font-family:microsoft Yahei; font-size:18px ">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;
Import Org.omg.CORBA.Request;
/**
* Uniform processing character Set using filter
* @author Jerry
*
*/
public class Charsetencodingfilter implements Filter {
Private String encoding = NULL;
public void Destroy () {
}
public void Dofilter (ServletRequest request, servletresponse response,
Filterchain chain) throws IOException, Servletexception {
System.out.println ("Charsetencodingfilter--->>>>begin");
Setting the character set
request.setcharacterencoding (encoding);
Continue to execute
Chain.dofilter (request, response);
System.out.println ("Charsetencodingfilter--->>>>end");
}
public void init (Filterconfig filterconfig) throws Servletexception {
this.encoding = Filterconfig.getinitparameter ("encoding");
System.out.println ("System.out.println---->>>encoding" + encoding);
}
}</span>
The filter class is not available, and it needs to be configured in Web.xml.
Copy Code code as follows:
<span style= "Font-family:microsoft Yahei; font-size:18px "><filter>
<filter-name>CharsetEncodingFilter</filter-name>
<filter-class>com.bjpowernode.drp.util.filter.CharsetEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GB18030</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharsetEncodingFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping></span>
Here, there is a flexible way to set up the encoding, which can be flexibly changed in the configuration file, simplifying the maintenance of the future.
From this small example, we can see that there are actually a lot of code that can be optimized, from the ability to achieve simple code gradually optimized to be fearless of various modifications, maintenance of better code, which is more of a code optimization thinking, obviously, I am not enough, but also need more practice, more thinking.