动态地将 /xxx/yyy/zzz 映射到类 XxxYyyZzz.java 的 servlet 或过滤器

发布于 2024-10-14 01:14:19 字数 789 浏览 1 评论 0原文

我想编写一个 servlet 或过滤器,自动将 url /xxx/yyy/zzz 映射到类 XxxYyyZzz.java。

例如,以下 URL 将映射到以下 java 类:

/comment/add --> CommentAdd.java
/comment/delete --> CommentDelete.java
/comment/view --> CommentView.java
/search --> Search.java
/viewposts --> Viewposts.java

此外,servlet 或过滤器必须符合两个额外要求:

  1. Servlet 或过滤器应具有“/*”的 servlet 映射,我不希望前缀为几个 servlet“/comment/*”、“/search”等。

  2. 也许很困难,但是拥有 /* 的 servlet 映射不应允许它覆盖 JSP 处理。意思是,如果没有找到类,它应该检查 jsp 页面是否存在并运行它。

我想知道如何使用 Servlet API 来完成此操作。请不要向我推荐任何可以完成这项工作的框架。只需给我看一下代码即可。

映射到的类将遵循命令模式,或者可以是 HttpServlet 的子类。在这两种情况下,都应该存在一个类似于“execute(HttpServletRequest request, and HttpServletResponse response)”的方法。一旦访问 URL 并且可能使用单个 servlet 或过滤器找出 java 类,此方法将自动执行。

I want to write a servlet or filter that automatically maps the url /xxx/yyy/zzz to class XxxYyyZzz.java.

For example the following URLs will map to the following java classes:

/comment/add --> CommentAdd.java
/comment/delete --> CommentDelete.java
/comment/view --> CommentView.java
/search --> Search.java
/viewposts --> Viewposts.java

In addition the servlet or filter must comply with two extra requirements:

  1. The servlet or filter should have a servlet mapping of "/*", I dont want a prefix with several servlets "/comment/*", "/search", etc.

  2. Maybe difficult, but having a servlet mapping of /* should not allow it to override the JSP processing. Meaning, if a class is not found, it should check if a jsp page exists and run it.

I want to know how can this be done using the Servlet API. Please don't refer me to any framework that does the job. Just show me the code.

The classes that are mapped to will follow the command pattern or could be a subclass of the HttpServlet. In both cases, a method should exist like "execute(HttpServletRequest request, and HttpServletResponse response)". This method will be automatically executed once the URL is accessed and the java class is figured out possibly using a single servlet or filter.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

千年*琉璃梦 2024-10-21 01:14:19

我不确定我是否明白你的意思。如果我这样做了:

您不需要什么特别的,编写一个映射到“/”的 Servlet,这样它就可以获取所有内容。解析PATH_INFO(现在不知道在Java中如何调用它),使用Class.forName(或使用预先填充的Map),并调用其方法execute。

I'm not sure, if I got what you mean. In case I did:

You need nothing special, write a single Servlet mapped to "/", so it gets everything. Parse the PATH_INFO (don't know now how it gets called in Java), use Class.forName (or use a pre-filled Map), and call its method execute.

瀞厅☆埖开 2024-10-21 01:14:19

这是一个 http://www.tuckey.org/urlrewrite/ 过滤器实现,可能会对您有所帮助。一探究竟。不过我自己还没用过。

Here is a http://www.tuckey.org/urlrewrite/ filter implementation that might help you. Check it out. I have not used it myself though.

咽泪装欢 2024-10-21 01:14:19

您可以使用 Stripes 框架 及其默认 NameBasedActionResolver

You can use Stripes framework with its default NameBasedActionResolver config.

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