永远的草莓地Strawberry79~
===========================================================
Filter过滤器的使用
===========================================================

本篇文章是介绍javax.servlet包中Filter的几个功能,一是字符编码过滤,二是拦截指文件夹中的文件,实现登录验证.


本篇文章是介绍javax.servlet包中Filter的几个功能,一是字符编码过滤,二是拦截指文件夹中的文件,实现登录验证.实现以上两个功能需以下几个步骤: 1、实现javax.servlet.Filter接口,编写两个类,如下: /** * 作用:过滤编码 */ package filter; import java.io.IOException; import javax.servlet.*; import javax.servlet.http.HttpServletRequest; public class EncodingFilter implements Filter { protected FilterConfig filterConfig; private String targetEncoding; public EncodingFilter() { targetEncoding = "gb2312";//直接初始化0912 } public void init(FilterConfig filterconfig) throws ServletException { filterConfig = filterconfig; //targetEncoding = filterconfig.getInitParameter("encoding");//web.xml挂参初始化 } public void doFilter(ServletRequest servletrequest, ServletResponse servletresponse, FilterChain filterchain) throws IOException, ServletException { HttpServletRequest httpservletrequest = (HttpServletRequest)servletrequest; httpservletrequest.setCharacterEncoding(targetEncoding); System.out.println("使用"+targetEncoding+"对请求进行编码过滤"); filterchain.doFilter(servletrequest, servletresponse); } public void setFilterConfig(FilterConfig filterconfig) { filterConfig = filterconfig; } public void destroy() { filterConfig = null; } } /** * 作用:过滤外界非法的进入页面 * 注意:使用该功能时,一定要把登录页与被过滤文件夹设为同级,否则会出现循环 */ package filter; import java.io.IOException; import javax.servlet.*; import javax.servlet.http.*; public class ManageFilter implements Filter { String LOGIN_PAGE; protected FilterConfig filterConfig; public ManageFilter() { LOGIN_PAGE = "/login.jsp"; } public void doFilter(ServletRequest servletrequest, ServletResponse servletresponse, FilterChain filterchain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) servletrequest; HttpServletResponse response = (HttpServletResponse) servletresponse; HttpSession session = request.getSession(); String key = ""; try { //通过验证时在session中存的一个标志 key = (String) session.getAttribute("key"); if (key!= null&&key.equals("ok")) { filterchain.doFilter(servletrequest, servletresponse); } else { response.sendRedirect(LOGIN_PAGE); System.out.println("被拦截一个未认证的请求"); } } catch (Exception exception) { exception.printStackTrace(); } } public void setFilterConfig(FilterConfig filterconfig) { filterConfig = filterconfig; } public void destroy() { filterConfig = null; } public void init(FilterConfig filterconfig) throws ServletException { filterConfig = filterconfig; } } 2、在web.xml中做一些设置 code filter.EncodingFilter manage filter.ManageFilter code /* manage /ShuiBaoRHPWK_NEW/system/ruhepaiwukou/*-->

strawberry79 发表于:2005.02.28 13:35 ::分类: ( Struts技术研究 ) ::阅读:(1896次) :: 评论 (71)
re: Filter过滤器的使用 [回复]

抄的哪的?laughinglaughing

可笑 评论于: 2006.08.13 13:21
re: Filter过滤器的使用 [回复]

乱糟糟的angry.gif

的 评论于: 2006.08.15 17:44
re: Filter鹿媒脗脣脝梅碌脛脢鹿脫脙 [回复]

Nice

Charalambos 评论于: 2007.03.19 23:12
re: Filter鹿媒脗脣脝梅碌脛脢鹿脫脙 [回复]

Cool...

Herakles 评论于: 2007.03.21 01:36
re: Filter鹿媒脗脣脝梅碌脛脢鹿脫脙 [回复]

Cool}Cool!

Drymiotes 评论于: 2007.03.21 08:52
re: Filter鹿媒脗脣脝梅碌脛脢鹿脫脙 [回复]

Nice...

Athanasios 评论于: 2007.03.22 02:51
re: Filter鹿媒脗脣脝梅碌脛脢鹿脫脙 [回复]

Cool...

Vasileios 评论于: 2007.03.22 06:14
re: Filter鹿媒脗脣脝梅碌脛脢鹿脫脙 [回复]

Nice

Herakles 评论于: 2007.03.22 12:50
re: Filter鹿媒脗脣脝梅碌脛脢鹿脫脙 [回复]

Cool}Cool!

Ioannes 评论于: 2007.03.22 16:21
re: Filter鹿媒脗脣脝梅碌脛脢鹿脫脙 [回复]

interesting

Odysseas 评论于: 2007.03.24 03:02
re: Filter鹿媒脗脣脝梅碌脛脢鹿脫脙 [回复]

Nice

Ivan 评论于: 2007.03.26 05:32
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

Sorry angry.gif

Iakovos 评论于: 2007.07.26 13:02
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

Nice

Manolis 评论于: 2007.07.26 20:32
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

Cool!

Kostas 评论于: 2007.07.27 19:27
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

Interesting...

Athanassios 评论于: 2007.07.28 05:20
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

Cool...

Nicolaon 评论于: 2007.07.28 05:46
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

Interesting...

Thanasis 评论于: 2007.07.28 18:03
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

Cool!

Stefanos 评论于: 2007.07.28 22:52
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

interesting

Anastasios 评论于: 2007.07.29 05:27
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

Nice!

Doxiadis 评论于: 2007.07.29 15:09
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

Cool!

Sotirios 评论于: 2007.07.29 22:38
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

Cool...

Antonis 评论于: 2007.07.30 12:56
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

Nice...

Alekos 评论于: 2007.07.30 17:31
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

Cool!

Arion 评论于: 2007.07.31 06:47
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

Nice!

Emmanuil 评论于: 2007.07.31 17:52
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

Nice...

Theodoros 评论于: 2007.08.01 07:54
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

Nice

Theologos 评论于: 2007.08.01 10:06
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

interesting

Evangelos 评论于: 2007.08.01 14:26
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

Nice!

Charalambos 评论于: 2007.08.02 02:30
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

Cool.

Platon 评论于: 2007.08.02 03:28
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

Interesting...

Alexandros 评论于: 2007.08.02 14:46
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

Nice...

Kosta 评论于: 2007.08.05 16:45
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

Interesting...

Ivan 评论于: 2007.08.05 20:42
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

Sorry angry.gif

Constantinos 评论于: 2007.08.05 22:54
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

Cool.

Loukas 评论于: 2007.08.06 17:50
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

Nice

Alexandros 评论于: 2007.08.07 15:08
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

Nice...

Glafkos 评论于: 2007.08.07 17:13
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

interesting

Crist 评论于: 2007.08.08 19:40
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

interesting

Constantinos 评论于: 2007.08.09 19:38
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

Sorry angry.gif

Harrys 评论于: 2007.08.09 20:24
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

Nice!

Eleftherios 评论于: 2007.08.09 22:23
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

Cool!

Herakles 评论于: 2007.08.10 17:46
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

Cool.

Apostolos 评论于: 2007.08.11 19:45
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

Cool.

Nikolaos 评论于: 2007.08.12 18:58
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

Cool...

Ivan 评论于: 2007.08.13 03:42
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

Interesting...

Epaminondas 评论于: 2007.08.13 16:06
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

Nice...

Kris 评论于: 2007.08.14 23:51
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

Cool.

Epameinondas 评论于: 2007.08.15 04:49
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

Interesting...

Aiolos 评论于: 2007.08.15 06:04
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

Nice

Hermes 评论于: 2007.08.15 12:32
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

Cool!

Sophocles 评论于: 2007.08.15 16:06
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

Sorry angry.gif

Hristos 评论于: 2007.08.16 06:34
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

Nice...

Nico 评论于: 2007.08.16 08:14
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

Nice

Yanni 评论于: 2007.08.16 11:07
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

Nice...

Lambros 评论于: 2007.08.16 14:47
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

interesting

Philippos 评论于: 2007.08.16 21:41
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

Interesting...

Simos 评论于: 2007.08.17 10:37
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

Nice

Vardis 评论于: 2007.08.17 17:59
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

Nice...

Leonidas 评论于: 2007.08.17 22:53
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

Interesting...

Pantelis 评论于: 2007.08.18 09:09
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

Cool...

Zaharias 评论于: 2007.08.18 10:49
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

Cool.

Manos 评论于: 2007.08.19 02:48
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

Sorry angry.gif

Makarios 评论于: 2007.08.19 08:51
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

Cool...

Constantine 评论于: 2007.08.21 01:38
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

Nice...

Mamadshah 评论于: 2007.08.21 09:33
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

Nice

Ahmed 评论于: 2007.08.21 15:05
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

Interesting...

Vasilios 评论于: 2007.08.24 03:56
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

interesting

Kimon 评论于: 2007.08.24 12:11
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

Nice!

Theodosios 评论于: 2007.08.24 12:16
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

Cool...

Stelios 评论于: 2007.08.24 20:58
re: Filter锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷 [回复]

Cool.

Petros 评论于: 2007.08.25 09:06

发表评论
标题

在此添加评论
表情符号: smile laughing tongue angry crying sad wassat wink

称呼

邮箱地址(可选)

个人主页(可选)




自我介绍
切换风格
新闻聚合
博客日历
文章归档...
最新发表...
博客统计...
Blog信息
网站链接...