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

[经验分享] JETTY嵌入式Web容器的开发(一)---基础开发

[复制链接]

尚未签到

发表于 2017-2-27 07:53:09 | 显示全部楼层 |阅读模式
qiuJETTY嵌入式Web容器的开发(一)---基础开发
JETTY嵌入式Web容器体积小,启动速度快,性能好,免费开源,是一款很适合开发调试和简单演示的轻量级Web容器.
而且它的嵌入式的特点,使得开发者可以直接将容器的操作包含在程序逻辑里,得以摆脱TOMCAT,JBOSS等独立容器带来的安装,维护,部署等一系列令人头疼的问题.
JETTY嵌入式开发步骤

  • 下载
本文采用的是2012年最新版本的JETTY8 (8.1.4版)
jetty-hightide-8.1.4.v20120524.zip

  • ECLIPSE建立一个普通JAVA项目testjetty
注意:
是JAVA  APPLICATION项目,不是WAR项目.
JDK要1.6
在项目里建立jetty目录
目录下再建立etc目录
将jetty-hightide-8.1.4.v20120524.zip包中etc目录下的jetty.xml和webdefault.xml文件拷贝入新建的 /jetty/etc目录下
将jetty-hightide-8.1.4.v20120524.zip文件中lib/目录下的所有jar文件和lib/jsp子目录下的所有jar文件都加入项目的buildPath
(很多讨论JETTY开发的文章总是在最小JAR包数上做文章,我觉得初学者还是应该将所有JAR包都加入,因为作为一款Web 容器,JETTY的JAR容纳了大量的细节功能. 有的JAR包你没有加入,也许测HELLOWORLD这类功能能够通过,但在真正的商用页面里,就会针对各种HTTP元素出问题)
在项目里建webRoot目录
webRoot目录下建立WEB-INF目录,以后web.xml就放在这里
建成的项目的结构如下:


  • 修改/jetty/etc/jetty.xml
我做了下面两处修改(粗体),以利于测试
"1.0"?>
 
"Server" class="org.eclipse.jetty.server.Server">
"ThreadPool">
 
"org.eclipse.jetty.util.thread.QueuedThreadPool">
"minThreads">30
"maxThreads">200
"detailedDump">false
 
 
"addConnector">
 
"org.eclipse.jetty.server.nio.SelectChannelConnector">
"host">"jetty.host" />
"port">"jetty.port" default="8088"/>
"maxIdleTime">300000
"Acceptors">2
"statsOn">false
"confidentialPort">8443
"lowResourcesConnections">20000
"lowResourcesMaxIdleTime">5000
 
 
 

  • JETTY  Service类
package org.jetty.demo;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.commons.lang.StringUtils;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.xml.XmlConfiguration;
import org.xml.sax.SAXException;
public class JettyCustomServer extends Server {
private String xmlConfigPath;
private String contextPath;
private String warPath;
private String resourceBase = "./webRoot";
private String webXmlPath = "./webRoot/WEB-INF/web.xml";
public JettyCustomServer(String xmlConfigPath,String contextPath,String resourceBase,String webXmlPath) {
this(xmlConfigPath,contextPath,resourceBase,webXmlPath,null);
}
public JettyCustomServer(String xmlConfigPath,String contextPath) {
this(xmlConfigPath,contextPath,null,null,null);
}
public JettyCustomServer(String xmlConfigPath,String contextPath,String warPath) {
this(xmlConfigPath,contextPath,null,null,warPath);
}
public JettyCustomServer(String xmlConfigPath,String contextPath,String resourceBase,String webXmlPath,String warPath) {
super();
if(StringUtils.isNotBlank(xmlConfigPath)){
this.xmlConfigPath = xmlConfigPath;
readXmlConfig();
}
if(StringUtils.isNotBlank(warPath)){
this.warPath = warPath;
if(StringUtils.isNotBlank(contextPath)){
this.contextPath = contextPath;
applyHandle(true);
}
}else{
if(StringUtils.isNotBlank(resourceBase))
this.resourceBase = resourceBase;
if(StringUtils.isNotBlank(webXmlPath))
this.webXmlPath = webXmlPath;
if(StringUtils.isNotBlank(contextPath)){
this.contextPath = contextPath;
applyHandle(false);
}
}
}
private void readXmlConfig(){
try {
XmlConfiguration configuration =  new XmlConfiguration(new FileInputStream(this.xmlConfigPath));
configuration.configure(this);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (SAXException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public void applyHandle(Boolean warDeployFlag){
ContextHandlerCollection handler = new ContextHandlerCollection();
WebAppContext webapp = new WebAppContext();
webapp.setContextPath(contextPath);
webapp.setDefaultsDescriptor("./jetty/etc/webdefault.xml");
if(!warDeployFlag){
webapp.setResourceBase(resourceBase);
webapp.setDescriptor(webXmlPath); 
}else{
webapp.setWar(warPath);
}
handler.addHandler(webapp);
super.setHandler(handler);
}
public void startServer(){
try {
super.start();
System.out.println("current thread:"+super.getThreadPool().getThreads()+"| idle thread:"+super.getThreadPool().getIdleThreads());
super.join();
} catch (Exception e) {
e.printStackTrace();
}
}
public String getXmlConfigPath() {
return xmlConfigPath;
}
public void setXmlConfigPath(String xmlConfigPath) {
this.xmlConfigPath = xmlConfigPath;
}
public String getContextPath() {
return contextPath;
}
public void setContextPath(String contextPath) {
this.contextPath = contextPath;
}
public String getResourceBase() {
return resourceBase;
}
public void setResourceBase(String resourceBase) {
this.resourceBase = resourceBase;
}
public String getWebXmlPath() {
return webXmlPath;
}
public void setWebXmlPath(String webXmlPath) {
this.webXmlPath = webXmlPath;
}
public String getWarPath() {
return warPath;
}
public void setWarPath(String warPath) {
this.warPath = warPath;
}
}

  • 做一个简单的可执行的服务启动类
package org.jetty.demo;
public class JettyServerStart {
public static void main(String[] args) {
JettyCustomServer server = newJettyCustomServer("./jetty/etc/jetty.xml","/testContext");
server.startServer();
}
}

  • /webRoot/目录下做一个JSP测试页面
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
 
 
 
"Content-Type" content="text/html; charset=GBK">
Insert title here
 
 
hello jetty! 中国!
 
 

  • 做一个测试Servlet
package org.jetty.demo;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
public class HelloWorldServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println("doGet");
resp.getWriter().write("hello world!");
resp.getWriter().close();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println("doPost");
super.doPost(req, resp);
}
}

  • /webRoot/WEB-INF目录下建立一个web.xml
"1.0" encoding="UTF-8"?>
"http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
 
 
This is the description of my J2EE component
This is the display name of my J2EE component
HelloWorldServlet
org.jetty.demo.HelloWorldServlet
 
 
HelloWorldServlet
/servlet/HelloWorldServlet
 
 
index.jsp
 
 


  • 启动服务
执行JettyServerStart
CONSOLE日志:
2012-06-27 15:49:35.250:INFO:oejs.Server:jetty-8.1.4.v20120524
2012-06-27 15:49:35.484:INFO:oejsh.ContextHandler:started o.e.j.w.WebAppContext{/testContext,file:/D:/RND/javaathome/eclipsehelio/testjetty/webRoot/}
2012-06-27 15:49:35.484:INFO:oejsh.ContextHandler:started o.e.j.w.WebAppContext{/testContext,file:/D:/RND/javaathome/eclipsehelio/testjetty/webRoot/}
2012-06-27 15:49:35.703:INFO:oejs.AbstractConnector:Started SelectChannelConnector@0.0.0.0:8088
current thread:30| idle thread:26
看红字部分,记得我们在./jetty/etc/jetty.xml文件中做的修改么?
测试
测试访问JSP页面
http://localhost:8088/testContext/index.jsp

测试访问SERVLET
http://localhost:8088/testContext/servlet/HelloWorldServlet

  refurl:http://my.oschina.net/u/989066/blog/157073

运维网声明 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-347617-1-1.html 上篇帖子: jetty启动后无法修改静态文件的问题 下篇帖子: 纠结的mod_jk与jetty的组合
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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