Embedding Jetty

Embedding Jetty
1、所需的jar包(jsp2.1):
  • servlet-api-2.5-6.x.jar
  • jetty-util-6.x.jar
  • jetty-6.x.jar
  • ant-1.6.5.jar
  • core-3.1.1.jar
  • jsp-2.1.jar
  • jsp-api-2.1.jar
2、示例代码片段:
    //设置web根目录
    String rootDirectory 
=  System.getProperty( " user.dir " );
    Context context 
=   new  WebAppContext(contexts, rootDirectory + " /webapp " , " / " );
    context.setWelcomeFiles(
new  String[]{ " index.jsp " });

    //
端口 监听
    
int  port  =   80 ;
    Server server 
=   new  Server();
    Connector httpConnector 
=   new  SelectChannelConnector();
    httpConnector.setHost(
null );
    httpConnector.setPort(port);
    server.addConnector(httpConnector);

    //设置Handler
    server.setHandlers(
new  Handler[] { contexts,  new  DefaultHandler() });
    ServletHandler handler 
=   new  ServletHandler();
    handler.addServletWithMapping(HttpDemoServlet.
class " / " );
    ContextHandler contextHandler 
=   new  ContextHandler(contexts,  " /httpdemo " );
    contextHandler.setHandler(handler);
    
    
try  {
        //启动服务.
        server.start();
    }
    
catch  (Exception e) {
        e.printStackTrace();
    }

你可能感兴趣的:(Embedding Jetty)