1

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.

Basil Musa
  • 8,198
  • 6
  • 64
  • 63
  • Related: http://stackoverflow.com/questions/3541077/design-patterns-web-based-applications/ – BalusC Jan 24 '11 at 22:39

3 Answers3

2

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.

maaartinus
  • 44,714
  • 32
  • 161
  • 320
  • [`HttpServletRequest#getPathInfo()`](http://download.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#getPathInfo%28%29), it is. – BalusC Jan 24 '11 at 22:40
  • Yes, but I need the web.xml configuration for the servlet and how it will handle routing unmapped classes to a JSP page. – Basil Musa Jan 25 '11 at 07:08
  • For now, your answer is correct, but I still need to map a servlet to /* and still be able to process JSPs. I'll have this asked in a different question. – Basil Musa Jan 25 '11 at 07:30
0

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

gbvb
  • 866
  • 5
  • 10
0

You can use Stripes framework with its default NameBasedActionResolver config.

Amol Katdare
  • 6,740
  • 2
  • 33
  • 36