Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Servlets and JSPs. A simple request?
- request.getRequestDispatcher("/WEB-INF/content" + request.getPathInfo()).forward(request, response);
- <filter>
- <filter-name>ProcessFilter</filter-name>
- <filter-class>my.filter.ProcessFilter</filter-class>
- </filter>
- <!-- Map all ".jsp" that should go through the filter-->
- <filter-mapping>
- <filter-name>ProcessFilter</filter-name>
- <url-pattern>/content/*.jsp</url-pattern>
- </filter-mapping>
- <!-- If you have Any servlets that needs to go through ProcessFilter -->
- <filter-mapping>
- <filter-name>ProcessFilter</filter-name>
- <servlet-name>MyServlet</servlet-name>
- </filter-mapping>
- public class ProcessFilter extends Filter {
- public final void doFilter(ServletRequest request, ServletResponse response,
- FilterChain filterChain)
- throws ServletException, IOException {
- if (!(request instanceof HttpServletRequest) ||
- !(response instanceof HttpServletResponse)) {
- throw new ServletException("OncePerRequestFilter just supports HTTP requests");
- }
- HttpServletRequest httpRequest = (HttpServletRequest) request;
- HttpServletResponse httpResponse = (HttpServletResponse) response;
- String alreadyFilteredAttributeName = "ALREADY_PROCESSED_BY_PROCESS_FILTER";
- if (request.getAttribute(alreadyFilteredAttributeName) != null) {
- // Proceed without invoking this filter...
- filterChain.doFilter(request, response);
- }
- else {
- // Do invoke this filter...
- request.setAttribute(alreadyFilteredAttributeName, Boolean.TRUE);
- try {
- doFilterInternal(httpRequest, httpResponse, filterChain);
- }
- finally {
- // Remove the "already filtered" request attribute for this request.
- request.removeAttribute(alreadyFilteredAttributeName);
- }
- }
- }
- protected void doFilterInternal(
- HttpServletRequest request, HttpServletResponse response,
- FilterChain filterChain) {
- throws ServletException, IOException
- /*
- *
- *
- * Put your processing logic here
- *
- *
- */
- }
- }
Add Comment
Please, Sign In to add comment