[Maven问题总结]Jetty9的Maven配置——嵌入式服务器

不多说,直接上配置代码:

1、pom.xml

<dependencies>
		<dependency>
		  	<groupId>org.eclipse.jetty</groupId>
		  	<artifactId>jetty-jsp</artifactId>
		  	<version>9.1.3.v20140225</version>
		  	<type>pom</type>
		</dependency>
		<dependency>
		  	<groupId>org.eclipse.jetty</groupId>
		   	<artifactId>jetty-webapp</artifactId>
		   	<version>9.1.3.v20140225</version>
		   	<scope>provided</scope>
		</dependency>
	</dependencies>



2.WebServer.java,这里只给出最近本的代码,可以根据API文档进行很多设置,也可以通过jetty.xml配置。


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

public class WebServer {

    public static void main(String[] args) throws Exception{
	String projectPath = System.getProperty("user.dir");
	String[] fileName = projectPath.split("\\\\");
	// 工程名称
	String projectName = fileName[fileName.length - 1];
	// web资源路径
	String WebRoot = "src/main/webapp";
	// 端口号
	int port = 8080;
       Server server = new Server(port);
       WebAppContext webapp = new WebAppContext();
       webapp.setDefaultsDescriptor("src/main/resources/webdefault.xml");
       webapp.setDescriptor("src/main/webapp/web.xml");
       webapp.setContextPath("/"+projectName);
       webapp.setWar(WebRoot);
       server.setHandler(webapp);
       server.start();
       server.join();
    }
}




3.开发中会遇到静态文件被锁的情况,上面代码已加入相关设置,但是仍需要将webdefault.xml文件复制到工程中。

webdefault.xml修改方式:在jetty-webapp.jar中找到该文件,useFileMappedBuffer改为false即可。




转载于:https://my.oschina.net/wangchen881202/blog/208635

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值