Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<ol> <li>Implement <a href="http://docs.oracle.com/javaee/6/api/javax/servlet/Filter.html" rel="noreferrer"><code>javax.servlet.Filter</code></a>.</li> <li>In <a href="http://docs.oracle.com/javaee/6/api/javax/servlet/Filter.html#doFilter%28javax.servlet.ServletRequest,%20javax.servlet.ServletResponse,%20javax.servlet.FilterChain%29" rel="noreferrer"><code>doFilter()</code></a> method, cast the incoming <a href="http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html" rel="noreferrer"><code>ServletRequest</code></a> to <a href="http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html" rel="noreferrer"><code>HttpServletRequest</code></a>.</li> <li>Use <a href="http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#getRequestURI%28%29" rel="noreferrer"><code>HttpServletRequest#getRequestURI()</code></a> to grab the path.</li> <li>Use straightforward <a href="http://docs.oracle.com/javase/6/docs/api/java/lang/String.html" rel="noreferrer"><code>java.lang.String</code></a> methods like <a href="http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#substring%28int%29" rel="noreferrer"><code>substring()</code></a>, <a href="http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#split%28java.lang.String%29" rel="noreferrer"><code>split()</code></a>, <a href="http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#concat%28java.lang.String%29" rel="noreferrer"><code>concat()</code></a> and so on to extract the part of interest and compose the new path.</li> <li>Use either <a href="http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getRequestDispatcher%28java.lang.String%29" rel="noreferrer"><code>ServletRequest#getRequestDispatcher()</code></a> and then <a href="http://docs.oracle.com/javaee/6/api/javax/servlet/RequestDispatcher.html#forward%28javax.servlet.ServletRequest,%20javax.servlet.ServletResponse%29" rel="noreferrer"><code>RequestDispatcher#forward()</code></a> to forward the request/response to the new URL (server-side redirect, not reflected in browser address bar), <strong>or</strong> cast the incoming <a href="http://docs.oracle.com/javaee/6/api/javax/servlet/ServletResponse.html" rel="noreferrer"><code>ServletResponse</code></a> to <a href="http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletResponse.html" rel="noreferrer"><code>HttpServletResponse</code></a> and then <a href="http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletResponse.html#sendRedirect%28java.lang.String%29" rel="noreferrer"><code>HttpServletResponse#sendRedirect()</code></a> to redirect the response to the new URL (client side redirect, reflected in browser address bar).</li> <li>Register the filter in <code>web.xml</code> on an <code>url-pattern</code> of <code>/*</code> or <code>/Check_License/*</code>, depending on the context path, or if you're on Servlet 3.0 already, use the <a href="http://docs.oracle.com/javaee/6/api/javax/servlet/annotation/WebFilter.html" rel="noreferrer"><code>@WebFilter</code></a> annotation for that instead.</li> </ol> <p>Don't forget to add a check in the code if the URL <em>needs</em> to be changed and if <em>not</em>, then just call <a href="http://docs.oracle.com/javaee/6/api/javax/servlet/FilterChain.html#doFilter%28javax.servlet.ServletRequest,%20javax.servlet.ServletResponse%29" rel="noreferrer"><code>FilterChain#doFilter()</code></a>, else it will call itself in an infinite loop.</p> <p>Alternatively you can also just use an existing 3rd party API to do all the work for you, such as <a href="http://tuckey.org/urlrewrite/" rel="noreferrer">Tuckey's UrlRewriteFilter</a> which can be configured the way as you would do with Apache's <code>mod_rewrite</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload