有趣的 Servlet Filter(0分)

  • 主题发起人 主题发起人 yysun
  • 开始时间 开始时间
Y

yysun

Unregistered / Unconfirmed
GUEST, unregistred user!
Filter 是 servlet 2.3 的新内容,用它可以做:
authen
tication filters
logging and auditing filters
image conversion filters
data compression filters
encryption filters
tokenizing filters
filters that trigger resource access events
XSL/T filters that transform XML content
MIME-type chain filters
http://java.sun.com/products/servlet/Filters.html#73808
http://java.sun.com/webservices/docs/ea1/tutorial/doc/Servlets8.html#64572
http://www.opensymphony.com/sitemesh/faq.html
http://www.onjava.com/pub/a/onjava/2001/05/10/servlet_filters.html
http://www.javaworld.com/javaworld/jw-06-2001/jw-0622-filters.html
http://www-106.ibm.com/developerworks/java/library/j-tomcat/?open&l=101,t=grj,p=TomcatTricks
http://www.orionserver.com/tutorials/filters/introduction
 
不大理解,是不是是jsp 1.3里的内容?
孙老师,我有个问题,servlet使用起来那么麻烦,为什么servlet还在发展呢?
JSP是否能够包含了它所有的功能?
请您用中文介绍几句filter,如果有空的话。
并非不愿看英文,而是 水平太低,不大理解。而且我比较喜欢断章取义。
 
filter只是让层次更加清楚。
 
一个 servlet 可以配上多个 filter,一个 filter 也可以被多个 servlet 使用,非常方便。
大家可能都熟悉 stream 的概念,创建一个数据流,可以外套多个 reader/writer,实现加密、压缩 ...
servlet filter 异曲同工之妙。这个在 Design pattern 中属于 Decorator Pattern。
http://www.opensymphony.com 的 sitemesh 就是用了 servlet filter。
 
Filter 是 [red]servlet 2.3[/red] 的内容。
servlet 基本思想是当 web server 遇到客户器的请求,就创建 request 对象(输入)
和 response 对象(输出),交由 servlet 处理。servlet 从 request 获得输入,
计算后,把结果输入到 response, web server 把 response 中内容送到客户器。
Filter 就是在这个过程上再加一层。
在 web server 把 request 交给 servlet 前,可以用个 filter 预先过滤一下 request,
做:用户检验、Logging、处理文件上载、解密、转发等等。
在 servlet 把 response 交给 web server 输出网页前,也可以用个 filter 过滤一下 request,
做:坏字过滤、数据压缩、加密、XSL 转换等等。
上面所列参考文献
onjava 的文章介绍了 logging 和用户输入检验的 filter。
javaworld 的文章是个处理上载文件的 filter。
java.sun.com 的文章里有个处理 xsl 的 filter,可惜似乎没有写全。
xsl filter 可以看这个: http://www.servletsuite.com/servlets/xmlflt.htm
 
孙老师,看来您的问题曲高和寡呀!几天也没有几个人发言。
又学这,又学那,真的好累。
您的问题我还是要慢慢理解。。。努力中!
从您给的网址里,找到一段代码,贴出来大家看看。我始终都觉得有程序的话,更容易理解一些。
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class TimerFilter implements Filter {
private FilterConfig config = null;
public void init(FilterConfig config) throws ServletException {
this.config = config;
}
public void destroy() {
config = null;
}
public voiddo
Filter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
long before = System.currentTimeMillis();
chain.doFilter(request, response);
long after = System.currentTimeMillis();
String name = "";
if (request instanceof HttpServletRequest) {
name = ((HttpServletRequest)request).getRequestURI();
}
config.getServletContext().log(name + ": " + (after - before) + "ms");
}
}
您给的这个网址有着很完备的filters教程,
http://www.orionserver.com/tutorials/filters/introduction/
只是我有点疑问,filters非常重要吗?有没有必要非要花很大的精力去把它看完?
 
嘿嘿,这个程序比较简单,一起贴出来:
package com.filters;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;
import javax.servlet.ServletException;
public class SimpleFilter implements Filter
{
private FilterConfig filterConfig;
public voiddo
Filter (ServletRequest request,
ServletResponse response,
FilterChain chain)
{
try
{
System.out.print ("Within Simple Filter ... ");
System.out.println ("Filtering the Request ...");
chain.doFilter (request, response);
System.out.print ("Within Simple Filter ... ");
System.out.println ("Filtering the Response ...");
} catch (IOException io) {
System.out.println ("IOException raised in SimpleFilter");
} catch (ServletException se) {
System.out.println ("ServletException raised in SimpleFilter");
}
}
public FilterConfig getFilterConfig()
{
return this.filterConfig;
}
public void setFilterConfig (FilterConfig filterConfig)
{
this.filterConfig = filterConfig;
}
}

--------------------------------------------------------------------------------
As you can see the simple filter example is a Java class named SimpleFilter.java which implements the javax.servlet.Filter interface. It provides implementation for the three methods defined in the javax.servlet.Filter interface.
Notice thatdo
Filter() can be separated into two sections: filtering the request and filtering the response. The two sections are separated by a call from the javax.servlet.FilterChain object to the next object in the chain, which could be the Servlet or another filter.
Now that we have written the filter, it might be nice to deploy it in a web server and see it in action.
 
我没有说 servlet filter “非常重要”。此贴标题不过是“有趣的 servlet filter”。
由于有些人包括 wukw 认为 servlet 已经没有什么发展了云云,我看了一下 servlet 2.3
的 spec,看到了 filter 和 listener。
 
filter的确比较有趣,可以衍生很多应用。
 
>>由于有些人包括 wukw 认为 servlet 已经没有什么发展了云云,
孙老师,您千万不要和我一般见识。
我只是一个菜菜鸟,jsp刚入门,servlet程序一共只运行成功了一个。
什么structs , XML , filter,我全都不懂。您老千万不要和我一般见识。
虽然正在努力学习,可是我估计再过两年,我也未必能达到曹晓钢现在的水平。
我在国家机关混,没有好好的项目,没有高手,就是自己时间比较多。
若要大幅度提高技战术水平,还得另外想办法,光自学不行。
最近,我还是觉得编exe程序比较好玩(比较有成就感)。
又想好好学VC(已有一定的基础),天!何时才能成为高手!
自学怎样才能成为高手?迷茫中~~
继续关注您贴出的所有文章~~
 
wukw,没有什么大不了的,这些技术有又是我们发明的,早知道一点或者多知道一点并不
代表什么。不断学习和积累就会知道得越来越多。我看你挺会看网上的文章,这个很好。
我也是这么查查文章。如果你能够做得仔细些则会更好的。
这里是专门介绍 Filter 的一个章节,来自《More Servlets and JSP》一书:
http://developer.java.sun.com/developer/Books/javaserverpages/servlets_javaserver/servlets_javaserver09.pdf
 
to yysun, 我看的文章大多数都是国内的Java网站的文章。尤其是初学者或者常见的问题都有很详细的解答。
几个月来我收集了很多Java网址,做点好事,我列出来:(其中的好多网站还是没有时间看)
2。Java/JSP
http://java.sun.com/products/servlet/2.3/javadoc/index.html JSP的参考文档
http://java.sun.com/docs/books/tutorial/index.html 这个比较适合初学者,中高级者也可以看,因为它的概念描述非常清晰,又是免费的。
http://www-900.ibm.com/developerWorks/java/index.shtml IBM的中文Java文档
http://scjp.home.sohu.com/ 模拟试题
http://scjp.myrice.com
http://java.sun.com/products/jsp/faq.html
http://www.sun.com.cn/education
http://java.sun.com
http://java.sun.com/j2se/1.4/docs/api/index.html java官方网站的帮助
http://java.sun.com/j2se/1.4/docs/
http://java.sun.com/j2se/1.4/download.html#docs 可下载版本
http://java.sun.com/docs/windows_format.html winhelp格式的
http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html 这个是CodingConvension,就是编码规则。写Java代码,这个是最基本的,必看的资料。
http://61.140.76.55:8080/index.html 木棉数字校园网
http://www.cnkapok.com/course/c_bea3.html weblogic认证
http://www.bea.com.cn/channel/education/index.html
http://www.servlets.com/docs/index.html
http://my-wjl.scu.edu.cn/~xxuzi/ 极品SCJP
http://my-wjl.scu.edu.cn/~xxuzi/
http://jdeveloper.myrice.com/
http://www.sawin.com.cn/dragon/manual/manualindex.htm
http://61.144.28.245/hjc/web/doc/servlet-jsp/servlet-jsp4.html
www.sybex.com
www.mcp.com
www.bbook.net
www.bjbb.com
www.medias.com.cn
www.phei.com.cn
http://www.justjavajobs.com/jjavj.nsf/autoj/
http://www.psclearn.com/
edge-china.com
http://www.commission-junction.com/track/track.dll?AID=55509&PID=499008&URL=http%3A%2F%2Fwww%2Ejobsleuth%2Ecom%2Fregister%2Ecfm%3F%26ref%3D2261
http://suned.sun.com/USA/certification/java_news_faq.html
http://suned.sun.com/USA/certification/progobj.html
http://suned.sun.com/usa/cert_test.html
http://www.java-cn.net/
http://dboy520.51.net/cgi-bin/javajia/
http://www.javalobby.org/
http://www.uni-koeln.de/themen/java/
http://www.javacats.com/US/articles/MultiThreading.html
http://www.rstcorp.com/javasecurity/links.html
http://www.playjavagames.com
http://physicsweb.org/TIPTOP/VLAB/
http://www.eastjava.com/
http://rigauxf.waika9.com/
http://ww4.chatweb.net/javachat/
http://www.ajug.org/
http://www.mste.uiuc.edu/java/
http://www.acm.org/crossroads/xrds4-2/serial.html
http://abc-java.com/
http://www.chatattack.net/
http://jazz.external.hp.com/src/java/
http://www.jerenajava.com/
http://www.central-java.com
http://www.acm.org/sigs/sigada/education/pages/ada_java.html
http://www.hoskinson.net/java/
http://java.areco.cz
http://www.javamud.org/
http://www.java.online.tr.tc/
http://www.engapplets.vt.edu/
国外:
http://www.jiveforum.com
http://www.theserverside.com
http://www.jdance.com
http://foundries.sourceforge.net/java
http://edocs.beasys.com/index.html
http://www.hostj2ee.com
http://www.hostjsp.com
http://www.onjava.com
http://ejbinfo.com
http://www.mgm-edv.de/ejbsig/ejbsig.html
http://www.uml-zone.com
http://www.jguru.com
http://www.ibm.com/developerworks
http://www.ibm.com/deveoperworks/cn 大家多看看,很不错的。ibm新推出一个websphere园地,内容也是很不错的。
国内:
http://www.javaunion.com
http://www.javaunion.org
http://www.javaunion.net
http://www.huihoo.com
http://www.javadigest.net
http://drivejava.www2.cn4e.com
http://www.umlchina.com
http://www.drivejava.com
http://www.javaunion.org/ 中国Java阵线联盟
http://www.javaunion.com Java阵线联盟(Java认证学习资料)
http://www.chinajavaworld.com/ Java开发者(XML教程,模考试题1)
http://javalovers.myetang.com Java爱好者
http://www.cn-java.com 中文Java技术网站
http://www.cnjavaclub.com/ 中国Java俱乐部
http://www.52jsp.com/index.jsp 52JSP
http://www.cnjsp.com/ 中国JSP网站
http://www.jsp001.com/ JSP001
http://www.china-jsp.net/ JSP新升代/也有ASP
http://www.javaxforum.com javaxforum开发指南,SCJP认证资源
http://www.pconline.com.cn/pcedu/empolder/wz/jsp/index.html pconline的一些教程
http://adionline.myrice.com/ 阿迪在线
http://216.239.33.100/search?q=cache:UiFo_1XVUN8C:members.aol.com/mnsjava/referate/AWTReferat/syntax.html+MenuShortcur&hl=zh-CN //常用Java界面控件方法说明
http://www.thejmaker.com/ Java控件
http://www.jpowered.com/ Java控件
http://www.jspsmart.com/
http://www.javareport.com
http://www.javaworld.com
http://www.jiveforum.com/
http://www.theserverside.com/home/index.jsp
http://www.gcsuncenter.com/eforum.nsf/eForumFrameset?OpenFrameSet
http://www.esperanto.org.nz/jsp/jspfaq.html
http://www.zdnet.com/pcweek/stories/news/0,4153,410709,00.html
http://developer.netscape.com/viewsource/kuslich_jsp/kuslich_jsp.html
http://web2.java.sun.com/products/jsp/jsp-asp.html
 
请您再看一下,这个问题,谢谢!
http://www.delphibbs.com/delphibbs/dispq.asp?lid=1118062
 
今年 SunOne 会议上有个发言介绍 Servlet Filter 说,
对于 MVC 结构的应用,Filter 将取代 servlet.
How Filters Replace Servlets in the JavaTM Platform Web Application Programming Model
http://servlet.java.sun.com/javaone/resources/content/sf2002/conf/sessions/pdfs/1208.pdf
 
yysun,关于这个问题,我在struts dev mailling list里面问了一下,这是答案:
On Fri, 5 Jul 2002, Xiaogang Cao wrote:
> Date: Fri, 5 Jul 2002 10:07:33 +0800
> From: Xiaogang Cao <caoxg@yahoo.com>
> Reply-To: Struts Developers List <struts-dev@jakarta.apache.org>
> To: Struts Developers List <struts-dev@jakarta.apache.org>
> Subject: Will Servlet Filter replace servlet as Controller in Struts
> framework?
>
> Hi, there:
> In SunOne meeting, there is a presentation
> (http://servlet.java.sun.com/javaone/resources/content/sf2002/conf/sessions/
> pdfs/1208.pdf) saying Filter will replace Servlet in some senario,
> especially when be used as controllers in an MVC style application.
> Actually Struts is the most famous MVC style web application framework, so,
> can anyone tell me are you using Filter as controller, or are you going to
>do
in this way? any plan?
>
Filters are really cool, and would make a very interesting technology to
use as the controller in an MVC framework like Struts. The only problem
is that they require a Servlet 2.3 platform to run on. For Struts 1.1 at
least, we need to continue supporting Servlet 2.2 environments, so the
controller will remain a servlet at least for that rev.
The developers haven't formally discussed or decided on what happens next
(we're concentrating on gettting 1.1 out first), but I would consider it
pretty likely that a next major version of Struts would declare Servlet
2.3 (and JSP 1.2) as prerequisites. That will let us also integrate the
JSP Standard Tag Library (JSTL) 1.0 that was just released, as well as
JavaServer Faces (currently under development in JSR-127). Both of those
libraries have 2.3/1.2 dependencies, so we'd need to have the same base
platform if we want to utilize them.
Given that we make the 2.3 decision, I think it's very worthwhile looking
at how the controller in Struts is put together, and consider at least an
alternative implementation based on filters.
> Best Regards.
> Xiaogang Cao.
Craig
 
谢谢曹大侠。您提供的信息和我得到的信息,都印证了我对 java web app 走向的判断:
轻量级的 web app,基于: STL + JavaServre Faces
重量级的 web app,基于: STL + JavaServre Faces + MVC + EJB
这些东西中,唯独 JavaServre Faces 干闻打雷,不见下雨,好着急
http://www.delphibbs.com/delphibbs/dispq.asp?lid=1191271
不知道曹大侠在 JavaServre Faces 方面有何信息?
 
整合Tomcat5.0.7和IIS5
http://www.delphibbs.com/keylife/iblog_show.asp?xid=2172
 
不好意思,结束帖子的是结束错了。

 
后退
顶部