0

I have a servlet, a filter and a login.jsp. I fill the username and pasword and then click the login button and the filter isn't called, only the servlet is called. The servlet and filter are in different packages and I see this is the problem. If I put servlet and filter in the same package the filter is called successfully. But I want to use them in different packages. What should I do? Thanks in advance!

enter image description here

Filter:

package log.reg.myfilter;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;

@WebFilter("/LoginFilter")
public class LoginFilter implements Filter {

    public void destroy() {
        // TODO Auto-generated method stub
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 
            throws IOException, ServletException {

        PrintWriter out = response.getWriter(); 

        HttpServletRequest req = (HttpServletRequest) request; 
        String username = req.getParameter("username"); 
        String pass = req.getParameter("password1"); 

        System.out.println(username); 

        if ((username.length() > 6) && (pass.length() > 3)) {

            System.out.println("In filter");  
            chain.doFilter(request, response);
        }
        else
            out.println("Invalid Input");
    }

    public void init(FilterConfig fConfig) throws ServletException {
        // TODO Auto-generated method stub
    }

}

Servlet:

package log.reg;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/firstServlet")
public class FirstServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {

        System.out.println("In servlet");

        PrintWriter out = response.getWriter();

        String username = request.getParameter("username"); 
        out.println("Welcome " + username); 
    }

}

login.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <form action="firstServlet" method="get">
        <table style="background-color: lightgreen; margin-left: 20px; margin-top: 20px">
            <tr>
                <td>
                    <h3 style="color: red">Login Page!!!</h3>
                </td>
            </tr>
            <tr>
                <td>UserName : </td>
                <td><input type="text" name="username"></td>
            </tr>
            <tr>
                <td>Password : </td>
                <td><input type="password" name="password1"></td>
            </tr>
            <tr>
                <td><input type="submit" name="submit" value="login"></td>
            </tr>
        </table>
    </form>
</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>Servlet ex29 - filter</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

2 Answers2

0

The value() of @WebServlet and @WebFilter don't have the same meaning.
The first one specifies the URL patterns of the servlet (that is its URL mapping) while the second one specifies the URL patterns to which the filter applies.

But in both cases you specified as name the simple name of the underlying class.
For the servlet, @WebServlet("/firstServlet") makes sense but annotating your Filter with @WebFilter("/LoginFilter") is not what you are looking for as you don't want to filter only the "/LoginFilter" URL.

For example to filter any requested URL you could specify : @WebFilter("/*")

davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • Thanks for reply! I want to add more servlets and jsp pages. What should I do if I want to filter all the servlets and jsp except login.jsp? –  Jun 30 '18 at 13:32
  • 1
    You are welcome. You could put all protected resources (jsp) and all protected servlet mappings in a specific base path , for example `/private`. Then apply the filter on this path such as `@WebFilter("/private/*")`. And to allow the login to be accessible, place it at a place not considered by the filter such as the root of the context : "/". – davidxxx Jun 30 '18 at 13:37
  • And how can I make this /private base path? In web.xml or where can I do it? –  Jun 30 '18 at 14:01
  • 1
    In the mapping for the servlets such as `@WebServlet("/private/firstServlet")` and move your private static resources in the `WEB-INF/private` folder. – davidxxx Jun 30 '18 at 14:24
0

A Servlet filter is an object that can intercept HTTP requests targeted at your web application. As I see here, you have not associated filter with servlet. You have to tell the url pattern you want to intercept/filter.

Note: Filter won't be executed independently it works with Servlet. So You have to associated Filter with Servlets.

If you have to associate with FirstServlet then use below one.

  @WebFilter("/firstServlet")

If you want to filter each request you can use /*

  @WebFilter("/*")
Gaurav Srivastav
  • 2,381
  • 1
  • 15
  • 18