Clean URLs using jsp/ servlets?

放肆的年华 提交于 2019-12-06 06:33:29

问题


I am planning to make a CMS using jsp and servlets. Could anyone tell me how to implement clean urls using this technologies?


回答1:


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.




回答2:


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



回答3:


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.




回答4:


I use the JSTL <c:url> tag



来源:https://stackoverflow.com/questions/3528303/clean-urls-using-jsp-servlets

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