0

I've set a servlet with this pattern /* but I get a 404 error even to get the index.jsp file, I thougt that /* matches with any pattern ,the code is correct because it works with *.html

web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>


   <servlet>
        <servlet-name>main</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>main</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

and this controller

package com.tutorial.ejemplospring.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class MainController {

    @RequestMapping("/main.html")
    public ModelAndView mainPage() {

        return new ModelAndView("main");
    }
    @RequestMapping("/second.html")
    public ModelAndView secondPage() {

        return new ModelAndView("second");
    }

}
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
AFS
  • 1,433
  • 6
  • 28
  • 52
  • What do you expect to happen when going to /index.jsp? And what do you mean by "it works with *.html"? – JB Nizet Nov 11 '16 at 17:21
  • with `/*` ,I run the app and I get a404 error,cannot see index.jsp ,if I use `*.html` everything works correct I can access all the jsp files – AFS Nov 11 '16 at 17:24
  • 2
    Well, `/*` means everything under `/`. `/index.jsp` thus matches this pattern, and the spring servlet is thus invoked. It isn't invoked when you use *.html, because /index.jsp does not match *.html. – JB Nizet Nov 11 '16 at 17:29
  • AFS: When you have /* and request url for main.html, it should work ? But index.jsp does not work, correct ? – Vasu Nov 11 '16 at 17:33
  • 2
    Now you have completely changed the question, making the answer you got irrelevant. Don't do that. Accept the response you had if it was helpful, and ask another question. – JB Nizet Nov 11 '16 at 17:56

1 Answers1

2

/* is an exhaustive pattern, usually you will map the filters with this pattern where you want all the request to be passed down to filter before reaches you dispatcher.

as @JB Nizet explained, /* will match the /index.jsp as well, so it is routed to dispatcher but dispatcher does not know what to do with that. second.html still goes through dispatcher but does know what to do any sends you to the right jsp page.

kuhajeyan
  • 10,727
  • 10
  • 46
  • 71