Can someone explain the Spring web.xml file?

前端 未结 1 1225
情深已故
情深已故 2021-02-04 16:42

I\'m new to Java Enterprise and to Spring but I have a strong grasp of standard Java. I am looking through an existing web application project. The project uses Tomcat/Spring/Hi

1条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-04 17:29

    Servlets are JavaEE's idiom for answering HTTP requests. You program the behavior of your application in a Servlet which will respond to a request.

    Tomcat is a Servlet container, which means you deploy your application in Tomcat and it will manage all the communication infrastructure for you: it accepts connections, manages database connections(*) and will call upon your servlets to handle incoming requests.

    web.xml is part of any JavaEE application, not Spring. Your code sample 1 declares that your app will use an instance of class org.springframework.web.servlet.DispatcherServlet to handle incoming requests.

    Although servlets are the basic foundations for JavaEE development, it is not advised to create your own; instead, with Spring, you create MVC controllers. Then the DispatcherServlet will call upon these controllers to handle the requests. It's just another indirection (but a very powerful one!)

    is it the DispatcherServlet that calls qrst.jsp using the servlet name?

    Not directly. It's just a coincidence that your servlet and the JSP file have the same name.

    What is loaded on startup?

    Your code sample 2 instructs the DispatcherServlet to load the beans from file /someLocation/some-servlet.xml. If there are controller beans in this file and according to how you configured the url mapping, beans from this file will answer the incoming requests. See the reference.

    I believe those init-param elements are just parameters that get set in the servlet's java class

    The init-param elements in web.xml are for the servlet class.

    The web app somehow "knows" when an AJAX (dwr) call is happening versus when the web app is being loaded for the first time (when its loading for the first time it should use qrst.jsp). How does it know this?

    Missing from the question are either the element (found in web.xml), or the url mappings (found in the spring files). These are responsible for deciding whether an URL should be handled by the dispatcher servlet or the dwr servlet.

    For instance, with an servlet mapping like below:

    
        qsrt
        *.do
    
    
        dwr
        *.dwr
    
    

    Then all URLs ending in .do will be answered by the dispatcher servlet, and those ending with .dwr will be handled by the dwr servlet. Here's where the names of the servlets are important.

    JSP files are a different story. The container will simply use them to handle a URL ending in *.jsp. Do not create your onw servlet mapping for URLs ending in *.jsp. This will only cause headaches. This is probably unspecified behavior.

    Edit:

    However, the URL in the browser's address bar always looks the same: it would always invoke the qrst servlet

    Then it is possible that your servlet-mapping is so broad (something like: /*) that it will handle anything you throw at the server and never give a chance for the other servlets to handle it.

    Last but not least, when working with DWR or any Ajax technology, install the HttpFox extension for Firefox so you can monitor the Ajax calls of your application.

    0 讨论(0)
提交回复
热议问题