doFilter方法没有运行?

时间:2014-04-24 06:22:05

标签: java filter

这是login.jsp页面

  <!DOCTYPE html>
  <html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
    </head>
   <body>
    <form action="checklogin" method="Post">
    <h2>Login tab</h2> <br>
    Login id:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input type="text" name="loginid"><br><br>
    Password:&nbsp;&nbsp;&nbsp;&nbsp;<input type="password" name="password"><br><br>
    <input type="submit" value="Submit">
    </form>
    </body>
    </html>

这是checklogin servlet

   protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
    {
    PrintWriter out=response.getWriter();

  javax.servlet.http.HttpSession session = request.getSession(true);
  String username = (String)request.getParameter("loginid");
  String password = (String)request.getParameter("password");
  session.setAttribute("UserName", username);
  if(username.equals("Prashant") && password.equals("123"))
  {
    response.sendRedirect("admin.jsp");
  }
 else
    {
    out.println("<h2>"+"Sharam aani chahiye  account banaya nahi aur login kar rahe ho"+"<h2>");
  }
   }

这是我的过滤器,它在we.xml中的过滤器中映射为此URL /admin.jsp/*

   public class adminfilter implements javax.servlet.Filter{

   @Override
   public void init(FilterConfig filterConfig) throws ServletException {
   System.out.println("Sawagat nahi karoge init method ka");
  }

    @Override
   public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
     System.out.println("Sawagat nahi karoge doFilter method ka");
}

   @Override
   public void destroy() {
     System.out.println("Sawagat nahi karoge destroy method ka");
   }

   }

这是我的web.xml文件

  <?xml version="1.0" encoding="UTF-8"?>
  <web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee /web-app_3_1.xsd">
    <filter>
    <filter-name>adminfilter</filter-name>
    <filter-class>Filters_Demo.adminfilter</filter-class>
    </filter>
     <filter-mapping>
    <filter-name>adminfilter</filter-name>
    <url-pattern>/admin.jsp/*</url-pattern>
    </filter-mapping>
    <servlet>
    <servlet-name>checklogin</servlet-name>
    <servlet-class>Filters_Demo.checklogin</servlet-class>
    </servlet>
    <servlet-mapping>
    <servlet-name>checklogin</servlet-name>
    <url-pattern>/checklogin</url-pattern>
    </servlet-mapping>
    <session-config>
    <session-timeout>
        30
    </session-timeout>
    </session-config>
    <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    </welcome-file-list>
    </web-app>

init()和destroy()方法正在运行但是doFilter方法没有运行?

1 个答案:

答案 0 :(得分:2)

在你的doFilter方法中,你应该调用链中的下一个过滤器:

chain.doFilter(request, response);

如果不这样做,您的请求将不会在过滤后进一步处理。

另外,我建议在web.xml文件中进行更改

<url-pattern>*.jsp</url-pattern>