5. Using a filter
In practice, when we deal with client data, most of them are implemented in JavaBean, of course, can be blended in decoding in the session bean, but no one is willing to do this, and in fact we may have a lot of beans This approach is not allowed to maintain and update. We can also set up in JSPS / Servlets through servletRequest.setCharateRencoding (String Encoding), but it is also an annoying thing in many JSPS / servlets. The most ideal way is to be eternal and decoded only in one place, that is, in the filter. As long as the data submitted by the customer in the filter correctly, we don't worry about JSPS / servlets / javabeans. Our customers may be the mainland, or it may be Taiwan Province. That is to say, we have to worry about GB2312 and BIG5, the most ideal is to let them submit the data from UTF-8, then You don't have to decide Decoding, here we can use the fourth way to allow the server to affect the browser to select Encoding, which sets the Content-Type in the head, so that all JSP and Servlet's response entities are UTF-8, Then browser will also choose UTF-8 we use to encode submitted data.
Package filters;
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 javax.servlet.http.httpservletRequest;
Public Class EncodingFilter Implements Filter
{
PRIVATE FILTERCONFIG CONFIG;
PRIVATE STRING DEFALUTENCODEING
Public void init (FilterConfig Config)
{
THIS.CONFIG = Config;
Defalutencodeing = config.getinitParameter ("encoding");
}
Public Void Dofilter (ServletRequest Request, ServletResponse Response,
Filterchain Chain) Throws oException, ServletException
{
Request.setCharacterencoding (Defalutencode);
String Uri = (httpservletRequest) Request .getRequesturi ();
IF (Uri.indexof (". jsp")! = -1 || Uri.indexof ("servlet")! = -1)
Response.setContentType ("text / html; charSet =" defalutencode;
System.out.println ("Filter Set The Encoding of the Response TO"
Response.getCharacterencoding ()); chain.dofilter (request, response);
}
Public void destroy ()
{
// ...
}
}
Configure the following configuration in the Web.xml of Context:
init-param>
filter>
filter-mapping>
It can be found with a Test.jsp test, and the browser's output is not as good as we expected, and the test.jsp source code is as follows:
<% @ Page PageEncoding = "GBK"%>
<% String str = "In JSP, the encoding has been set to:" response.getcharacterencoding ();
System.out.println (STR);%>
<% = STR%>
Its output is in Figure 3-3 and Figure 3-4, there is no way to garbled in the client, because we can find that in the filter through the output of the server, the encoding has been set to UTF-8, but In JSP, the encoding is also set to ISO-8859-1. With the filter, the JSP coding setting failed, so the output of JSP failed, we saw garbled.
Figure 3-3 Pre-setting coding failed to the filter for the filter
Figure 3-4 Server Output Description Code in JSP is also set to ISO8859-1
But a servlet code proves that the filter has been set up.
Import javax.servlet.http.httpservlet;
Import javax.servlet.http.httpservletRequest;
Import javax.servlet.http.httpservletResponse;
Import javax.servlet.servletException;
Import java.io.ioException;
Import java.io.printwriter;
Public class encoding extends httpservlet {
Protected Void Doget (HttpservletRequest Request, HttpservletResponse Response)
Throws servletexception, ioException {
PrintWriter out = response.getwriter ();
String str = "In servlet, the code is:" response.getcharacterencoding ();
System.out.println (STR);
Out.println (STR);
Out.flush ();
}
}
You can do experiments in the absence of a filter and without a filter, and the result should be found that the server side and the client get the output of the UTF-8 in the state of the filter. When there is no filter, the server The correct output encoding is ISO-8859-1, but the client outputs garbled, whether it is garbled or no filter servlet when JSP, these are understandable, because when there is no CONTENTTYPE instruction for JSP The JSP engine automatically set it to ISO-8859-1, and use ISO-8859-1 to encode Chinese, of course, only garbled. It is now possible to use a filter to set the response entity encoding to achieve the hope of the encoding of the control browser to submit data, no one will use servlet to output the form-containing HTML page. However, the coding code for setting the data submitted in the filter is still meaningful. For JSP, we can compromise to the ContentType in each JSP, perhaps define ContentType in a JSP file, and other JSP files are feasible to use the file to use the file.
<% - encoding.jsp -%>
<% @ Page ContentType = "Text / HTML; Charset = UTF-8"%>
Static
<% - form.jsp -%>
<% @ Page PageEncoding = "GBK"%>
<% @ include file = "encoding.jsp"%>
<% String name = request.getParameter ("name");%>
hEAD>