JSP recodes data sent from the browser in two ways

  • 2020-06-19 11:31:30
  • OfStack

In the recent DRP project, when using JSP to operate Chinese, there are often some messy code problems. These problems caused Chinese can not be entered, or can not be displayed properly. This involves the set of character set, the encoding mode of character set.

In JSP/Servlet, there are mainly the following places to set the code, pageEncoding="GB18030",contentType="text/html; charset=GB18030", ES13en.setCharacterEncoding ("GB18030") and response.setCharacterEncoding ("GB18030"), of which the first two can only be used for JSP and the last two for JSP and Servlet.

Here, we'll only talk about the encoding method for recoding data sent from the browser. As you know, recoding data sent from the browser requires only one statement, which is quite simple.

Option 1 :(simple enough)
 
<span style="font-family:Microsoft YaHei; font-size:18px">request.setCharacterEncoding("GB18030");</span><span style="font-family:'Microsoft YaHei'; font-size:18px"></span> 

But here there was a problem, you need to set up level character page has a lot of, also, this way is also a lack of flexibility, for the maintenance of the limit in the future is very big, therefore, I optimization under the scheme for 1, joined the Filter interface, will set the character set of statements 1 java abstracts classes, the java class implements the Filter interface. Now let's look at code 1.

Scheme 2 :(Filter series 1 is adopted for processing character set)
 
<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; 

/** 
*  using Filter system 1 Processing character set  
* @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"); 
//  Set the character set  
request.setCharacterEncoding(encoding); 

//  Continue to perform  
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 enough; it needs to be configured in ES40en.xml.
 
<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, we have made a flexible setting for the encoding mode, which can be changed flexibly in the configuration file, making future maintenance much easier.

From this small case, we can see, there are a lot of code can be optimized, from simple code can realize functions gradually optimized unafraid to various modification, various maintenance better code, which is more of a consideration of code optimization, obviously, I'm good enough, still need more practice, more thinking.

Related articles: