Skip to content
Related Articles

Related Articles

Servlet – Authentication Filter

View Discussion
Improve Article
Save Article
  • Last Updated : 16 Feb, 2022

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 “geeksforgeeks”, 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
  • web.xml

File: index.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 Program to Illustrate ServletFilter Class
// Importing required classes
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("geeksforgeeks")) {
            // 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 Program to Illustrate GFG Class
// Importing required classes
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
        PrintWriter out = response.getWriter();
        out.print("welcome to GEEKSFORGEEKS");
        // Closing connections to
        // avoid any memory leakage

File: web.xml




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

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!