Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jsp 嵌入式jetty服务器不能同时运行servlet和webapp_Jsp_Servlets_Embedded Jetty_Jetty 9 - Fatal编程技术网

Jsp 嵌入式jetty服务器不能同时运行servlet和webapp

Jsp 嵌入式jetty服务器不能同时运行servlet和webapp,jsp,servlets,embedded-jetty,jetty-9,Jsp,Servlets,Embedded Jetty,Jetty 9,我正在为嵌入式Jetty 9.4.3.x服务器编写POC。当我使用多个处理程序运行服务器时,只有第一个处理程序工作。在代码中,如果我将“context”作为第一个处理程序,那么我的hello servlet可以工作,对于jsp,我会得到一个404错误。如果我使用“webapp”作为第一个处理程序,jsp就可以工作,而我得到的servlet是404。这是代码。我遗漏了什么吗?servlet和jsp文件是简单的测试文件。如果需要,我可以添加webdefault.xml和jetty.xml文件 pac

我正在为嵌入式Jetty 9.4.3.x服务器编写POC。当我使用多个处理程序运行服务器时,只有第一个处理程序工作。在代码中,如果我将“context”作为第一个处理程序,那么我的hello servlet可以工作,对于jsp,我会得到一个404错误。如果我使用“webapp”作为第一个处理程序,jsp就可以工作,而我得到的servlet是404。这是代码。我遗漏了什么吗?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();
    }
}

}
package com.easyask.server;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.InputStream;
导入java.util.ArrayList;
导入java.util.List;
导入org.eclipse.jetty.server.server;
导入org.eclipse.jetty.server.handler.HandlerCollection;
导入org.eclipse.jetty.servlet.ServletContextHandler;
导入org.eclipse.jetty.webapp.Configuration;
导入org.eclipse.jetty.webapp.WebAppContext;
导入org.eclipse.jetty.xml.XmlConfiguration;
公共类EasyAskServer{
私有静态字符串m_webdefaultXMLFileName=“etc/webdefault.xml”;
公共静态void main(字符串[]args){
服务器=新服务器();
列表配置=新建ArrayList();
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;
列表对象=新的ArrayList();
试一试{
用于(字符串配置文件:配置){
InputStream configStream=null;
文件xmlConfiguration=新文件(configFile);
if(xmlConfiguration.exists()){
configStream=新文件输入流(xmlConfiguration);
}
XmlConfiguration=新的XmlConfiguration(configStream);
if(last!=null){
configuration.getIdMap().putAll(last.getIdMap());
}
添加(configuration.configure());
最后=配置;
}
}捕获(例外e){
e、 printStackTrace();
}
服务器=(服务器)对象。获取(0);
ServletContextHandler上下文=新的ServletContextHandler(服务器“/”,ServletContextHandler.SESSIONS);
context.setContextPath(“/”);
setResourceBase(“com/easyask/server”);
addServlet(HelloServlet.class,“/hello”);
WebAppContext webapp=新的WebAppContext();
setResourceBase(“com/easyask/server”);
webapp.setContextPath(“/”);
webapp.setExtractWAR(false);
setDefaultsDescriptor(m_webdefaultXMLFileName);
setAttribute(“javax.servlet.context.tempdir”、“tmp”);
Configuration.ClassList=Configuration.ClassList
.setServerDefault(服务器);
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.ContainerCludeJarPattern”,
“*/[^/]*servlet api-[^/]*\\.jar$\\.*/javax.servlet.jsp.jstl-.\\\\.jar$\\.*/[^/]*标记库。*\\\.jar$”;
HandlerCollection handlers=新的HandlerCollection();
addHandler(上下文);
addHandler(webapp);
setHandler(处理程序);
试一试{
server.start();
server.dumpStdErr();
join();
}捕获(例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
}

您在同一个
contextPath
上同时拥有
ServletContextHandler
WebAppContext
,这是行不通的

将它们合并在一起

contextPath(“/”)