TruePS
TruePS

Reputation: 523

doFilter method is not running?

This is login.jsp page

  <!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>

This is 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>");
  }
   }

This is my filter which is mapped in filter in we.xml as this 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");
   }

   }

This is my web.xml file

  <?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() and destroy() method are running but doFilter method is not running?

Upvotes: 0

Views: 2215

Answers (1)

Sanjay Rabari
Sanjay Rabari

Reputation: 2081

In your doFilter method you should call the next filter in the chain:

chain.doFilter(request, response);

Without this your request will not be processed further after your filter.

Also,I suggest make change in web.xml file

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

Upvotes: 2

Related Questions