当前位置:   article > 正文

使用filter过滤器实现对用户登录的检查,如果未登录跳转至登录页面_out.println("您没有登录,请先登录,3秒自动跳回登录页面。"); res.sethead

out.println("您没有登录,请先登录,3秒自动跳回登录页面。"); res.setheader("ref

        对于前几天写的一个简单的学生管理系统,想要实现对于用户登陆检查的功能,就上网自学了filter知识

        第一步先在项目目录中创建包filter,在此包下面创建filter名为LoginFilter,代码如下

  1. package com.student.filter;
  2. import com.student.entity.Users;
  3. import com.sun.deploy.net.HttpRequest;
  4. import javax.servlet.*;
  5. import javax.servlet.annotation.WebFilter;
  6. import javax.servlet.http.HttpServlet;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. import javax.servlet.http.HttpSession;
  10. import java.io.IOException;
  11. import java.io.PrintWriter;
  12. /**
  13. * @Description
  14. * @Author 单继重
  15. * @Date 2016/10/24 10:19
  16. */
  17. @WebFilter(filterName = "LoginFilter")
  18. public class LoginFilter implements Filter {
  19. public void destroy() {
  20. }
  21. public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
  22. //chain.doFilter(req, resp);
  23. HttpServletRequest request = (HttpServletRequest)req;
  24. HttpServletResponse response = (HttpServletResponse)resp;
  25. request.setCharacterEncoding("UTF-8");
  26. response.setContentType("text/html;charset=UTF-8");
  27. HttpSession session = request.getSession();
  28. PrintWriter out = response.getWriter();
  29. Users user = (Users)session.getAttribute("user");
  30. if(user != null){
  31. chain.doFilter(request,response);
  32. } else{
  33. out.println("您还未登陆,三秒钟后跳转至登录页面");
  34. //out.println("<script language='javascript'>alert('你还未登录');");
  35. response.setHeader("refresh","3;/pages/users/login.jsp");
  36. //response.sendRedirect("/pages/users/login.jsp");
  37. //request.getRequestDispatcher("/pages/users/login.jsp").forward(request,response);
  38. }
  39. }
  40. public void init(FilterConfig config) throws ServletException {
  41. }
  42. }

        待解决问题:JS的alert方法无法实现,不知道原因。

        未登录跳页使用response.setHeader实现定时跳页功能。

         第二步在web.xml文件中配置相应文件,部分代码如下

<filter>
        <filter-name>LoginFilter</filter-name>
        <filter-class>com.student.filter.LoginFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>LoginFilter</filter-name>
        <url-pattern>/pages/student/*</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>LoginFilter</filter-name>
        <url-pattern>/pages/users/update.jsp</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>LoginFilter</filter-name>
        <url-pattern>/index.jsp</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>LoginFilter</filter-name>
        <url-pattern>/servlet/StudentServlet</url-pattern>
    </filter-mapping>

         然后在以上配置中的所有页面检查session中是否有登陆后存入的信息,如果没有,则跳转至登录页面

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/编程革命者/article/detail/61355
推荐阅读
相关标签
  

闽ICP备14008679号