0

I have been following the netbeans ecommerce tutorial and they introduced a ControllerServlet which they setup to deal with multiple URLs.

However the way they do this is basically to have a big if then else if statement:

    String userPath = request.getServletPath();

    // if category page is requested
    if (userPath.equals("/category")) {
        // TODO: Implement category request

    // if cart page is requested
    } else if (userPath.equals("/viewCart")) {
        // TODO: Implement cart page request

        userPath = "/cart";

    // if checkout page is requested
    } else if (userPath.equals("/checkout")) {
        // TODO: Implement checkout page request

    // if user switches language
    } else if (userPath.equals("/chooseLanguage")) {
        // TODO: Implement language request

    }

Is this really the recommended way of handling multiple URLs in a single servlet? This approach seems horrible and difficult to manage.

Or am I missing some obvious way of handling this cleanly?

Sam Holder
  • 32,535
  • 13
  • 101
  • 181
  • This is indeed horrible. A real decent front controller is however much more complex. They apparently wanted to keep it simple for starters. I would for this tutorial rather have used separate servlets (since Servlet 3.0 it's really easy to register servlets as standalone mini-controllers by `@WebServlet` annotation without any XML configuration), instead of giving a misleading introduction into "Front Controller" design. See also this related question/answer: http://stackoverflow.com/questions/3541077/design-patterns-web-based-applications – BalusC May 11 '12 at 18:26
  • @BalusC thanks. I agree that they should have just done one servlet per url in the samples, and the answer linked reconfirmed my initial ideas about addressing the issues. Much appreciated. – Sam Holder May 12 '12 at 09:58

2 Answers2

1

You should use an MVC framework like Stripes or Spring MVC, which handles this for you (and much more). In those frameworks, you configure a single servlet to intercept all the requests to a given URL pattern (*.action for example), and this servlet dispatches to appropriate actions based on annotations and/or naming conventions and/or configuration.

Sam Holder
  • 32,535
  • 13
  • 101
  • 181
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Thanks. yes I am aware of Spring MVC (which I agree is a much better way of handling it) but this is for a course in which were taught to use servlets, so I really wanted to know what the best practice was for plain vanilla servlets. – Sam Holder May 12 '12 at 10:01
  • If you use plain servlets, then have one servlet per URL (or group of URLs), or reimplement your own MVC framework. But there are already dozens of them, so I would go this way. – JB Nizet May 12 '12 at 10:04
  • Yes thanks. I'll probably implement a single servlet which has an AbstractFactory to generate a handler for the particular URL rather than take a dependency on a 3rd party library, but point out in the report for my coursework that reinventing this wheel is probably a bad idea and point out the available frameworks which could make this simpler. Thanks for the input. – Sam Holder May 12 '12 at 10:17
0

You could use a ServletFilter and configure your web.xml in the following manner to redirect the client requests to the appropriate destination controller.

The would make keep changing for the different user-paths from your code snippet above.

Using this approach you could send multiple urls to the same(or multiple) filters based on your requirement.

<filter>
  <filter-name>My Filter</filter-name>
  <filter-class>com.project.my.MYclass</filter-class>
</filter>


<filter-mapping>
  <filter-name>My Filter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>
shayan ray
  • 321
  • 1
  • 3
  • I don't see how this is any different than what I currently have, which is annotations on a class which defines which urls it will handle, but all urls are still handled by the same method in the class? which method in the specified class (`com.project.my.MYclass`) will be called when the filter matches – Sam Holder May 12 '12 at 12:28
  • I did not see the annotations in the link (or code snippet) you have provided above so it appeared to me that a simple if and else condition blocks were used to redirect the url. If it is the same class which all the requests then you probably need a Factory pattern in place for the implementation to promote low coupling. – shayan ray May 12 '12 at 16:18