Java web xml filter

From John Freier
Jump to: navigation, search

This will allow you to edit income request.

UrlFilter.java

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.*;

public class UrlFilter implements Filter
{
        private FilterConfig filterConfig;

        public void init(FilterConfig filterConfig) throws ServletException{
                this.filterConfig=filterConfig;
        }

        public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain) throws IOException,ServletException {
                HttpServletRequest httpRequest=(HttpServletRequest)request;
                HttpServletResponse httpResponse=(HttpServletResponse)response; 

                String incomingUrl=httpRequest.getRequestURI();
                System.out.println("PATH:" + incomingUrl);

                if(incomingUrl.endsWith(".html"))
                {
                        String replaceUrl = incomingUrl.replace(".html", ".gsp"); 

                        int index = replaceUrl.substring(1, replaceUrl.length()).indexOf("/");
                        String news = replaceUrl.substring(index+1, replaceUrl.length());
                        System.out.println("New ModURL:" + news); 
  

                        RequestDispatcher request_Dispatcher=request.getRequestDispatcher(news);
                        request_Dispatcher.forward(request,response);
                }

                chain.doFilter(request,response);

        }

        public void destroy(){
        }
}


web.xml

    <filter>
        <filter-name>Url_Filter</filter-name>
        <filter-class>com.johnfreier.UrlFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>Url_Filter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>