Results 1 to 10 of 10

Thread: Understanding DispatcherServlet

  1. #1
    Join Date
    Aug 2012
    Posts
    7

    Default Understanding DispatcherServlet

    I am trying to use spring-security

    Before all of the configuration

    Code:
    http://localhost:9090/app/login2.xhtml
    request, works as i expected.

    I added a controller:

    Code:
     @Controller
        @RequestMapping("/auth")
        public class LoginController {
        
        
         @RequestMapping(value = "/login", method = RequestMethod.GET)
         public String getLoginPage(@RequestParam(value="error", required=false) boolean error, 
           ModelMap model) {
        return "login2.xhtnml";
        }
        
        }
    I have in web.xml:




    Code:
    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>
                		classpath:META-INF/spring-servlet.xml
          		</param-value>
            </init-param>
        <load-on-startup>1</load-on-startup>
        </servlet>
         
        <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/*</url-pattern>
        </servlet-mapping>
    With this configuration when i call

    Code:
    http://localhost:9090/app/login2.xhtml
    Error comes

    WARN org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/app/login2.xhtml] in DispatcherServlet with name 'spring'


    BUT when i change configuration mapping to

    Code:
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/app/*</url-pattern>
        </servlet-mapping>
    Code:
    `http://localhost:9090/app/login2.xhtml`
    works as i expected

    but

    Code:
    http://localhost:9090/app/auth/login
    gives no error, no exception, no redirection, i think dispatcher servlet can not know about this request.

    Code:
    http://localhost:9090/app/app/auth/login
    works with

    Code:
      <servlet-mapping>
            <servlet-name>spring</servlet-name>
            <url-pattern>/app/*</url-pattern>
            </servlet-mapping>
    My understanding:

    dispatcher servlet use `"http://localhost:9090/"` as base for searching login2.xhtml
    and use
    Code:
    `"http://localhost:9090/app"`
    for `/auth/login` URL.

    I do not know where to set this, and why they are different.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    It depends and you leave out some of the details so it is actually quite hard to tell.

    In general the url is /[application]/[servlet]/[remainder].

    Now if you have a foo.war application would be foo, if you have mapped your servlet to app the remainder will be used do detect a controller. If nothing can be found detection and rendering is passed to the servlet container. So given this.


    Code:
    http://localhost:9090/app/auth/login
    That isn't deteced by the dispatcher servlet because then there should be a servlet mapped to /auth/* or /* but you hvae mapped to /app (and /app/app != /app ).

    Error comes

    WARN org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/app/login2.xhtml] in DispatcherServlet with name 'spring'
    Read again it isn't an error it is a warning indicating that this servlet cannot handle te request, which makes perfectly sense as you have not mapped a controller to the login2.xhtml.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Aug 2012
    Posts
    7

    Default

    yes, it is a warning but it does not allow the page to come up.

    Additional info: my tomcat server.xml contains a line like

    Code:
    <context path="/app" >

    Now i only want to know

    with

    Code:
    <url-pattern>/*</url-pattern>
    why can dispatcher servlet find
    Code:
    http://localhost:9090/app/auth/login
    why can not dispatcher servlet find
    Code:
    http://localhost:9090/app/login2.xhtml
    I think i resolve the thing if i can understand it.

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    Read my post...

    Quote Originally Posted by mdeinum
    ... you have not mapped a controller to the login2.xhtml.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Aug 2012
    Posts
    7

    Default

    I changed web xml to have two dispacther but my normal urls like /app/login2.xhtml are still unhappy about the second dispatcher why does not contain "mapping found for HTTP request".

    Code:
    <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
            		classpath:META-INF/spring-servlet.xml
      		</param-value>
        </init-param>
    <load-on-startup>1</load-on-startup>
    </servlet>
     
    
    <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/app/*</url-pattern>
    </servlet-mapping>
    
     
    <servlet>
    <servlet-name>spring2</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
            		classpath:META-INF/spring2-servlet.xml
      		</param-value>
        </init-param>
    <load-on-startup>1</load-on-startup>
    </servlet>
    
    
    <servlet-mapping>
    <servlet-name>spring2</servlet-name>
    <url-pattern>/*</url-pattern>
    </servlet-mapping>

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    That doesn't matter even if you would have another one mapped at *.xhtml it still doesn;t work.. You don't have a Controller mapped to that URL so there is NOTHING that handles that URL. A DispatcherServlet isn't a Controller it dispatches requests to Controllers.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  7. #7
    Join Date
    Aug 2012
    Posts
    7

    Default

    Sorry for asking too much, if you are bored please do not reply

    Quote Originally Posted by Marten Deinum View Post
    That doesn't matter even if you would have another one mapped at *.xhtml it still doesn;t work.. You don't have a Controller mapped to that URL so there is NOTHING that handles that URL. A DispatcherServlet isn't a Controller it dispatches requests to Controllers.
    Then, why does it work with mapping below, there is no controller in below case too

    Code:
    <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/app/*</url-pattern>
    </servlet-mapping>

  8. #8
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    I changed web xml to have two dispacther but my normal urls like /app/login2.xhtml are still unhappy about the second dispatcher why does not contain "mapping found for HTTP request".
    Then, why does it work with mapping below, there is no controller in below case too
    You have now lost me, the post before it it didn't work (at least that is what you state) and now you say it does work.. Which is it.

    The case is still the same it doesn't work because there is no mapping to a Controller in neither servlet (unless you have configured a default handler then there is).

    I strongly suggest you read the reference guide (or by my book) and understanding what is happening.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  9. #9
    Join Date
    Aug 2012
    Posts
    7

    Default

    works with one dispatcher servlet and one mapping contains /app/*

    when i add two dispatcher servlet and different mappings with one /* , it does not work.

    sorry i could not tell briefly

  10. #10
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    I still don't get your problem, the dispatcherservlet works in the way described earlier. You still need a handler the fact that there is/or is a servlet doesn't matter.

    /app is your application with a servlet mapped to /app the servlet will react to urls like /app/app not /app (basic servlet mapping rules I suggest the servlet spec for that). If it doesn't match (as explained also earlier) it will pass on control to the normal servlet container and as your xhtml is accesible by anyone it will render the page.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •