1
我试图在Servlet中的应用程序中嵌入Jetty服务器。嵌入式码头:Servlet未初始化
当我打的网址: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>");
}
}
}
我缺少什么?如何/在哪里一个servlet被初始化。
实际使用9.3版本..但同样差。我现在看到我的错误。我并不了解servlet上下文实际上包含ServletHandler。谢谢你的帮助。 – AllenKll 2014-10-30 21:41:33