Apache Tomcat 4.1.x: how to get front page without redirect like slashdot.org

Discussion in 'Java' started by Alex Martinoff, Sep 6, 2003.

  1. Hello all,

    Suppose I'm running slashdot.org except coded with Java instead of
    Perl. I want the main page served by a servlet named index but without
    any redirects. So when the user types

    http://slashdot.org/

    into their browser and goes there they won't be redirected to
    http://slashdot.org/index. The real slashdot.org doesn't automatically
    redirect you to index.pl. You just see http://slashdot.org/ in your
    location bar. So, I tried to set this up with Tomcat and have only
    found a partial-solution that isn't completely satisfactory.

    My application's web.xml contains

    <!-- ... -->
    <servlet-mapping>
    <servlet-name>index</servlet-name>
    <url-pattern>/</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
    <servlet-name>article</servlet-name>
    <url-pattern>/article</url-pattern>
    </servlet-mapping>
    <!-- ... -->

    Then, I edited the site-wide web.xml in conf/ so that the default
    servlet is no longer mapped to "/" but instead to the various types my
    site uses like:
    <!-- ... -->
    <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.gif</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.css</url-pattern>
    </servlet-mapping>
    <!-- ... -->

    If I don't do that then my images and css will no longer load. I
    thought just simply making the change I made to my application's
    web.xml would be sufficient, but apparently the meaning of
    servlet-mapping to "/" isn't the intuitive thing of just mapping
    requests for "/" to index and nothing else; it maps all other request
    URIs not given servlet-mappings in those 2 web.xml files to the index
    servlet instead of to the default servlet. That's what servlet-mapping
    to something broader like "/*" or "/**/*" or something should do;
    mapping to just "/" shouldn't give the effect it does. So if I do
    http://localhost:8080/foo then I'll get the index servlet unless
    foo=article or blah.gif, etc. I'd rather get a NOT FOUND message for
    random URIs.

    I suppose editing org.apache.catalina.servlets.DefaultServlet is a
    possibilty, but there HAS to be a better approach! For example, can I
    set up a default servlet that checks if the request is for "/" and if
    it is then pass the request and response to the doGet (or doPost, as
    the case may be) index serlet method? (If it's not for "/" then pass
    the request and response to the usual DefaultServlet method.)

    This is frustrating! Any and all replies really appreciated. Thanks a
    bunch!
    Alex Martinoff, Sep 6, 2003
    #1
    1. Advertising

  2. (Alex Martinoff) wrote in
    news::

    > Hello all,
    >
    > Suppose I'm running slashdot.org except coded with Java instead of
    > Perl. I want the main page served by a servlet named index but without
    > any redirects. So when the user types
    >
    > http://slashdot.org/
    >
    > into their browser and goes there they won't be redirected to
    > http://slashdot.org/index. The real slashdot.org doesn't automatically
    > redirect you to index.pl. You just see http://slashdot.org/ in your
    > location bar. So, I tried to set this up with Tomcat and have only
    > found a partial-solution that isn't completely satisfactory.
    >
    > My application's web.xml contains
    >
    > <!-- ... -->
    > <servlet-mapping>
    > <servlet-name>index</servlet-name>
    > <url-pattern>/</url-pattern>
    > </servlet-mapping>
    >
    > <servlet-mapping>
    > <servlet-name>article</servlet-name>
    > <url-pattern>/article</url-pattern>
    > </servlet-mapping>
    > <!-- ... -->
    >
    > Then, I edited the site-wide web.xml in conf/ so that the default
    > servlet is no longer mapped to "/" but instead to the various types my
    > site uses like:
    > <!-- ... -->
    > <servlet-mapping>
    > <servlet-name>default</servlet-name>
    > <url-pattern>*.gif</url-pattern>
    > </servlet-mapping>
    >
    > <servlet-mapping>
    > <servlet-name>default</servlet-name>
    > <url-pattern>*.css</url-pattern>
    > </servlet-mapping>
    > <!-- ... -->
    >
    > If I don't do that then my images and css will no longer load. I
    > thought just simply making the change I made to my application's
    > web.xml would be sufficient, but apparently the meaning of
    > servlet-mapping to "/" isn't the intuitive thing of just mapping
    > requests for "/" to index and nothing else; it maps all other request
    > URIs not given servlet-mappings in those 2 web.xml files to the index
    > servlet instead of to the default servlet. That's what servlet-mapping
    > to something broader like "/*" or "/**/*" or something should do;
    > mapping to just "/" shouldn't give the effect it does. So if I do
    > http://localhost:8080/foo then I'll get the index servlet unless
    > foo=article or blah.gif, etc. I'd rather get a NOT FOUND message for
    > random URIs.
    >
    >


    The trouble with servlet mappings is that the url pattern you can use is
    not very fine grained. I hope in the future you can use regular
    expressions for it.

    For now would suggest introducing jsp pages. In the web.xml you then add
    an entry like

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

    All requests to yourdomain.org/ are handled by this index.jsp

    You can drop all the servlet mappings, and your css and gif mappings
    (which looks like a workaround to me) can also go.

    Let me know if you need more info!
    Kees de Kooter
    Kees de Kooter, Sep 6, 2003
    #2
    1. Advertising

  3. Re: Apache Tomcat 4.1.x: how to get front page without redirect likeslashdot.org

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


    AFAIK Since Servlet API 2.3 a servlet can be a welcome-file
    The problem is that itll probably show at the and of the url, which is
    not what was needed. [The same thing happens with jsp's. ]


    Michal.
    Michal Dzirba, Sep 6, 2003
    #3
  4. > The trouble with servlet mappings is that the url pattern you can use is
    > not very fine grained. I hope in the future you can use regular
    > expressions for it.
    >
    > For now would suggest introducing jsp pages. In the web.xml you then add
    > an entry like
    >
    > <welcome-file-list>
    > <welcome-file>index.jsp</welcome-file>
    > </welcome-file-list>
    >
    > All requests to yourdomain.org/ are handled by this index.jsp


    But there is a redirect. Requests to mydomain.org/ are redirected to
    mydomain.org/index.jsp. Also, there's no need for the welcome-file to
    be a jsp. The welcome file can be a relative servlet url such as just
    plain old index for my index servlet. The requests to mydomain.org/
    are redirected to mydomain.org/index.
    Alex Martinoff, Sep 6, 2003
    #4
  5. > The trouble with servlet mappings is that the url pattern you can use is
    > not very fine grained. I hope in the future you can use regular
    > expressions for it.
    >
    > For now would suggest introducing jsp pages. In the web.xml you then add
    > an entry like
    >
    > <welcome-file-list>
    > <welcome-file>index.jsp</welcome-file>
    > </welcome-file-list>
    >
    > All requests to yourdomain.org/ are handled by this index.jsp


    But there is a redirect. Requests to mydomain.org/ are redirected to
    mydomain.org/index.jsp. Also, there's no need for the welcome-file to
    be a jsp. The welcome file can be a relative servlet url such as just
    plain old index for my index servlet. The requests to mydomain.org/
    are redirected to mydomain.org/index.
    Alex Martinoff, Sep 6, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Martinoff
    Replies:
    2
    Views:
    4,179
    Alex Martinoff
    Sep 7, 2003
  2. Replies:
    0
    Views:
    922
  3. Marcin Cenkier
    Replies:
    1
    Views:
    5,414
    Marcin Cenkier
    Apr 12, 2006
  4. Replies:
    4
    Views:
    581
    Joseph Kesselman
    Aug 10, 2006
  5. Curt Hibbs
    Replies:
    2
    Views:
    93
    Bill Guindon
    Mar 4, 2005
Loading...

Share This Page