I am planning to make a CMS using jsp and servlets. Could开发者_如何学Go anyone tell me how to implement clean urls using this technologies?
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.
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:
- Hidden features of JSP/Servlet
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
精彩评论