Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2566738
  • 博文数量: 709
  • 博客积分: 12251
  • 博客等级: 上将
  • 技术积分: 7905
  • 用 户 组: 普通用户
  • 注册时间: 2005-07-17 00:00
个人简介

实现有价值的IT服务

文章存档

2012年(7)

2011年(147)

2009年(3)

2008年(5)

2007年(74)

2006年(431)

2005年(42)

分类: Java

2006-08-21 14:27:18

J2EE Filter 的几种用法

1。 利用Struts 进行账户权限管理


这里只有一个com.micas.utils.test.MyFilter定义,但是在web app启动实例化了2个instance,分别读入2个参数。并且,指定了触发着两个instance的uri 的mapping.

 1 <web-app xmlns=""
 2          xmlns:xsi=""
 3          xsi:schemaLocation=" /web-app_2_4.xsd"
 4          version="2.4">
 5 
 6     <servlet>
 7         <servlet-name>actionservlet-name>
 8         <servlet-class>org.apache.struts.action.ActionServletservlet-class>
 9         <init-param>
10             <param-name>configparam-name>
11             <param-value>/WEB-INF/struts-config.xml
12             param-value>
13         init-param>
14     servlet>
15 
16     
17     <servlet-mapping>
18         <servlet-name>actionservlet-name>
19         <url-pattern>*.dourl-pattern>
20     servlet-mapping>
21 
22 
23     <display-name>pogodisplay-name>
24 
25     <filter>
26         <filter-name>patGeneralfilter-name>
27         <filter-class>com.micas.utils.test.MyFilterfilter-class>
28         <init-param>
29             <param-name>roleparam-name>
30             <param-value>generalparam-value>
31         init-param>
32     filter>
33 
34     <filter>
35         <filter-name>patManagerfilter-name>
36         <filter-class>com.micas.utils.test.MyFilterfilter-class>
37         <init-param>
38             <param-name>roleparam-name>
39             <param-value>managerparam-value>
40         init-param>
41     filter>
42 
43     <filter-mapping>
44         <filter-name>patManagerfilter-name>
45         <url-pattern>/view/pat/manager/*url-pattern>
46     filter-mapping>
47 
48     <filter-mapping>
49         <filter-name>patGeneralfilter-name>
50         <url-pattern>/view/pat/general/*url-pattern>
51     filter-mapping>
52 
53 
54 web-app>

MyFilter 代码如下:
 1 package com.micas.utils.test;
 2 
 3 
 4 import javax.servlet.Filter;
 5 import javax.servlet.FilterChain;
 6 import javax.servlet.FilterConfig;
 7 import javax.servlet.ServletException;
 8 import javax.servlet.ServletRequest;
 9 import javax.servlet.ServletResponse;
10 import javax.servlet.http.HttpServletRequest;
11 import java.io.IOException;
12 
13 
23 public class MyFilter implements Filter
24 {
25     private String role;
26 
27     public void init(FilterConfig config) throws ServletException
28     {
29          role = config.getInitParameter("role");
30         System.out.println("role is "+role);
31         config.getServletContext().setAttribute("test",role);
32 
33     }
34 
35     public void doFilter(ServletRequest request, ServletResponse response,
36             FilterChain chain)
37             throws IOException, ServletException
38     {
39 
40         if("manager".equalsIgnoreCase(role)){
41             System.out.println("you are in as a manager");
42         }
43         else if("general".equalsIgnoreCase(role)){
44             System.out.println("you are in as general");
45         }
46         else{
47             System.out.println("something not right");
48         }
49         HttpServletRequest hRequest = (HttpServletRequest)request;
50         System.out.println("the file sends the request is "+hRequest.getRequestURI());
51         chain.doFilter(request, response);
52     }
53 
54     public void destroy()
55     {
56     }
57 }
58 
阅读(738) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~