【问题标题】:embedded jetty server does not run both servlet and webapp嵌入式码头服务器不能同时运行 servlet 和 webapp
【发布时间】:2018-03-13 23:47:45
【问题描述】:

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

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();
    }
}

}

【问题讨论】:

    标签: jsp servlets embedded-jetty jetty-9


    【解决方案1】:

    ServletContextHandlerWebAppContext 都在同一个 contextPath 上,这是行不通的。

    将它们合并在一起。

    contextPath("/") 上只有一个ServletContextHandlerWebAppContext

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-28
      • 2018-08-28
      • 1970-01-01
      • 2023-03-29
      相关资源
      最近更新 更多