编程开发 > JSP > 文章内容

jsp中过滤器选择过滤器的写法详解

2017-4-27编辑:ljnbset

jsp中过滤器选择过滤器的写法详解

实例如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

public class LoginCheckFilter implements Filter {

 

 

private FilterConfig config=null;

private String webroot=null;

public void destroy() {

// TODO Auto-generated method stub

 

 

}

 

 

@Override

public void doFilter(ServletRequest req, ServletResponse resp,

FilterChain chain) throws IOException, ServletException {

// TODO Auto-generated method stub

HttpServletRequest request=(HttpServletRequest)req;

HttpServletResponse response=(HttpServletResponse)resp;

HttpSession session=request.getSession(false); //取得当前会话的session,没有session也不创建session,同request.getSession()一样

 

 

String url=request.getRequestURI(); //获取当前输入的 /项目/当前访问的路径 与我们定义的路径是否相同

 

 

if(url!=null&&url.equals(webroot+"/login.jsp")&&url.equals(webroot+"/LoginServlet")){

 

chain.doFilter(req,resp);

}else{

 

if(session==null){

response.sendRedirect(webroot+"/login.jsp");

 

}else{

 

String user2=(String)session.getAttribute("user1");

if(user2==null){

 

response.sendRedirect(webroot+"/login.jsp");

 

}else{

 

chain.doFilter(request, response);

 

}

}

 

}

 

}

 

 

@Override

public void init(FilterConfig config) throws ServletException {

// TODO Auto-generated method stub

this.config=config;

ServletContext ctx=config.getServletContext(); //获取url  /项目名

webroot=ctx.getContextPath();    //项目名

 

 

}

}

SpringAOP代理详细介绍

热点推荐

登录注册
触屏版电脑版网站地图