Two ways of re encoding data sent by browser by JSP


In recent DRP projects, when using JSP to operate Chinese, there are often some garbled problems. These problems lead to the failure of Chinese input or normal display. Here is about the setting of character set and the encoding method of character set.

In JSP / servlet, you can set the encoding in the following places: pageencoding = GB18030, contenttype = text / HTML; charset = GB18030 request.setCharacterEncoding (GB 18030) and response.setCharacterEncoding The first two of them can only be used in JSP, and the second two can be used in JSP and servlet.

Here, we only talk about the encoding method when re encoding the data sent by the browser. As we all know, to re encode the data sent by the browser, only one statement is needed, which is very simple.

Plan 1: (very simple)

Copy codeThe code is 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>

However, there is a problem here. There are many pages that need to set character level. Moreover, this way is inflexible, which limits the maintenance in the future. Therefore, I optimized scheme 1 by adding filter interface, abstracting the statement of setting character set into a Java class, which implements the filter interface. Let’s take a look at the code.

Scheme 2: (using filter to process character set uniformly)

Copy codeThe code is as follows:
<span style=”font-family:Microsoft YaHei; font-size:18px”>import;

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 to deal with character set uniformly
* @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 {

//Set character set

//Go ahead
chain.doFilter(request, response);


public void init(FilterConfig filterConfig) throws ServletException {
this.encoding = filterConfig.getInitParameter(“encoding”);
//System.out.println(“System.out.println—->>>encoding” + encoding);

It’s not good to have the filter class. You need to web.xml Configuration in.

Copy codeThe code is as follows:
<span style=”font-family:Microsoft YaHei; font-size:18px”><filter>

Here, the coding mode is set flexibly, which can be changed flexibly in the configuration file, which simplifies the maintenance in the future.

From this small example, we can see that there are many codes that can be optimized, from simple codes that can realize functions to better codes that are not afraid of various modifications and maintenance. More of them are thinking about code optimization. Obviously, I’m not qualified enough, and I need more practice and thinking.