Clean URLs using jsp/ servlets?

孤者浪人 提交于 2019-12-04 13:08:37

You could try using urlrewritefilter: http://code.google.com/p/urlrewritefilter/. This uses a servlet filter and an xml-file to allow your application to have clean url's. The construction of the clean url's would be your own responsibility.

BalusC

Make use of HttpServletRequest#getPathInfo() in the servlet which is acting as front controller.

Kickoff example without any trivial validation:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    request.getRequestDispatcher("/WEB-INF" + request.getPathInfo() + ".jsp").forward(request, response);
}

This will make a request on for example http://example.com/context/servlet/foo/bar to display the /WEB-INF/foo/bar.jsp file. The JSP files should be placed in /WEB-INF to prevent them from direct access.

See also:

Use URLRewriteFilter or you can write it by yourself, it's quite simple if you know how to use deployment descriptor and filter. For example, you have a servlet that responses content based on request parameter such as a.com?cat=book&post=java (call it showContent servlet) and you want to rewrite the url to a.com/book/java so you should create a filter: filter name: dispatcher mapping: /*

and in that filter, you should handle the string "/book/java" to generate data for cat and post variables. Then just forward it to the showContent servlet to handle the request.

I use the JSTL <c:url> tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!