js|servlet| Coding | Problem using filter to change the code of request
In the previous article, we discussed the JSP and the servlet character encoding problem under Tomcat!
Know that when the code for request is not specified, the data obtained from the client is ISO-8859-1 encoded (Request.getparameter () The value of the parameter being passed);
But how do we change the code for the request?
There are many kinds of methods!
For example: In Getrequestdispatcher ("/jsp/jsptoserv/hello.jsp"). Forward (request, response);
The encoding of the request, then the parameter value obtained in the jsp/jsptoserv/hello.jsp is the coded character.
In this article we use filter to modify the request code!
1) First write the filter class:
Package myfilter;
Import java.io.IOException;
Import javax.servlet.*;
public class Changecharsetfilter implements Filter {
protected String encoding = null;/////encoding to be developed, configured in Web.xml
protected Filterconfig filterconfig = null;
public void Destroy () {
this.encoding = null;
This.filterconfig = null;
}
public void Dofilter (ServletRequest request, servletresponse response, Filterchain chain)
Throws IOException, Servletexception {
if (request.getcharacterencoding () = null) {
String encoding = GetEncoding ();////gets the specified encoded name
if (encoding!= null)
request.setcharacterencoding (encoding);////set the encoding of the request
}
Chain.dofilter (request, response);///has the opportunity to execute the next filter
}
public void init (Filterconfig filterconfig) throws Servletexception {
This.filterconfig = Filterconfig;
this.encoding = Filterconfig.getinitparameter ("encoding");///get the encoding configured in Web.xml
}
Protected String getencoding () {
return (this.encoding);///get the specified encoding
}
}
2. Edit Web.xml File
<?xml version= "1.0" encoding= "Iso-8859-1"?>
<! DOCTYPE Web-app
Public "-//sun Microsystems, INC.//DTD Web application 2.3//en"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<filter>
<filter-name>SetCharacterEncoding< /filter-name>
<filter-class>myfilter.changecharsetfilter </filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>gb2312</param-value>////// Specifies that the encoding is GB2312
</init-param>
</ Filter>
<filter-mapping>
< Filter-name>setcharacterencoding</filter-name>
< url-pattern>/*</url-pattern>////////change its encoding for all request
</filter-mapping>
</web-app>
///
3.
Write a a.jsp
<%@ page contenttype= "text/html; charset=gb2312 "%>
<body>
<%
String name=request.getparameter ("name");///originally the character is iso-8859-1 encoded and cannot be directly
Output in the console, but now changes the way the request is encoded, at which point the name encoding is GB2312, so it can be displayed correctly in the console.
SYSTEM.OUT.PRINTLN (name);
%>
<form action= "a.jsp" method= "POST" >
<input type= "text" name= "name" >
<input type= "Submit" >
</form>
<%=name%>
</body>
Finish!
About the Chinese processing of the problem is written this!