main方法启动jetty java嵌入式web服务器jetty的使用

本文介绍了如何通过main方法启动Jetty服务器,详细讲解了启动过程中需要注意的导包问题和配置路径。通过导入必要的依赖并设置服务器监听端口、上下文路径,成功实现了Jetty服务器的启动。示例代码展示了如何创建Server对象、配置WebAppContext,并给出运行后的请求路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天本来是想搞个SuperDiamond配置服务器的搭建,看到他使用jetty做web容器,就先来研究下jetty。搞个jetty启动的demo


jetty的启动有三种方式,我这里用main方法直接启动的方式


要解决两个问题:

1.导包要全

<dependency>
   <groupId>org.eclipse.jetty.aggregate</groupId>
   <artifactId>jetty-all-server</artifactId>
   <version>8.2.0.v20160908</version>
</dependency>


      <dependency>
         <groupId>org.eclipse.jetty</groupId>
         <artifactId>jetty-jsp</artifactId>
         <version>8.2.0.v20160908</version>
      </dependency>

导入上面的两包即可,还看到有说这个包跟tomcat的包有冲突,要是用tomcat启动最好包这些包注释了。


2.路径问题

package com.iflytek.start;


import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;


public class Main {


public static void main(String[] args) {


// 服务器的监听端口  
        Server server = new Server(9999);  
        // 关联一个已经存在的上下文  
        WebAppContext context = new WebAppContext();  
        // 设置描述符位置  
        context.setDescriptor("./src/main/webapp/WEB-INF/web.xml");     
        // 设置Web内容上下文路径  
        context.setResourceBase("./src/main/webapp");  
        // 设置上下文路径  
        context.setContextPath("/JettyDemo");  
        context.setParentLoaderPriority(true);  
        server.setHandler(context);  


try {
server.start();
// server.join();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("server is  start");


}


}



最后附上请求路径:http://localhost:9999/JettyDemo/index.jsp

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值