Saturday, January 31, 2026
HomeLanguagesJavaServlet – Authentication Filter

Servlet – Authentication Filter

Servlets are the Java programs that run on the Java-enabled web server or application server. They are used to handle the request obtained from the webserver, process the request, produce the response, then send a response back to the webserver.

Authentication Filter In Servlets 

Authentication may be done in the filter. Here, we’ll verify the user’s password in the ServletFilter class; if the password is “neveropen”, the request will be sent to the Gfg servlet; otherwise, an error message will be displayed.

Implementation: Let’s look at a simple example of utilizing a filter to authenticate a user. We’ve produced four files here:

  • index.html
  • Gfg.java
  • ServletFilter.java
  • web.xml

File: index.html

HTML




<form action = "servlet1">
  
               Name: < input type = "text" name = "name" / > < br / > < br / >
  
                                    Password: < input type = "password" name = "password" / > < br / > < br / >
  
                                            <input type = "submit" value = "login">
  
                                                    < / form >


Example 1-A:

Java




// Java Program to Illustrate ServletFilter Class
  
// Importing required classes
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
  
// Class
// Implementing Filter class
public class ServletFilter implements Filter {
  
    public void init(FilterConfig arg0)
        throws ServletException
    {
    }
  
    public void doFilter(ServletRequest req,
                         ServletResponse resp,
                         FilterChain chain)
        throws IOException, ServletException
    {
  
        PrintWriter out = resp.getWriter();
  
        String password = req.getParameter("password");
  
        if (password.equals("neveropen")) {
  
            // Sending request to next
            chain.doFilter(req, resp);
        }
  
        // Password incorrect
        else {
            out.print("username or password is wrong");
            RequestDispatcher rd
                = req.getRequestDispatcher("index.html");
            rd.include(req, resp);
        }
    }
    public void destroy() {}
}


Example 1-B:

Java




// Java Program to Illustrate GFG Class
  
// Importing required classes
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
  
// Class
// Derived from HttpServlet class
public class GFG extends HttpServlet {
  
    // Getting request response
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws ServletException, IOException
    {
  
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
  
        out.print("welcome to GEEKSFORGEEKS");
  
        // Closing connections to
        // avoid any memory leakage
        out.close();
    }
}


File: web.xml

XML




<web-app>  
 <servlet>  
    <servlet-name>Gfg</servlet-name>  
    <servlet-class>Gfg</servlet-class>  
  </servlet>  
    
  <servlet-mapping>  
    <servlet-name>Gfg</servlet-name>  
    <url-pattern>/servlet1</url-pattern>  
  </servlet-mapping>  
      
 <filter>  
  <filter-name>f1</filter-name>  
  <filter-class>ServletFilter</filter-class>  
  </filter>  
  <filter-mapping>  
  <filter-name>f1</filter-name>  
  <url-pattern>/servlet1</url-pattern>  
  </filter-mapping>  
      
</web-app>


Output:

Click on the login button, if the password is correct then the above message will be shown to the user.

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32478 POSTS0 COMMENTS
Milvus
123 POSTS0 COMMENTS
Nango Kala
6849 POSTS0 COMMENTS
Nicole Veronica
11978 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12066 POSTS0 COMMENTS
Shaida Kate Naidoo
6987 POSTS0 COMMENTS
Ted Musemwa
7222 POSTS0 COMMENTS
Thapelo Manthata
6934 POSTS0 COMMENTS
Umr Jansen
6917 POSTS0 COMMENTS