there is a maven project with the /WEB-INF/web.xml file of the following content:

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <servlet> <servlet-name>baseWebApp</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>baseWebApp</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> 

applicationContext.xml:

  <mvc:annotation-driven/> <context:component-scan base-package="...."/> <mvc:resources mapping="/resources/**" location="/resources/"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/pages/" p:suffix=".jsp"/> </beans> 

The controller is implemented on the basis of annotations.

Being run by mvn -Djetty.port = 9999 jetty: run, the application runs perfectly. I decided to initialize jetty in code, and I can’t manually create a context for jetty. Below is the main class:

 public class AppMain { public static void main(String[] args) throws Exception { ServletContextHandler sch = new ServletContextHandler(); /* инициализация контекста .... */ Server jetty = new Server(9999); jetty.setHandler(sch); jetty.start(); } } 

question: how to initialize context based on web.xml?

    1 answer 1

    You need to create a WebAppContext .

     import org.eclipse.jetty.server.Server; import org.eclipse.jetty.webapp.WebAppContext; public class AppMain { public static void main(String[] args) throws Exception { Server jetty = new Server(9999); // Создаем корневой контекст из директории WebAppContext webapp = new WebAppContext("D:\\some\\path\\to\\webapp\\root", "/");); // WebAppContext является ContextHandler, назначаем его серверу jetty.setHandler(webapp); // Стартуем и не выключаемся до завершения работы Jetty jetty.start(); jetty.join(); } } 
    • The example from the textbook does not fit)) because the war file is not supposed to be - dmitrijk
    • @dmitrijk, do you want to start an unpacked web application? - Nofate
    • yes _________________ - dmitrijk
    • @dmitrijk, updated the answer. - Nofate