开发者

A servlet or filter that dynamically maps /xxx/yyy/zzz to class XxxYyyZzz.java

开发者 https://www.devze.com 2023-02-07 00:35 出处:网络
I want to write a servlet or filter that automatically maps the url /xxx/yyy/zzz to class XxxYyyZzz.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 f开发者_如何学Goilter 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.


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.


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


You can use Stripes framework with its default NameBasedActionResolver config.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号