In recent DRP projects, some garbled problems often occur when using JSP to operate Chinese. These problems cause Chinese language to be unable to be entered or displayed normally. This involves the setting of the character set and the encoding method of the character set.
There are mainly the following places that can be set in JSP/Servlets, pageEncoding="GB18030", contentType="text/html;charset=GB18030", ("GB18030") and ("GB18030"). The first two can only be used in JSP, and the last two can be used in JSP and Servlets.
Here, we will only talk about the encoding method when re-encoding the data sent by the browser. As we all know, to recode the data sent by the browser, it only takes one statement, which is very simple.
Plan 1: (It's very simple)
<span style="font-family:Microsoft YaHei; font-size:18px">("GB18030");</span><span style="font-family:'Microsoft YaHei'; font-size:18px"></span>
However, a problem arises here. There are many pages that need to be set at character level, and this method also lacks flexibility and has great limitations for future maintenance. Therefore, I optimized the solution a little, added the Filter interface, and abstracted a java class from the statements that set the character set, which implements the Filter interface. Let's take a look at the code below.
Solution 2: (Use Filter to uniformly process character sets)
<span style="font-family:Microsoft YaHei; font-size:18px">import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
/**
*Use Filter to uniformly process character sets
* @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 {
//("CharsetEncodingFilter--->>>>begin");
// Set character set
(encoding);
// Continue to execute
(request, response);
//("CharsetEncodingFilter--->>>>end");
}
public void init(FilterConfig filterConfig) throws ServletException {
= ("encoding");
//("---->>>encoding" + encoding);
}
}</span>
It's not possible with the Filter class, and it still needs to be configured in it.
<span style="font-family:Microsoft YaHei; font-size:18px"><filter>
<filter-name>CharsetEncodingFilter</filter-name>
<filter-class></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, a flexible setting of the encoding method is made, which can be flexibly changed in the configuration file, simplifying a lot for future maintenance.
From this small example, we can see that there are actually a lot of code that can be optimized, gradually optimized from simple code that can implement functions to better code that is not afraid of various modifications and maintenance. This is more about thinking about code optimization. Obviously, I am not qualified and need more practice and thinking.
There are mainly the following places that can be set in JSP/Servlets, pageEncoding="GB18030", contentType="text/html;charset=GB18030", ("GB18030") and ("GB18030"). The first two can only be used in JSP, and the last two can be used in JSP and Servlets.
Here, we will only talk about the encoding method when re-encoding the data sent by the browser. As we all know, to recode the data sent by the browser, it only takes one statement, which is very simple.
Plan 1: (It's very simple)
Copy the codeThe code is as follows:
<span style="font-family:Microsoft YaHei; font-size:18px">("GB18030");</span><span style="font-family:'Microsoft YaHei'; font-size:18px"></span>
However, a problem arises here. There are many pages that need to be set at character level, and this method also lacks flexibility and has great limitations for future maintenance. Therefore, I optimized the solution a little, added the Filter interface, and abstracted a java class from the statements that set the character set, which implements the Filter interface. Let's take a look at the code below.
Solution 2: (Use Filter to uniformly process character sets)
Copy the codeThe code is as follows:
<span style="font-family:Microsoft YaHei; font-size:18px">import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
/**
*Use Filter to uniformly process character sets
* @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 {
//("CharsetEncodingFilter--->>>>begin");
// Set character set
(encoding);
// Continue to execute
(request, response);
//("CharsetEncodingFilter--->>>>end");
}
public void init(FilterConfig filterConfig) throws ServletException {
= ("encoding");
//("---->>>encoding" + encoding);
}
}</span>
It's not possible with the Filter class, and it still needs to be configured in it.
Copy the codeThe code is as follows:
<span style="font-family:Microsoft YaHei; font-size:18px"><filter>
<filter-name>CharsetEncodingFilter</filter-name>
<filter-class></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, a flexible setting of the encoding method is made, which can be flexibly changed in the configuration file, simplifying a lot for future maintenance.
From this small example, we can see that there are actually a lot of code that can be optimized, gradually optimized from simple code that can implement functions to better code that is not afraid of various modifications and maintenance. This is more about thinking about code optimization. Obviously, I am not qualified and need more practice and thinking.