嵌入式码头服务器无法同时运行servlet和webapp [英] embedded jetty server does not run both servlet and webapp

查看:115
本文介绍了嵌入式码头服务器无法同时运行servlet和webapp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为嵌入式Jetty 9.4.3.x服务器编写POC.当我使用多个处理程序运行服务器时,只有第一个处理程序有效.在代码中,如果我将上下文"作为第一个处理程序,则我的hello servlet可以工作,对于jsp,我会收到404错误.如果我将"webapp"作为第一个处理程序,则jsp可以工作,并且会得到404的servlet.这是代码.我有什么想念的吗? servlet和jsp文件是简单的测试文件.如果需要,我可以添加webdefault.xml和jetty.xml文件.

I am writing a POC for embedded Jetty 9.4.3.x server. When I run the server with multiple handlers, only the first handler works. In the code if I have 'context' as the first handler, my hello servlet works and for jsp I get a 404 error. If I have 'webapp' as the first handler, jsp works and I get a 404 for servlet. Here is the code. Am I missing anything? The servlet and jsp files are simple test files. If needed I can add the webdefault.xml and jetty.xml files.

package com.easyask.server;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.webapp.Configuration;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.xml.XmlConfiguration;

public class EasyAskServer {
    private static String m_webdefaultXMLFileName = "etc/webdefault.xml";

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

    List<String> configurations = new ArrayList<String>();
    configurations.add("etc/jetty.xml"); //$NON-NLS-1$

    configurations.add("etc/jetty-http.xml"); //$NON-NLS-1$
    configurations.add("etc/jetty-ssl.xml"); //$NON-NLS-1$
    configurations.add("etc/jetty-ssl-context.xml"); //$NON-NLS-1$
    configurations.add("etc/jetty-https.xml"); //$NON-NLS-1$

    XmlConfiguration last = null;
    List<Object> objects = new ArrayList<Object>();
    try{
        for (String configFile : configurations) {
            InputStream configStream = null;

            File xmlConfiguration = new File(configFile);
            if (xmlConfiguration.exists()) {
                configStream = new FileInputStream(xmlConfiguration);
            }

            XmlConfiguration configuration = new XmlConfiguration(configStream);
            if (last != null) {
                configuration.getIdMap().putAll(last.getIdMap());
            }
            objects.add(configuration.configure());
            last = configuration;
        }


    }catch (Exception e){
        e.printStackTrace();
    }
    server = (Server) objects.get(0);

    ServletContextHandler context = new ServletContextHandler(server, "/", ServletContextHandler.SESSIONS);
    context.setContextPath("/");
    context.setResourceBase("com/easyask/server");
    context.addServlet(HelloServlet.class, "/hello");

    WebAppContext webapp = new WebAppContext();
    webapp.setResourceBase("com/easyask/server");
    webapp.setContextPath("/");
    webapp.setExtractWAR(false);
    webapp.setDefaultsDescriptor(m_webdefaultXMLFileName);
    webapp.setAttribute("javax.servlet.context.tempdir", "tmp");

    Configuration.ClassList classlist = Configuration.ClassList
            .setServerDefault(server);
    classlist.addAfter("org.eclipse.jetty.webapp.FragmentConfiguration",
            "org.eclipse.jetty.plus.webapp.EnvConfiguration",
            "org.eclipse.jetty.plus.webapp.PlusConfiguration");
    classlist.addBefore(
            "org.eclipse.jetty.webapp.JettyWebXmlConfiguration",
            "org.eclipse.jetty.annotations.AnnotationConfiguration");

    webapp.setAttribute(
            "org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern",
            ".*/[^/]*servlet-api-[^/]*\\.jar$|.*/javax.servlet.jsp.jstl-.*\\.jar$|.*/[^/]*taglibs.*\\.jar$");

    HandlerCollection handlers = new HandlerCollection();

    handlers.addHandler(context);
    handlers.addHandler(webapp);
    server.setHandler(handlers);

    try {
        server.start();
        server.dumpStdErr();
        server.join();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

推荐答案

您在同一个contextPath上同时具有ServletContextHandlerWebAppContext,这是行不通的.

You have both the ServletContextHandler and WebAppContext on the same contextPath, that's not going to work.

将它们合并在一起.

contextPath("/")上只有ServletContextHandlerWebAppContext

这篇关于嵌入式码头服务器无法同时运行servlet和webapp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆