Can someone explain the Spring web.xml file?
Asked Answered
S

1

14

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.

Shearwater answered 29/4, 2011 at 20:34 Comment(0)
F
19

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.

Fazio answered 29/4, 2011 at 20:59 Comment(5)
Thanks a lot for the help. I do have a servlet-mapping defined for both qrst AND for dwr-invoker, so what you're saying makes sense to me! However, the URL in the browser's address bar always looks the same: it would always invoke the qrst servlet. Could dwr be setting the URL in the javascript code before making its request? I've already looked for this a little bit but don't see anything like that, if I'm on the right track I'll keep looking.Shearwater
In the some-servlet.xml, there are two elements with an attribute "class" that contains "Controller". One is the main one, and the other is for the DWR! So it seems that DispatcherServlet probably just chooses the appropriate one to invoke, does that sound sensible? And I have to go home now but I will check out your links asap on Monday. Thanks a million for the help, I really can't tell you how much I appreciate it.Shearwater
Thanks again for all of the help, I can't tell you how much I appreciate it, I've been able to study the appropriate topics as a result of your great post. @Everyone - Using HttpFox showed that "/dwr" IS part of the URL being posted to when the ajax calls execute. Leonel's statement about servlet-mappings was spot on. Now its just a matter of finding out HOW that url gets posted to (since I don't see where it is set... ). I'm updating the original question with some helpful documentation.Shearwater
Where can I find the full structure of web.xml?Cirilla
I'm curious about the ` it is not advised to create your own` part. I'm guessing this is because standards like REST exist and rather than try to fulfill the standard, let a 3rd party framework abstract this low-level implementation away.Peebles

© 2022 - 2024 — McMap. All rights reserved.