Web.xml servlet mapping for wildcard queries

I want one of my servlets (test2) to handle the request "/" (ie http: // localhost / ), and the other servlet (test1) to handle all other requests ("/ *").

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

Can someone tell how to do 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 that my question was a bit obscure and incomplete. Here is an example of what I want to achieve.

web.xml :

<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>

. http://mytestsite.com/, catchall.jsp index.html. , , catchall.jsp( System.out.println , ).

+3
4

, . , , http://your-domain.com/.

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

, test2.jsp - "" . , MVC http://your-domain.com/.

, index.jsp( ). index.jsp - "" .

+1

. "/", .

filter --> /*

servlet1 --> /_some_hidden_path_1_
servlet2 --> /_some_hidden_path_2_
+1

, , , , \ , . \map test2 .

Yours faithfully

0
source

Do not try to display / request something (get rid of the test2 servlet) and use the welcome file instead:

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

Source: https://habr.com/ru/post/1718661/


All Articles