Categories
Computing Java Web Development WebSphere

Setting up Spring MVC welcome page without rewrites or redirection

So you have a Spring MVC web application and want to make sure that when the user enters a URL such as
http://www.myawesomedomain.com he/she will see the home page of your application. How do you get Spring MVC’s RequestDispatcher servlet, which acts as the traffic cop and then some for your web application, to act as the default URL handler in the eyes of the web application? There are two good ways to do it, and two bad ones. Let’s start with the bad ones, of course:

  • Redirect through meta tag: you add a meta-refresh element to the head element which causes the browser showing the page to go the ‘real’ home page url, such as /index.do
  • You use JavaScript to force the page to redirect – this will also work but forces you to rely on client-side processes to get stuff done. For example: window.location="index.do" inside an index.html file.

Both are sub-prime because search engines are not too keen to follow such redirects, often time avoiding JavaScript altogether.

So what are the good ways:

  • Using Apache’s mod-rewrite to mask the URL of your application, but that assumes you have a web server in front of your application
  • Using the servlet API – which is what you read below

So how do you do it?

  1. In the application’s web.xml file map the RequestDispatcher servlet to handle requests for index.htm, aside from handling the extension of your liking, say, *.do. You do this through the servlet-mapping element. This should look like this:

    <servlet -mapping>
        </servlet><servlet -name>requestDispatcher</servlet>
        <url -pattern>*.do</url>
     </servlet -mapping>
      <servlet -mapping>
        </servlet><servlet -name>requestDispatcher</servlet>
        <url -pattern>/index.htm</url>
     </servlet -mapping>  
    
  2. Also in web.xml, set up a welcome file that may be requested by a browser when a user does not enter a specific page request, e.g. www.domain.com. Most browsers at a minimum will automatically request, on behalf of the user, index.html and index.htm.

So choose one, say index.htm and set the <welcome-file> element to look something like this:

<welcome-file-list>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>

This will make your application by default make requests for ‘/’ go to index.htm.

  • The last and final step is to map the url index.htm to the controller for the application’s home page. You do this in a Spring MVC beans xml file when specifying the URL mapper.
    For example:

    <bean id="unAuthenticatedUrlMapping"
            class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
          <props>
            <prop key="/index.do">homePageController</prop>
            <prop key="/index.htm">homePageController</prop>
          </props>
        </property>
      </bean>
    

    This way the RequestDispatcher will know which control is actually going to load the information that will be displayed by the view for the home page.

  • And that’s it. No redirection. No nonsense.

    Share
    Categories
    General

    Silverlight premature for prime time at mlb.com

     

    Having learned my lesson with mlb.com a couple of years ago – giving them a limited-duration, limited-amount, one-time credit card to avoid automatic renewal charges, I decided to again subscribe to their audio broadcast service. I enjoy listening to baseball radio characters from other cities, although the older announcers seem to be fading away with their unique styles. I did grumble for a year that they switched broadcast station in the Boston area to a station with a weak signal so people will *have* to pay for the online service. Sadly, that’s life. Anyway…

    mlb.com is a bleeding edge website and I do not say it at all in a negative fashion. What they did with their Gameday application in Flash, where you can get scores, stats and more in live animation, preceded most major data-driven rich Internet applications. They also rightly moved from Real Audio, which was just bad, to Microsoft’s embedded media player for their live broadcasts.

    This year, they chose to move to a new technology again, opting to switch to Microsoft’s Flash-killer, Silverlight, as the mechanism to broadcast online. Problem is, on multiple computers, in both Firefox and Internet Explorer, their Silverlight player fails. While you can still opt to use the Windows Media Player, you are strongly encouraged to switch to Silverlight. Still, there is one big problem: the Silverlight player plainly does not work. Better yet, once you switch, there is no way to go back to the old Windows Media Player, despite instructions in their FAQ that refer to options that do not exist.

    Contacting mlb.com’s support by email is a joke. You send in an email, get an immediate response that they will answer you as soon as possible, only to well, never be responded to. I am waiting for a month. The solution, though, is easy. Just uninstall Silverlight. Removing the cookies from the site does nothing to correct the issue, but outright removal of Silverlight does allow you to choose to use Windows Media Player again and you can get your money’s worth. A bit absurd but with mlb.com, well, you need to do preventative and creative actions, rather than rely on them to give you your money’s worth.

    They suck. They deserve bad publicity. So there you go.

    Share
    Categories
    General

    Fight bulk mailers back!

    A creative way to avenge the bulk mail deluge in your mailbox.

    Share
    Share