Phoenix
Phoenix

Reputation: 8923

Web application configuration in java

I have 3 "contextual" files for my web application. One is web.xml, One is context.xml, and another is dispatcher-servlet.xml. Note I've to use Spring for my web app. I have a single web application which needs to run on the server. Can someone verify my understanding ?
1. Web.xml - needed and must have for all java web apps. This is where the servlet configuration goes in. What are the parameters param-name = contextConfigLocation and contextLoaderListener and why are they needed ? What does a contextLoaderListener do exactly ?

<context-param>
        <description>Spring Application Context Configuration</description>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/applicationContext-workflow.xml
            /WEB-INF/applicationContext-general.xml
        </param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.mvc</url-pattern>
    </servlet-mapping>
  1. Context.xml is for environment specific db connections, queue connections go. Is using commons dbcp the preferred choice ?

  2. And the dispatcher-servlet.xml looks like this: Why does it need to be like this ?

  3. In terms of these files being loaded by the server, will context.xml be loaded first, then web.xml then dispatcher-servlet.xml ?

  4. In terms of a client request, will it first be intercepted by dispatcher-servlet ?

Upvotes: 0

Views: 320

Answers (1)

Tejas
Tejas

Reputation: 6533

  1. yes, dbcp can be used if it fits your requirements. Basically context.xml is a Spring configuration files to define spring beans. This is for spring-core.

  2. dispatcher-servlet.xml This is an another Spring configuration file for Spring Web MVC configuration. It is also possible to mix file 1 and 2 but strictly discarded. It defines your Sprinf MVC configuration, any web resourcebundles, mvc interceptors etc. If you choose to use different web framework like JSF or struts, you do not need to have this file and other configuration files for those frameworks will be added to your project.

  3. web.xml will be loaded first - Always for any Java web app. There you define your dispatcher servlet. And you have defined the listener. Once the web app is loaded, the listener is notified (Answer to your question- what listener is doing) The listener then gets the file list specified as context params and load spring context from it. Once done, the web app is loaded completely.

  4. What the request will hit first depends on following line

 <servlet-mapping>
     <servlet-name>dispatcher</servlet-name>
     <url-pattern>*.mvc</url-pattern>
 </servlet-mapping>

any request url ending with .mvc will be intercepted by Spring dispatcher-servlet.

Upvotes: 1

Related Questions