0

I've created a abstract base class Page that figures out how to construct a dynamic web page. I'm trying to come up with a good way to generate a Page based off the GET request that comes in as a HttpServletRequest. For example...

public class RootServlet extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) {

        Page page = Page.generatePage(request);

        // do stuff with page and write back response
    }
}

In the generatePage() method, I somehow have to figure out what page is being requested, build the correct page, and then return an instance of it. But I'm not sure how to do this well... for example, I need to handle these kinds of URLs coming in:

http://example.com/       : build the default home page
http://example.com/ab123  : build the page corresponding to the given token "ab123"
http://example.com/about/ : build the "about" page
http://help.example.com/  : build the "help" page

Each of these "pages" extend the abstract base class Page so they know how to build themselves, but I'm not sure how to determine that the AboutPage needs to be built, or the HelpPage, as opposed to the default HomePage.

I'm using Apache Velocity as the template engine, so these Page objects really contain only the important information needed to generate that page, like which styles and scripts to use, and the relevant content to be displayed on the page.

I would think there are better ways to do this than to look at the end of the URL and see if "about" is a substring to build the AboutPage, for example. Any suggestions?

Hristo
  • 45,559
  • 65
  • 163
  • 230

2 Answers2

2

There are dozens of off the shelf tools frameworks that do this for you. In the very least I'd suggest Spring MVC which will work with velocity.

Rick Mangi
  • 3,761
  • 1
  • 14
  • 17
  • 1
    There are others if you don't like spring: http://velocity.apache.org/engine/devel/webapps.html just don't reinvent the wheel ;-) – Rick Mangi May 24 '12 at 20:11
  • I definitely don't want to reinvent the wheel, which is why I came here ;) I've already taken advantage of the link you've shared here... I guess there is something I'm missing/misunderstanding – Hristo May 24 '12 at 20:14
  • What are you missing/misunderstanding? At the end of the day *something* has to look at the url and decide how to route it. These frameworks all handle some basic stuff: routing urls to controllers/views is one of those. Which one you choose is based on your requirements and personal preference. They're all pretty good. Spring MVC is probably the most popular because it's very powerful but it has a steep learning curve. Struts is also very popular and easy to use. I haven't used the others on that page... – Rick Mangi May 24 '12 at 20:31
  • What I'm missing is Struts :D At the writing of this post, I didn't know about Velocity Struts and was going to do the "look at URL and decide what to do" logic myself... but it seems like that is already taken care of by these frameworks. – Hristo May 24 '12 at 20:42
1

Spring MVC has a great way to deal with this kind of stuff using controllers with annotated methods to handle the specific pattern that you want.

They have a great example application here:

https://github.com/SpringSource/spring-mvc-showcase

Anyway, it is not a good practice to build your pages using java code.

Eduardo Andrade
  • 946
  • 7
  • 10
  • When I say "build" a page, I mean each `Page` object generates the appropriate `VelocityContext` that will be used to populate a template. So do you still think this is bad practice? I'll definitely check out Spring MVC though! – Hristo May 24 '12 at 20:07