Я пытаюсь встроить сервер Jetty в приложение с сервлетами.Embedded Jetty: Servlet не инициализирован
Когда я ударил URL: https://127.0.0.1:8443/hello
я получаю:
HTTP ERROR: 503
Problem accessing /hello.html. Reason:
javax.servlet.UnavailableException: Servlet not initialized
Powered by 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>");
}
}
}
Что мне не хватает? Как/Где инициализируется сервлет.
На самом деле, используя версию 9.3 .. но такая же разница. Теперь я вижу свою ошибку. Я не понимал, что контекст сервлета фактически содержит ServletHandler. Спасибо за вашу помощь. – AllenKll