设为首页 收藏本站
查看: 813|回复: 0

[经验分享] jetty作为内嵌服务器自启动

[复制链接]

尚未签到

发表于 2017-3-1 12:11:46 | 显示全部楼层 |阅读模式
  为了完成web工程的测试,最近内嵌jetty也要搞起来.第一次搞还是挺焦头烂额的.直接上成果:



package com.test.action;
import java.io.File;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.eclipse.jetty.webapp.WebAppContext;
public class RunTest {
public static void main(String[] args) {
tt();
}
public static void tt() {
Server server = new Server();// 创建jetty web容器
server.setStopAtShutdown(true);// 在退出程序是关闭服务
// 创建连接器,每个连接器都是由IP地址和端口号组成,连接到连接器的连接将会被jetty处理
Connector connector = new SelectChannelConnector();// 创建一个连接器
connector.setHost("127.0.0.1");// ip地址
connector.setPort(8080);// 连接的端口号
server.addConnector(connector);// 添加连接
QueuedThreadPool threadPool = new QueuedThreadPool();
threadPool.setMaxThreads(3000);
server.setThreadPool(threadPool);
// 配置服务
WebAppContext context = new WebAppContext();// 创建服务上下文
context.setContextPath("/strutsDemo");// 访问服务路径 http://{ip}:{port}/
context.setConfigurationDiscovered(true);
String baseDir = Thread.currentThread().getContextClassLoader()
.getResource("").getPath();
context.setDescriptor(baseDir + File.separator + "/WEB-INF/web.xml");// 指明服务描述文件,就是web.xml
// context.setDescriptor("/E:/workspace/strutsDemo/target/classes/\\/WEB-INF/web.xml");//
// 指明服务描述文件,就是web.xml
context.setResourceBase(System.getProperty("user.dir")
+ "/src/main/webapp/");// 指定服务的资源根路径,配置文件的相对路径与服务根路径有关
server.setHandler(context);// 添加处理try {
server.start();// 开启服务
            server.join();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
}
  启动访问就可以了.主要是context的配置花了很多功夫,老是配不好.还有据说jetty版本或者jar包不同会有配置差异,我用的是8.0.4版本,顺便贴上maven依赖:



<!--jetty -->
<dependency>
<groupId>org.eclipse.jetty.aggregate</groupId>
<artifactId>jetty-all</artifactId>
<version>8.0.4.v20111024</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>8.0.4.v20111024</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>8.0.4.v20111024</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>8.0.4.v20111024</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-deploy</artifactId>
<version>8.0.4.v20111024</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-xml</artifactId>
<version>8.0.4.v20111024</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-security</artifactId>
<version>8.0.4.v20111024</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-client</artifactId>
<version>8.0.4.v20111024</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-ajp</artifactId>
<version>8.0.4.v20111024</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-annotations</artifactId>
<version>8.0.4.v20111024</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-websocket</artifactId>
<version>8.0.4.v20111024</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-util</artifactId>
<version>8.0.4.v20111024</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-io</artifactId>
<version>8.0.4.v20111024</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-http</artifactId>
<version>8.0.4.v20111024</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-continuation</artifactId>
<version>8.0.4.v20111024</version>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jsp-2.1-glassfish</artifactId>
<version>2.1.v20100127</version>
</dependency>
  事实上不需要这么多,我这是一劳永逸的偷懒做法,你们可以改进哟

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-348812-1-1.html 上篇帖子: maven中jetty插件配置 下篇帖子: maven jetty 配置
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表