tags:

views:

1511

answers:

4

I want one of my servlets (test2) to handles the "/" request (i.e. http://localhost/), while another servlet (test1) handles all other requests ("/*").

I set up my web.xml below, but the problem is that ALL requests go to test1.jsp (even the "/" request)

Can someone tell my how to accomplish this?

<servlet>
    <servlet-name>test1</servlet-name>
    <jsp-file>/test1.jsp</jsp-file>
</servlet>
<servlet-mapping>
    <servlet-name>test1</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

<servlet>
    <servlet-name>test2</servlet-name>
    <jsp-file>/test2.jsp</jsp-file>
</servlet>
<servlet-mapping>
    <servlet-name>test2</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

----EDIT-----

i realized my question was a bit unclear and incomplete. here is an example of exactly what i want to accomplish..

  1. http://mytestsite.com/ -> maps to http://mytestsite.com/index.html
  2. http://mytestsite.com/servlet1 -> runs com.mytestsite.servlet1
  3. http://mytestsite.com/* -> maps to http://mytestsite.com/catchall.jsp (i want all other requests that aren't mapped in web.xml to map to catchall.jsp)

so my web.xml looks as follows:

<servlet>
    <servlet-name>servlet1</servlet-name>
    <servlet-class>com.mytestsite.servlet1</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>servlet1</servlet-name>
    <url-pattern>/servlet1</url-pattern>
</servlet-mapping>

<servlet>
    <servlet-name>catchall</servlet-name>
    <jsp-file>/catchall.jsp</jsp-file>
</servlet>
<servlet-mapping>
    <servlet-name>catchall</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

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

so i noticed a strange problem. when i request http://mytestsite.com/, it goes to catchall.jsp before being redirected to index.html. however, it happens so quickly i wouldn't have even noticed it hitting catchall.jsp (but i put a System.out.println in this file, and it was definitely hitting it).

A: 

Really not sure about that, but maybe the order that you declare\map your servlets defines precedence. Try to declare\map test2 first and see.

Kind Regards

marcospereira
A: 

Try not mapping the / request to anything (get rid of the test2 servlet), and instead use a welcome file:

<welcome-file-list>
  <welcome-file>
    test2.jsp
  </welcome-file>
</welcome-file-list>
Kaleb Brasee
+1  A: 

I think your goal is a bit confusing and brittle. However, to answer your question, try a welcome file entry for the http://your-domain.com/ request.

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

It is most common to then have test2.jsp perform a redirect or forward to some other 'controller' in your application. That way your MVC is always fired even on http://your-domain.com/ requests.

If you agree with me on that, then your welcome file should be index.jsp (to follow common conventions). The code in index.jsp is then a one-liner redirect to a 'welcome' servlet.

Brian
thanks for taking the time to look at the issue i'm having. i edited my question above. hopefully it clears things up...
jmoney
+1  A: 

Use a forwarding filter instead of servlet. It's very simple to intercept "/" using such method.

filter --> /*

servlet1 --> /_some_hidden_path_1_
servlet2 --> /_some_hidden_path_2_
serge_bg