簡體   English   中英

嵌入式Jetty:未初始化Servlet

[英]Embedded Jetty: Servlet not initialized

我試圖在一個帶有servlet的應用程序中嵌入Jetty服務器。

當我點擊網址: https://127.0.0.1:8443/hello ://127.0.0.1:8443 / https://127.0.0.1:8443/hello

我明白了:

HTTP錯誤:503

訪問/hello.html時遇到問題。 原因:

 javax.servlet.UnavailableException: Servlet not initialized 

由Jetty提供://

這是我的代碼:

public class Main 
{
  public static void main(String[] args) throws Exception 
  {
    // path to keystore.
    String jetty_home = System.getProperty("jetty.home","c:\\jetty.home");
    System.setProperty("jetty.home", jetty_home);

    // Create a basic jetty server object 
    Server server = new Server();

    // HTTP Configuration
    HttpConfiguration http_config = new HttpConfiguration();
    http_config.setSecureScheme("https");
    http_config.setSecurePort(8443);
    http_config.setOutputBufferSize(32768);

    // SSL Context Factory for HTTPS
    SslContextFactory sslContextFactory = new SslContextFactory();
    sslContextFactory.setKeyStorePath(jetty_home + "\\keystore");
    sslContextFactory.setKeyStorePassword("password");
    sslContextFactory.setKeyManagerPassword("password");

    // HTTPS Configuration
    HttpConfiguration https_config = new HttpConfiguration(http_config);
    https_config.addCustomizer(new SecureRequestCustomizer());

    // HTTPS connector
    ServerConnector https = new ServerConnector(server,
        new SslConnectionFactory(sslContextFactory, "http/1.1"),
        new HttpConnectionFactory(https_config));
    https.setPort(8443);

    // Set the connectors
    server.setConnectors(new Connector[] {  https });

    // Specify the Session ID Manager
    HashSessionIdManager idmanager = new HashSessionIdManager();
    server.setSessionIdManager(idmanager);

    // Sessions are bound to a context.
    ServletContextHandler context = new ServletContextHandler(null, "/", ServletContextHandler.SESSIONS|ServletContextHandler.NO_SECURITY);
    server.setHandler(context);

    // Create the SessionHandler (wrapper) to handle the sessions
    HashSessionManager manager = new HashSessionManager();
    SessionHandler sessions = new SessionHandler(manager);
    context.setHandler(sessions);

    // handle the servlets in the session inside of SessionHandler 
    ServletHandler servletHandler = new ServletHandler();
    sessions.setHandler(servletHandler);

    // tree should look like this:
    // Server
    //   + Connector -> HTTPS
    //      + ContextHandler( "/" )
    //         + SessionHandler
    //             + servletHandler

    // set up the servlet
    ServletHolder servletHolder = new ServletHolder(Main.MyServlet.class);

    servletHandler.addServletWithMapping(servletHolder, "/*");

    // Start the server
    server.start();
    server.join();
  }

  public static class MyServlet extends HttpServlet
  {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
      resp.getOutputStream().print("<html><body><h1>Hello</h1></body></html>");
    }
  }
}

我錯過了什么? servlet如何/在何處初始化。

由於你沒有說明什么版本的Jetty,我假設你使用的是Jetty 9.2.3.v20140905

這段代碼很奇怪

// Sessions are bound to a context.
ServletContextHandler context = new ServletContextHandler(null, "/",
   ServletContextHandler.SESSIONS|ServletContextHandler.NO_SECURITY);
server.setHandler(context);  // <-- this is the ServletContext

// Create the SessionHandler (wrapper) to handle the sessions
HashSessionManager manager = new HashSessionManager();
SessionHandler sessions = new SessionHandler(manager);
context.setSessionHandler(sessions); // <-- set the session handler

// set up the servlet
ServletHolder servletHolder = new ServletHolder(Main.MyServlet.class);
context.addServlet(servletHolder, "/*");

首先,不要自己管理ServletHandler ,只是撤消ServletContextHandler本身所做的所有工作。

另外,不要自己管理處理程序樹,讓內部管理正確的鏈接,因此切換到.setSessionHandler()

最后,使用context.addServlet()而不是.addServletWithMapping()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM