Skip to content

Instantly share code, notes, and snippets.

@vincentes
Created December 16, 2016 03:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vincentes/84ec0378dcbb14c8fbef5de272610ecf to your computer and use it in GitHub Desktop.
Save vincentes/84ec0378dcbb14c8fbef5de272610ecf to your computer and use it in GitHub Desktop.
Filters "/file.jsp" to "/file"
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