简体   繁体   中英

Filter servlet not working with glassfish in netbeans

This is my web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

     <filter>
            <display-name>MyFilter</display-name>
            <filter-name>MyFilter</filter-name>
            <filter-class>AB_DB.MyFilter</filter-class>
          </filter>
          <filter-mapping>
            <filter-name>MyFilter</filter-name>
            <url-pattern>/MyFilter</url-pattern>
          </filter-mapping>
          <servlet>
            <description></description>
            <display-name>Profile</display-name>
            <servlet-name>Profile</servlet-name>
            <servlet-class>AB_DB.Profile</servlet-class>
          </servlet>
          <servlet-mapping>
            <servlet-name>Profile</servlet-name>
            <url-pattern>/Profile</url-pattern>
          </servlet-mapping>
        </web-app>

This is the filter servlet I've tried

package AB_DB;


import java.io.IOException;
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.http.HttpServletRequest;

public class MyFilter implements Filter {

    public MyFilter() {
    }

    public void destroy() {
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        String requri = ((HttpServletRequest) request).getRequestURI().substring(((HttpServletRequest) request).getContextPath().length() + 1);
        System.out.println(requri);
        //if request uri starts with user/ then system will forward the request to Profile servlet
        if (requri.startsWith("user/")) {
            //get userid from request uri
            String id = requri.substring(5);
            System.out.println(id);

            //set attribute "user" with userid value
            request.setAttribute("user", id);

            //forward the request to Profile servlet
            request.getRequestDispatcher("/Profile")
                    .forward(request, response);
        } else {
            chain.doFilter(request, response);
        }
    }

    public void init(FilterConfig fConfig) throws ServletException {
    }

}

However in the GlassFish 4 server log, there is no errors at all when running the project. The URI isnt even being printed out with 'System.out.println(id);' so I assume that the filter isnt even being injected.

Can anyone see any issues with my code? The sevlet Profile isn't being ran

You have mapped oyur filter to /MyFilter

<url-pattern>/MyFilter</url-pattern>

and that doesn't match with URL you are requesting

/user/Surname1993

so your filter won't get invoked

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM