Topic: 如何设置Filter

  Print this page

1.如何设置Filter Copy to clipboard
Posted by: shokey520
Posted on: 2006-11-23 21:50

正在学习JSP,使用了一个Filter进行统一中文编码,Filter名为econding。在web.xml中配置如下:
<filter>
  <filter-name>encoding</filter-name>
  <filter-class>com.demo.filter.EncodingFilter</filter-class>
  <init-param>
    <param-name>encoding</param-name>
    <param-value>GBK</param-value>
  </init-param>
</filter>
<filter-mapping>
  <filter-name>encoding</filter-name>
  <url-pattern>/test/*</url-pattern>
</filter-mapping>
但是当我在浏览器中键入http://127.0.0.1:8080/test/index.jsp时,这个页面中的中文还是乱码。
而且我在Filter的doFilter方法中加入System.out.println("请求编码");Tomcat控制台没有任何输出。
我被这个搞郁闷两天了,请高手指点一下。

2.Re:如何设置Filter [Re: shokey520] Copy to clipboard
Posted by: YuLimin
Posted on: 2006-11-24 19:51

web.xml全部的内容看看,reload了应用没有?

3.Re:如何设置Filter [Re: shokey520] Copy to clipboard
Posted by: shokey520
Posted on: 2006-11-25 03:40

以下是web.xml的全部的内容
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
version="2.4">
<display-name>demo</display-name>

<filter>
  <filter-name>encoding</filter-name>
  <filter-class>com.demo.filter.EncodingFilter</filter-class>
  <init-param>
    <param-name>encoding</param-name>
    <param-value>GBK</param-value>
  </init-param>
</filter>


<filter-mapping>
  <filter-name>encoding</filter-name>
  <url-pattern>/demo/*</url-pattern>
</filter-mapping>
<filter-mapping>
  <filter-name>encoding</filter-name>
  <url-pattern>/demo/test.jsp</url-pattern>
</filter-mapping>

<servlet>
<servlet-name>debugjsp</servlet-name>
<description>Added to compile JSPs with debug info</description>
<servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
<init-param>
<param-name>classdebuginfo</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>3</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>debugjsp</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>

<welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

</web-app>

4.Re:如何设置Filter [Re: shokey520] Copy to clipboard
Posted by: xiaoxiaohei
Posted on: 2007-02-16 10:46

应该改成
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
这样就可以了


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923