2

My WAR is structure is as follows:

my-web-app.war/
    views/
        index.html
        blah.html
    META-INF/
        MANIFEST.MF
    WEB-INF/
        web.xml
        lib/
            <!-- Dependencies -->
        classes/
            org.me.mywebapp.controllers/
                MyController.class
            <!-- Other packages/classes as well -->

I would like to configure web.xml so that when the WAR is deployed locally it's index.html page can be accessed by going to http://localhost/my-web-app/index.html.

Here's what I have so far:

<?xml version="1.0" encoding="UTF-8"?>
<web-app
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">

    <!-- The display name of this web application -->
    <display-name>My Web App</display-name>

    <listener>
        <listener-class>
            org.me.mywebapp.context.ContextImpl
        </listener-class>
    </listener>
</web-app>

How do I configure this URL to view mapping? Thanks in advance!

IAmYourFaja
  • 55,468
  • 181
  • 466
  • 756

3 Answers3

6

You could map your servlets like this

<servlet>
    <servlet-name>controller</servlet-name>
    <servlet-class>org.me.mywebapp.controllers.MyController</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>controller</servlet-name>
    <url-pattern>index.html</url-pattern>
</servlet-mapping>

<servlet>
    <servlet-name>controller2</servlet-name>
    <servlet-class>org.me.mywebapp.controllers.OtherController</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>controller2</servlet-name>
    <url-pattern>blah.html</url-pattern>
</servlet-mapping>

And if you want to show view/blah.html as /blah.html, in the controller you just dispatch the request to the apropriate views/*.html or jsp or anything you want.

EDIT: As you requested: You can dispatch the request to another page inside the servlet like this:

RequestDispatcher dispatcher = 
       getServletContext().getRequestDispatcher("/views/blah.html");
dispatcher.forward(request, response);

Although the above code is working, you should probably implement a more "sophisticated" approach inside each servlet to decide to which view you will dispatch, espesially if your application has a lot of controller, views etc. Try reading more about MVC implementations if you haven't done already.

pater
  • 1,211
  • 9
  • 16
  • Thanks for such a great answer @pater (+1)! - Can you provide an example of what you mean by "*in the controller you just dispatch the request to the appropriate views/*.html...*"? I've always used servlet frameworks like Spring so all this "raw servlet" stuff has me a little confused. Thanks again! – IAmYourFaja Aug 06 '12 at 12:59
1

You can use a filter that routes specific requests to view path. See the response : https://stackoverflow.com/a/3593513/221951 Then you decide in filter if a request should be passed to servlet or not.

You can alos try to use Tuckey URL rewrite filter http://tuckey.org/urlrewrite/

Community
  • 1
  • 1
Piotr Gwiazda
  • 12,080
  • 13
  • 60
  • 91
  • @4herpsand7derpsago: check the above link that it will lead you to http://stackoverflow.com/questions/3541077/design-patterns-web-based-applications/3542297#3542297 which explains everythinkg you need. piotr: +1 for referencing an appropriate existing answer – pater Aug 06 '12 at 13:13
-1

You can do this by re-writing the URL in Filter.

Like it is implemented by most of frameworks like Struts, Spring MVC, Tapestry, Wicket, etc

Rahul Agrawal
  • 8,913
  • 18
  • 47
  • 59