Skip to content

Instantly share code, notes, and snippets.

@froop
Created February 4, 2012 05:54
Show Gist options
  • Save froop/1735707 to your computer and use it in GitHub Desktop.
Save froop/1735707 to your computer and use it in GitHub Desktop.
[Java][Servlet] REST風URL化 /hoge?id=123 -> /123/hoge してid引継ぎをなくす例(UrlRewriteFilter版)
<html>
<body>
<a href="rewrite/123/hoge?param1=abc">GET</a>
<form method="POST" action="rewrite/123/hoge">
<input type="text" name="param1">
<input type="submit" value="POST" />
</form>
</body>
</html>
<rule>
<from>^/rewrite/(\d+)(?:/(.*)$)?</from>
<to type="forward">/rewrite/$2?id=$1</to>
</rule>
@WebServlet({"/rewrite/hoge", "/rewrite/fuga"})
public class UrlRewriteTestServlet extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
Writer writer = response.getWriter();
writer.write("<html>");
writer.write(request.getRequestURI() + "<br/>");
writer.write("id=" + request.getParameter("id") + "<br/>");
writer.write("param1=" + request.getParameter("param1") + "<br/>");
writer.write("<a href='fuga'>fuga</a>");
writer.write("</html>");
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
<!-- http://tuckey.org/urlrewrite/ -->
<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment