Подтвердить что ты не робот

Настроить встроенный причал с помощью web.xml?

Я пытаюсь создать как войну с моим веб-приложением, так и автономный файл jar со встроенным причалом. Для встроенного причала (распределение файлов jar) я добавляю сервлет следующим образом:

public static void main(String[] args) throws Exception {
    Server server = new Server(8080);

    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/");
    server.setHandler(context);

    context.addServlet(new ServletHolder(new HelloServlet()),"/*");

    server.start();
    server.join();
}

В разделе "Файл войны" используется файл web.xml, содержащий следующее в разделе веб-приложения:

<servlet>
    <servlet-class>com.example.HelloServlet</servlet-class>
    <servlet-name>SimplestServer</servlet-name>
</servlet>
<servlet-mapping>
    <servlet-name>HelloServlet</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

Это работает. Однако я хочу избавиться от дублирования между двумя подходами. I.e., когда я добавляю новый сервлет, я хочу настроить его только в одном месте. Могу ли я загрузить и использовать файл web.xml из встроенного причала?

4b9b3361

Ответ 1

Используйте org.eclipse.jetty.webapp.WebAppContext

Пример:

package jetty;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;

public class OnWebApp
{
    public static void main(String[] args) throws Exception
    {
        // Create a basic jetty server object that will listen on port 8080.
        // Note that if you set this to port 0 then a randomly available port
        // will be assigned that you can either look in the logs for the port,
        // or programmatically obtain it for use in test cases.
        Server server = new Server(8080);

        // The WebAppContext is the entity that controls the environment in
        // which a web application lives and breathes. In this example the
        // context path is being set to "/" so it is suitable for serving
        // root context requests and then we see it setting the location of
        // the war. A whole host of other configurations are available,
        // ranging from configuring to support annotation scanning in the
        // webapp (through PlusConfiguration) to choosing where the webapp
        // will unpack itself.
        WebAppContext webapp = new WebAppContext();
        webapp.setContextPath("/");
        webapp.setWar("path/to/my/test.war");

        // A WebAppContext is a ContextHandler as well so it needs to be set to
        // the server so it is aware of where to send the appropriate requests.
        server.setHandler(webapp);

        // Start things up! By using the server.join() the server thread will
        // join with the current thread.
        // See http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html#join()
        // for more details.
        server.start();
        server.join();
    }
}

Обратите внимание, что вы создадите обычный WAR файл и используете его с Jetty.

Если у вас есть особые требования, такие как сканирование аннотаций или JNDI, вам нужно будет получить спецификацию конфигурации.

// Enable parsing of jndi-related parts of web.xml and jetty-env.xml
org.eclipse.jetty.webapp.Configuration.ClassList classlist =
   org.eclipse.jetty.webapp.Configuration.ClassList.setServerDefault(server);

// Enable JNDI
classlist.addAfter("org.eclipse.jetty.webapp.FragmentConfiguration",
   "org.eclipse.jetty.plus.webapp.EnvConfiguration",
   "org.eclipse.jetty.plus.webapp.PlusConfiguration");

// Enable Annotation Scanning
classlist.addBefore("org.eclipse.jetty.webapp.JettyWebXmlConfiguration",
  "org.eclipse.jetty.annotations.AnnotationConfiguration");

Для более подробного примера этого в WebAppContext см. пример ServerWithAnnotations.

Также обратите внимание, что вы будете иметь все правила загрузчика классов Webapp, используя эту технику. Это означает, что у вас будет загрузчик классов для webapp и еще один для сервера. Это важно понять.

Есть несколько настроек, которые вы можете сделать для WebAppContext для загрузчиков классов, но вы не можете их устранить, просто контролируйте, как они себя ведут.

WebAppContext webapp = new WebAppContext();
// ... various setup of the webapp ...
// Flip the classloader priority from servlet spec where webapp is first to
// Standard java behavior of parent (aka Server classloader) is first.
webapp.setParentLoaderPriority(true);

См. также:

Ответ 2

В итоге я использовал подход Joakim, но указывал на каталог webapp вместо военного файла.

public static void main(String[] args) throws Exception {
    Server server = new Server(8080);

    String rootPath = SimplestServer.class.getClassLoader().getResource(".").toString();
    WebAppContext webapp = new WebAppContext(rootPath + "../../src/main/webapp", "");
    server.setHandler(webapp);

    server.start();
    server.join();
}