Created
December 16, 2016 03:15
-
-
Save vincentes/84ec0378dcbb14c8fbef5de272610ecf to your computer and use it in GitHub Desktop.
Filters "/file.jsp" to "/file"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.bermudez.filter; | |
import javax.servlet.*; | |
import javax.servlet.annotation.WebFilter; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import java.io.IOException; | |
import java.util.HashSet; | |
@WebFilter("*") | |
public class URLFilter implements Filter { | |
HashSet<String> invalidExts = new HashSet<String>(); | |
public void init(FilterConfig filterConfig) throws ServletException { | |
} | |
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { | |
HttpServletRequest req = (HttpServletRequest) servletRequest; | |
HttpServletResponse res = (HttpServletResponse) servletResponse; | |
String context = req.getContextPath(); | |
String uri = req.getRequestURI(); | |
String query = req.getQueryString(); | |
if(!uri.endsWith("/") && uri.length() > context.length()) { | |
if(uri.endsWith(".jsp")) { | |
String path = uri.substring(0, uri.lastIndexOf(".jsp")); | |
if(query != null && query.length() > 0) { | |
path += "?" + query; | |
} | |
res.sendRedirect(path); | |
} else { | |
filterChain.doFilter(req, res); | |
} | |
} else { | |
filterChain.doFilter(req, res); | |
} | |
} | |
public void destroy() { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment