RightFilter.java
上传用户:nbxinmin
上传日期:2021-10-09
资源大小:46k
文件大小:3k
源码类别:

Internet/IE编程

开发平台:

Java

  1. package com.xdf.exams.filter;
  2. import java.io.IOException;
  3. import javax.servlet.Filter;
  4. import javax.servlet.FilterChain;
  5. import javax.servlet.FilterConfig;
  6. import javax.servlet.ServletException;
  7. import javax.servlet.ServletRequest;
  8. import javax.servlet.ServletResponse;
  9. import javax.servlet.http.HttpServletRequest;
  10. import javax.servlet.http.HttpServletResponse;
  11. import com.xdf.exams.bean.Teacher;
  12. public class RightFilter implements Filter{
  13. public void destroy() {
  14. }
  15. public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
  16. HttpServletRequest request = (HttpServletRequest) req;
  17. HttpServletResponse response = (HttpServletResponse)resp;
  18. String uri = request.getRequestURI();
  19. String ctx = request.getContextPath();
  20. uri = uri.substring(ctx.length());
  21. //考试中
  22. if(request.getSession().getAttribute("examinglist")!=null) {
  23. if((uri.endsWith(".do")||uri.endsWith(".jsp")||uri.equals("/"))&&!uri.equals("/student/exam/go.do")&&!uri.equals("/student/exam/end.do")) {
  24. request.setAttribute("message","正在考试请不要访问其他页面!");
  25. request.getRequestDispatcher("/student/exam/go.do").forward(request,response);
  26. return;
  27. }
  28. }
  29. if(uri.startsWith("/student")) {
  30. if(request.getSession().getAttribute("student")==null) {
  31. request.setAttribute("message","您没有登陆");
  32. request.getRequestDispatcher("/index.jsp").forward(req,resp);
  33. return;
  34. }
  35. }
  36. if(uri.startsWith("/teacher")) {
  37. if(request.getSession().getAttribute("teacher")==null) {
  38. request.setAttribute("message","您没有登陆");
  39. request.getRequestDispatcher("/index.jsp").forward(req,resp);
  40. return;
  41. }
  42. if(uri.endsWith("/delete.do")||uri.endsWith("/updatedo.do")
  43. ||uri.endsWith("/deletequestion.do")||uri.endsWith("/updatequestiondo.do")
  44. ) {
  45. Teacher t = (Teacher)request.getSession().getAttribute("teacher");
  46. if(!t.getUsername().equals("admin")) {
  47. request.setAttribute("message","您没有删除和修改的权限");
  48. request.getRequestDispatcher("/teacher/message.jsp").forward(req,resp);
  49. return;
  50. }
  51. }
  52. }
  53. if(uri.startsWith("/teacher/admin")) {
  54. Teacher t = (Teacher)request.getSession().getAttribute("teacher");
  55. if(!t.getUsername().equals("admin")) {
  56. request.setAttribute("message","您没有修改教师的权限");
  57. request.getRequestDispatcher("/teacher/message.jsp").forward(req,resp);
  58. return;
  59. }
  60. }
  61. chain.doFilter(req,resp);
  62. }
  63. public void init(FilterConfig config) throws ServletException {
  64. }
  65. }