Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>i see 3 ways: filter, basic servlet(all requests via main servlet) or "servlet-mapping"</p> <p><strong>filter</strong></p> <p>This example rewrite url <pre>http://example.org/&lt;int value&gt;</pre> to <pre>http://example.org/user?id=&lt;int value&gt;</pre> i.e</p> <pre>http://example.org/1234 -> http://example.org/user?id=1234</pre> <ol> <li>determinate filter in web.xml</li> </ol> <blockquote> <pre><code>&lt;filter&gt; &lt;filter-name&gt;Router&lt;/filter-name&gt; &lt;filter-class&gt;org.example.Router&lt;/filter-class&gt; &lt;init-param&gt; &lt;param-name&gt;param1&lt;/param-name&gt; &lt;param-value&gt;valueOfparam1&lt;/param-value&gt; &lt;/init-param&gt; &lt;/filter&gt; &lt;filter-mapping&gt; &lt;filter-name&gt;Router&lt;/filter-name&gt; &lt;url-pattern&gt;/*&lt;/url-pattern&gt; &lt;/filter-mapping&gt; </code></pre> </blockquote> <p>in init-param section you can specify route rules</p> <ol> <li><p>filter class</p> <pre><code>public class Router implements javax.servlet.Filter { private static final Pattern REWRITE_PATTERN = Pattern.compile("(^[1-9]\\d*)$"); @Override public void destroy() { } @Override public void doFilter(ServletRequest req, ServletResponse res, FilterChain fc) throws IOException, ServletException { //this method calling before controller(servlet), every request HttpServletRequest request = (HttpServletRequest) req; String url = request.getRequestURL().toString(); String number = url.substring(url.lastIndexOf("/")).replace("/", ""); Matcher m = REWRITE_PATTERN.matcher(number); if(m.find()) { RequestDispatcher dispatcher = request.getRequestDispatcher("user?id=" + m.group(1)); dispatcher.forward(req, res); } else { fc.doFilter(req, res); } } @Override public void init(FilterConfig fc) throws ServletException { //here you may read params from web.xml }} </code></pre></li> </ol> <p><strong>basic sevlet</strong></p> <pre><code>public class BasicServlet extends HttpServlet { //route rules here, and rewrite } </code></pre> <p><strong>servlet-mapping</strong></p> <pre><code>&lt;servlet-mapping&gt; &lt;servlet-name&gt;UserServlet&lt;/servlet-name&gt; &lt;uri-mapping&gt;/user/*&lt;/uri-mapping&gt; &lt;/servlet-mapping&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;PostServlet&lt;/servlet-name&gt; &lt;uri-mapping&gt;/post/*&lt;/uri-mapping&gt; &lt;/servlet-mapping&gt; </code></pre> <ul> <li>filter - more flexible and not require serious modify working code</li> <li>basic servlet - flexible, require change you code</li> <li>servlet mapping - simply, not flexible(for specific route rules)</li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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