使用 jsp/servlet 清理 URL?
我计划使用 jsp 和 servlet 制作一个 CMS。谁能告诉我如何使用这种技术实现干净的网址?
I am planning to make a CMS using jsp and servlets. Could anyone tell me how to implement clean urls using this technologies?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以尝试使用 urlrewritefilter:http://code.google.com/p/urlrewritefilter/。这使用 servlet 过滤器和 xml 文件来允许您的应用程序拥有干净的 url。干净 url 的构建将由您自己负责。
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.
在充当前端控制器的 servlet 中使用 HttpServletRequest#getPathInfo()。
没有任何简单验证的启动示例:
这将在例如 http://example.com 上发出请求/context/servlet/foo/bar 显示
/WEB-INF/foo/bar.jsp
文件。 JSP 文件应放置在/WEB-INF
中,以防止直接访问。另请参阅:
Make use of
HttpServletRequest#getPathInfo()
in the servlet which is acting as front controller.Kickoff example without any trivial validation:
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:
使用
URLRewriteFilter
或者你可以自己编写它,如果你知道如何使用部署描述符和过滤器,那就很简单了。例如,您有一个 Servlet,它根据请求参数响应内容,例如 a.com?cat=book&post=java(调用它
showContent servlet
)并且您想将 url 重写为
a.com/book/java
所以你应该创建一个过滤器:
过滤器名称:调度员
映射: /*
在该过滤器中,您应该处理字符串
"/book/java"
来生成 cat 和 post 变量的数据。然后只需将其转发到showContent 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 theshowContent servlet
to handle the request.我使用 JSTL;标签
I use the JSTL <c:url> tag