使用 jsp/servlet 清理 URL?

发布于 2024-09-15 01:25:35 字数 57 浏览 11 评论 0原文

我计划使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

沉鱼一梦 2024-09-22 01:25:35

您可以尝试使用 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.

关于从前 2024-09-22 01:25:35

在充当前端控制器的 servlet 中使用 HttpServletRequest#getPathInfo()。

没有任何简单验证的启动示例:

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

这将在例如 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:

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:

小ぇ时光︴ 2024-09-22 01:25:35

使用URLRewriteFilter或者你可以自己编写它,如果你知道如何使用部署描述符和过滤器,那就很简单了。
例如,您有一个 Servlet,它根据请求参数响应内容,例如 a.com?cat=book&post=java(调用它showContent servlet
并且您想将 url 重写为 a.com/book/java
所以你应该创建一个过滤器:
过滤器名称:调度员
映射: /*

在该过滤器中,您应该处理字符串 "/book/java" 来生成 catpost 变量的数据。然后只需将其转发到 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 the showContent servlet to handle the request.

倒带 2024-09-22 01:25:35

我使用 JSTL;标签

I use the JSTL <c:url> tag

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文