Can someone explain the Spring web.xml file?

2020-05-24 04:52发布

问题:

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/Hibernate which I understand is fairly common. It also uses DWR for remote method invocations. I'm finding it somewhat difficult to separate responsibilities: what Tomcat is responsible for, what Spring is responsible for, how a request gets from one to the other, and how the major pieces of Spring fit together. I've read a great deal of documentation on Spring, particularly about beans and bean factory and am still in process of reading more. Any advice you guys have would be welcome, but I'll provide some specific questions.

Question 1: Where does the web.xml fit into things (when is it used/called, and where is it called from)?

Code sample 1:

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

What does the above snippet do (or, what does it cause to happen)? At some point in my web app qrst.jsp gets used; is it the DispatcherServlet that calls qrst.jsp using the servlet name? Else what is the significance of the servlet name? What is load on startup?

Code sample 2:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /someLocation/some-servlet.xml
    </param-value>
</context-param>

Links or explanation of what the above does? I can see from looking at the XML file that it contains bean definitions and I do understand what beans are and how they are used, but I don't know any other details about this and would like to.

Code sample 3:

<servlet>
  <servlet-name>dwr-invoker</servlet-name>
  <display-name>DWR</display-name>
  <servlet-class>
        org.directwebremoting.servlet.DwrServlet
</servlet-class>
    <init-param>
        <param-name>classes</param-name>
        <param-value>
            somepackage.someclass
        </param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
</servlet>

From what I read about beans, I believe those init-param elements are just parameters that get set in the servlet's java class. What's the significance of the servlet name, and what about the load on startup? 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? How does it decide to route the request to DWR instead of to qrst.jsp? Where does it do this?

Thanks.

回答1:

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 <servlet-mapping> 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:

<servlet-mapping>
    <servlet-name>qsrt</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>dwr</servlet-name>
    <url-pattern>*.dwr</url-pattern>
</servlet-mapping>

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: <url-pattern>/*</url-pattern>) 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.



标签: spring dwr