2017-10-02 8 views
0

私は組み込みJetty 9.4.3.xサーバ用のPOCを作成しています。複数のハンドラーでサーバーを実行すると、最初のハンドラーだけが動作します。コードで、最初のハンドラとして 'コンテキスト'がある場合、helloサーブレットが動作し、jspに対して404エラーが発生します。私が最初のハンドラとして 'webapp'を持っていれば、jspが動作し、サーブレットのために404が得られます。ここにコードがあります。私は何か不足していますか?サーブレットとJSPファイルは単純なテストファイルです。必要な場合は、webdefault.xmlファイルとjetty.xmlファイルを追加できます。あなたは同じcontextPathServletContextHandlerWebAppContextの両方を持っている埋め込みjettyサーバはサーブレットとwebappの両方を実行しません

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

} 

答えて

0

、それが仕事に行くのではありません。

これらを結合します。

のどちらかを持っているだけServletContextHandlerまたはWebAppContextcontextPath("/")

関連する問題