求一个好用的中文编码过滤器的代码 3Q 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <form name="theForm" action="" method="post" onClick="fsubmit()"> <input type="radio" name="page">JSP页面<p> <input type="radio" name="page">Servlet页面<p> 请输入测试内容:<input type="text" name="content"><p> <input type="submit" value="提交"> </form>过滤器package org.sunxin.lesson.jsp.ch21;import java.io.IOException;import javax.servlet.*; public class SetCharacterEncodingFilter implements Filter { protected String encoding = null; protected FilterConfig filterConfig = null; protected boolean ignore = true; public void destroy() { this.encoding = null; this.filterConfig = null; } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // Conditionally select and set the character encoding to be used if (ignore || (request.getCharacterEncoding() == null)) { String encoding = selectEncoding(request); if (encoding != null) request.setCharacterEncoding(encoding); } response.setCharacterEncoding("text/html;charset=+encoding"); chain.doFilter(request, response); } public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig = filterConfig; this.encoding = filterConfig.getInitParameter("encoding"); String value = filterConfig.getInitParameter("ignore"); if (value == null) this.ignore = true; else if (value.equalsIgnoreCase("true")) this.ignore = true; else if (value.equalsIgnoreCase("yes")) this.ignore = true; else this.ignore = false; } protected String selectEncoding(ServletRequest request) { return (this.encoding); }} <?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"> <servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>TestServlet</servlet-name> <servlet-class>org.sunxin.lesson.jsp.ch21.TestServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>TestServlet</servlet-name> <url-pattern>/test</url-pattern> </servlet-mapping> <filter> <filter-name>SetCharacterEncodingFilter</filter-name> <filter-class>org.sunxin.lesson.jsp.ch21.SetCharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>GBK</param-value> </init-param> <init-param> <param-name>ignore</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>SetCharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping></web-app> 刚学hibernate通到的问题,忘解答! jxl读取多个sheet问题 JSP 學習中一基礎問題. Servelt的问题 struts表单问题..急..真心谢谢! eclipse+lomboz编写jsp页面死机的奇怪问题 有点挑战的问题,最近流行的点晴广告它的实现的技术原理是怎样的 怎样设置cookie的有效期 absolute为什么不好用啊?急啊!!! 请做过手机铃声发送的人请进,up有分! 用JAVA拼凑的html字符串怎样在页面中显示? oracle中的varchar2类型在formbean中应该怎么对应?为什么String不行?
<input type="radio" name="page">JSP页面<p>
<input type="radio" name="page">Servlet页面<p>
请输入测试内容:<input type="text" name="content"><p>
<input type="submit" value="提交">
</form>过滤器package org.sunxin.lesson.jsp.ch21;import java.io.IOException;
import javax.servlet.*; public class SetCharacterEncodingFilter implements Filter { protected String encoding = null;
protected FilterConfig filterConfig = null;
protected boolean ignore = true; public void destroy() { this.encoding = null;
this.filterConfig = null; } public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException { // Conditionally select and set the character encoding to be used
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
}
response.setCharacterEncoding("text/html;charset=+encoding"); chain.doFilter(request, response); }
public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null)
this.ignore = true;
else if (value.equalsIgnoreCase("true"))
this.ignore = true;
else if (value.equalsIgnoreCase("yes"))
this.ignore = true;
else
this.ignore = false; } protected String selectEncoding(ServletRequest request) { return (this.encoding); }
} <?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">
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>TestServlet</servlet-name>
<servlet-class>org.sunxin.lesson.jsp.ch21.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
<filter>
<filter-name>SetCharacterEncodingFilter</filter-name>
<filter-class>org.sunxin.lesson.jsp.ch21.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
<init-param>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SetCharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>