Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add web application context to Jetty

I want to create simple Spring MVC "Hello world" application that runs under Jetty (wich is a part of application).

Structure of my application is:

|-WEB-INF
| |-view
| |-layout
| |-index.jsp
| |-web.xml
|
|-jetty.xml
|-application-context.xml

I try to create Jetty server and add web application context based on web.xml file:

Resource jettyConfig = Resource.newSystemResource("jetty.xml");
XmlConfiguration configuration = new XmlConfiguration(jettyConfig.getInputStream());
Server server = (Server)configuration.configure();

WebAppContext wac = new WebAppContext();
wac.setDescriptor("WEB-INF/web.xml");
wac.setContextPath("/");
wac.setParentLoaderPriority(true);
server.setHandler(wac);

server.start();

Server starts fine, but without my context: no info about spring start up in logs, spring mvc controllers are not available. Have anybody ideas what I do wrong?

Content of jetty.xml:

  <Configure id="server" class="org.mortbay.jetty.Server">
      <Call name="addConnector">
          <Arg>
              <New class="org.mortbay.jetty.nio.SelectChannelConnector">
                  <Set name="port">9080</Set> 
              </New>
          </Arg>
      </Call>
      <Set name="handler">
          <New class="org.mortbay.jetty.handler.HandlerList">
              <Set name="handlers">
                  <Array type="org.mortbay.jetty.Handler">
                      <Item>
                          <New class="org.mortbay.jetty.handler.DefaultHandler" /> 
                      </Item>
                      <Item>
                          <New class="org.mortbay.jetty.handler.ResourceHandler">
                              <Set name="resourceBase">.</Set> 
                          </New>
                      </Item>
                  </Array>
              </Set>
          </New>
      </Set>
  </Configure>

Content of WEB-INF/web.xml:

  <web-app>
      <display-name>Hello world</display-name>

      <init-param>
          <param-name>development</param-name> 
          <param-value>true</param-value> 
      </init-param>

      <servlet>
          <servlet-name>mvc</servlet-name> 
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
          <init-param>
              <param-name>contextConfigLocation</param-name> 
              <param-value>application-context.xml</param-value> 
          </init-param>
          <load-on-startup>1</load-on-startup> 
      </servlet>
      <servlet-mapping>
          <servlet-name>mvc</servlet-name> 
          <url-pattern>/*</url-pattern> 
      </servlet-mapping>

      <filter>
          <filter-name>springSecurityFilterChain</filter-name> 
          <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
      </filter>
      <filter-mapping>
          <filter-name>springSecurityFilterChain</filter-name> 
          <url-pattern>/*</url-pattern> 
      </filter-mapping>

      <listener>
         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
      </listener>

      <error-page>
          <error-code>404</error-code> 
          <location>/error/404.jsp</location> 
      </error-page>
      <error-page>
          <error-code>500</error-code> 
          <location>/error/500.jsp</location> 
      </error-page>
  </web-app>
like image 348
mikhail Avatar asked Oct 20 '25 04:10

mikhail


2 Answers

If you are running with an exploded war directory try setting the resource base property explicitly:

context.setResourceBase("/path-to-your-project/WebContent");
context.setDescriptor("/path-to-your-project/WebContent/WEB-INF/web.xml");

or if you are deploying the war itself you can just use:

  context.setWar("/path-to-your-project/WebContent");

Here are the docs showing embedded Jetty samples.

In the context of your app:

Resource jettyConfig = Resource.newSystemResource("jetty.xml");
XmlConfiguration configuration = new XmlConfiguration(jettyConfig.getInputStream());
Server server = (Server)configuration.configure();

WebAppContext wac = new WebAppContext();
wac.setResourceBase(".");
wac.setDescriptor("WEB-INF/web.xml");
wac.setContextPath("/");
wac.setParentLoaderPriority(true);
server.setHandler(wac);

server.start();

This assumes that your base path where you're running your server from is the same as the path to the web content.

like image 95
Joel Avatar answered Oct 22 '25 17:10

Joel


The application-context.xml file should be placed inside the WEB-INF directory.

like image 30
user1092781 Avatar answered Oct 22 '25 17:10

user1092781