资源描述
首页 资讯 精华 论坛 问答 博客 群组 更多 ▼
您还未登录 ! 登录 注册
黑色头发
永久域名
html:cancel按钮的作用,cancel按钮,取消 ... | jsp页面iframe,iframe单选钮显示图片,js ...
2008-08-13
权限管理过滤器,filter,session,页面或者哪个文件下的所有页面进行登陆过滤
博客分类:
· 过滤器
配置管理ServletBeanJSPC
2007-12-27 22:39JAVA源文件:
Java代码
1. package org.filter.system;
2. import java.io.IOException;
3. import javax.servlet.Filter;
4. import javax.servlet.FilterChain;
5. import javax.servlet.FilterConfig;
6. import javax.servlet.ServletContext;
7. import javax.servlet.ServletException;
8. import javax.servlet.ServletRequest;
9. import javax.servlet.ServletResponse;
10. import javax.servlet.http.*;
11. import javax.servlet.*;
12. import org.bean.SessionBean;
13. /**
14. * Title: LOGIN FILTER.
15. *
16. * Description: System user login servlet filter.
17. *
18. * Copyright: Copyright (c) 2007/10/05
19. *
20. * Company: JaMing SoftRoom
21. *
22. * @author: Jamee
23. *
24. * @version 1.0
25. */
26. public class SysLoginFilter implements Filter{
27.
28. FilterConfig config;
29.
30. public void init(FilterConfig config)throws ServletException
31.
32. {
33.
34. this.config=config;
35.
36. }
37. public void doFilter(ServletRequest request,ServletResponse response,
38.
39. FilterChain chain)throws IOException,ServletException
40. {
41.
42. HttpServletRequest hreq=(HttpServletRequest)request;
43.
44. HttpServletResponse hrep=(HttpServletResponse)response;
45.
46. request.setCharacterEncoding("GBK");
47.
48. HttpSession session=hreq.getSession();
49.
50. String back="default.jsp";
51.
52. RequestDispatcher dispatcher=hreq.getRequestDispatcher(back);
53.
54. try{
55.
56. SessionBean user=(SessionBean)session.getAttribute("user");
57.
58. boolean login=user.getIsLogin();
59.
60. if(login)
61. {
62.
63. chain.doFilter(request,response);
64.
65. }else{
66.
67. dispatcher.forward(request,response);
68.
69. }
70.
71. }catch(Exception e){
72.
73. dispatcher.forward(request,response);
74.
75. }
76.
77. }
78.
79. public void destroy(){}
80.
81. }
----附SessionBean.java源文件------------
Java代码
1. package org.bean;
2. /**
3. * Title: Session.
4. *
5. * Description: Session.
6. *
7. * Copyright: Copyright (c) 2007/9/27
8. *
9. * Company: JaMing SoftRoom
10. *
11. * @author: Jamee
12. *
13. * @version 1.0
14. */
15. public class SessionBean
16. {
17.
18. int QX=4;
19.
20. String LOGIN_NAME;
21.
22. boolean IS_LOGIN=false;
23.
24. public void SessionBean(){}
25.
26. public void setLoginName(String name)
27.
28. {
29.
30. this.LOGIN_NAME=name;
31. }
32.
33. public void setIsLogin(boolean l)
34. {
35.
36. this.IS_LOGIN=l;
37. }
38.
39. public void setQX(int i)
40.
41. {
42.
43. this.QX=i;
44.
45. }
46.
47. public String getLoginName()
48.
49. {
50.
51. return this.LOGIN_NAME;
52.
53. }
54.
55. public boolean getIsLogin()
56.
57. {
58.
59. return this.IS_LOGIN;
60.
61. }
62.
63. public int getQX()
64.
65. {
66.
67. return this.QX;
68.
69. }
70. public String getAdmin()
71.
72. {
73.
74. String admin=null;
75.
76.
77. int i=getQX();
78.
79.
80. if(i==0)admin="系统管理员";
81.
82.
83. if(i==1)admin="行政管理员";
84.
85.
86. if(i==2)admin="教务管理员";
87.
88.
89. if(i==3)admin="德育管理员";
90.
91.
92. if(i==4)admin="游客";
93.
94.
95. return admin;
96.
97. }
98. }
-------附web.xml配置---------
Xml代码
1. <filter>
2. <filter-name>ADMIN SESSION FILTER</filter-name>
3. <filter-class>org.filter.system.SysLoginFilter</filter-class>
4. </filter>
5.
6. <filter-mapping>
7. <filter-name>ADMIN SESSION FILTER</filter-name>
8. <url-pattern>/admin/*</url-pattern>
9. </filter-mapping>
解释:
如果你要对哪个页面或者哪个文件下的所有页面进行登陆过滤的话,只需要在<url-pattern></url-pattern>中写入相对路径就可以了.
所有程序都已经调试没有任何错误!
黑色头发
分享到:
html:cancel按钮的作用,cancel按钮,取消 ... | jsp页面iframe,iframe单选钮显示图片,js ...
· 20:31
· 评论 / 浏览 (1 / 2715)
· 相关推荐
评论
1 楼 famingyuan 2009-09-03
if(i==0)admin="系统管理员";
81.
82.
83. if(i==1)admin="行政管理员";
84.
85.
86. if(i==2)admin="教务管理员";
87.
88.
89. if(i==3)admin="德育管理员";
90.
91.
92. if(i==4)admin="游客";
如果用switch 效率会不会高点 . 请勿见怪!只是我一直都觉得switch会比较好点。
展开阅读全文