public class EncodingListener implements ServletRequestListener {
public void requestDestroyed(ServletRequestEvent req) {
}
public void requestInitialized(ServletRequestEvent req) {
try {
((HttpServletRequest) req.getServletRequest())
.setCharacterEncoding("gbk");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<listener>
<listener-class>com.wy.tools.EncodingListener</listener-class>
</listener>
</web-app>