upnp可能会使用独立服务器
原文出处:http://blog.chenlb.com/2009/01/quick-start-jetty-and-embed-in-project.html
<!-- google_ad_section_start -->
看到开源项目发布的时候都带一个 jsp 容器(jetty)。拿来做 demo、开发、调试的服务器还是很不错的。今天就小试下,主要把它运行起来。
第一步下载:http://dist.codehaus.org/jetty/jetty-6.1.14/jetty-6.1.14.zip�0�2是目前最新的稳定版。解压到如E:\jetty-6.1.14,其中比较重要的目录是:etc、contexts、webapps。个人认为可以类比tomcat的conf、conf\Catalina\localhost、webapps目录。contexts是热部署用的。
试运行下,可以把一个简单的web项目到到webapps目录下,或是*.war,如:web-demo(或web-demo.war)放到webapps目录下。
附件是demo,自己抽离的。
E:\jetty-6.1.14>java -jar start.jar
2009-01-13 15:34:38.937::INFO: Logging to STDERR via org.mortbay.log.StdErrLog
2009-01-13 15:34:39.265::INFO: jetty-6.1.14
2009-01-13 15:34:39.421::INFO: Deploy E:\jetty-6.1.14\contexts\javadoc.xml -> org.mortbay.jetty.handler.ContextHandler@15cda3f{/javadoc,file:/E:/jetty-6.1.14/javadoc/}
...
2009-01-13 15:35:01.828::INFO: Opened E:\jetty-6.1.14\logs\2009_01_13.request.log
2009-01-13 15:35:01.953::INFO: Started SelectChannelConnector@127.0.0.1:8080
打开:http://localhost:8080/web-demo ,恩有结果了。
如果不把web-demo放到webapps目录下也可,在contexts目录下建立一个文件告诉jetty就行了。可以在contexts目录下复制test.xml为web-demo.xml,然后修改如下:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">
<Configure class="org.mortbay.jetty.webapp.WebAppContext">
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<!-- Required minimal context configuration : -->
<!-- + contextPath -->
<!-- + war OR resourceBase -->
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<Set name="contextPath">/web-demo</Set>
<Set name="war">e:/workspace/web-demo/WebContent</Set>
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<!-- Optional context configuration -->
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<Set name="extractWAR">false</Set>
<Set name="copyWebDir">false</Set>
<Set name="defaultsDescriptor">
<SystemProperty name="jetty.home" default="." />/etc/webdefault.xml</Set>
<!--
<Set name="overrideDescriptor"><SystemProperty name="jetty.home"
default="."/>/contexts/test.d/override-web.xml</Set>
-->
</Configure>
war可以设置成绝对路径。然后再重启jetty试试看。好了, 对jetty的初步认识到这里。
下面再来看下,怎么把它放到项目中。现我有一示例项目 e:/workspace/web-demo(称为project_home),里面的Web根目录是WebContent。
在project_home建一个jetty目录,子目录如:contexts、etc、lib。
把${jetty_home}/etc目录下的jetty.xml、webdefault.xml文件复制到${project_home}/jetty/etc目录中。
把${jetty_home}/lib/jsp-2.1目录复制到${project_home}/jetty/lib目录下(如果不复制jsp-2.1或jsp-2.0也可以正常启动,只是不能解析jsp,打开主页时提示 JSP not support)。
同样把jetty-6.1.14.jar、jetty-util-6.1.14.jar、servlet-api-2.5-6.1.14.jar复制到${project_home}/jetty/lib目录下。
把${jetty_home}/start.jar复制到${project_home}/jetty目录下。
接下来在${project_home}/jetty/contexts目录下加一个文件。可以是上面给出的web-demo.xml。不过war 可以改为
<Set name="war"><SystemProperty name="jetty.home" default="."/>/../WebContent</Set>
注释${project_home}/jetty/etc/jetty.xml文件中的:
<!--
<Item>
<New id="RequestLog" class="org.mortbay.jetty.handler.RequestLogHandler" />
</Item>
-->
<!--
<Call name="addLifeCycle">
<Arg>
<New class="org.mortbay.jetty.deployer.WebAppDeployer">
<Set name="contexts"><Ref id="Contexts" /></Set>
<Set name="webAppDir"><SystemProperty name="jetty.home" default="." />/webapps</Set>
<Set name="parentLoaderPriority">false</Set>
<Set name="extract">true</Set>
<Set name="allowDuplicates">false</Set>
<Set name="defaultsDescriptor"><SystemProperty name="jetty.home" default="." />/etc/webdefault.xml</Set>
</New>
</Arg>
</Call>
-->
<!--
<Item>
<New class="org.mortbay.jetty.security.HashUserRealm">
<Set name="name">Test Realm</Set>
<Set name="config"><SystemProperty name="jetty.home" default="." />/etc/realm.properties</Set>
<Set name="refreshInterval">0</Set>
</New>
</Item>
-->
<!--
<Ref id="RequestLog">
<Set name="requestLog">
<New id="RequestLogImpl" class="org.mortbay.jetty.NCSARequestLog">
<Set name="filename"><SystemProperty name="jetty.logs" default="./logs" />/yyyy_mm_dd.request.log</Set>
<Set name="filenameDateFormat">yyyy_MM_dd</Set>
<Set name="retainDays">90</Set>
<Set name="append">true</Set>
<Set name="extended">false</Set>
<Set name="logCookies">false</Set>
<Set name="LogTimeZone">GMT</Set>
</New>
</Set>
</Ref>
-->
RequestLog 是访问记录,不注释也可,前提就是有logs目录。
addLifeCycle 不注释也行,前提是有webapps目录。
Test Realm 不注释也是,前提是etc目录下有realm.properties。
配置好了,现在到E:\workspace\web-demo\jetty,运行java -jar start.jar 可以启动这个项目了。
代码启动jetty
public static void main(String[] args) throws Exception {
Server server = new Server();
BoundedThreadPool threadPool = new BoundedThreadPool();
threadPool.setMaxThreads(100);
server.setThreadPool(threadPool);
Connector connector = new SelectChannelConnector();
connector.setPort(8080); //端口
server.setConnectors(new Connector[] { connector });
WebAppContext context = new WebAppContext("你的web应用路径", "你的context");
server.addHandler(context);
server.setStopAtShutdown(true);
server.setSendServerVersion(true);
server.start();
server.join();
}
xml配置文件启动jetty
先创建一个jetty-web.xml,就放在你的webapp目录下吧
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">
<Configure id="Server" class="org.mortbay.jetty.Server">
<Set name="ThreadPool">
<New class="org.mortbay.thread.QueuedThreadPool">
<Set name="minThreads">10</Set>
<Set name="maxThreads">200</Set>
<Set name="lowThreads">20</Set>
<Set name="SpawnOrShrinkAt">2</Set>
</New>
</Set>
<Call name="addConnector">
<Arg>
<New class="org.mortbay.jetty.nio.SelectChannelConnector">
<Set name="host"><SystemProperty name="jetty.host" default="127.0.0.1"/></Set>
<Set name="port"><SystemProperty name="jetty.port" default="8080"/></Set>
<Set name="maxIdleTime">30000</Set>
<Set name="Acceptors">2</Set>
<Set name="statsOn">false</Set>
<Set name="confidentialPort">8443</Set>
<Set name="lowResourcesConnections">5000</Set>
<Set name="lowResourcesMaxIdleTime">5000</Set>
</New>
</Arg>
</Call>
<Set name="handler">
<New id="Handlers" class="org.mortbay.jetty.handler.HandlerCollection">
<Set name="handlers">
<Array type="org.mortbay.jetty.Handler">
<Item>
<New id="RequestLog" class="org.mortbay.jetty.handler.RequestLogHandler"/>
</Item>
<Item>
<New class="org.mortbay.jetty.webapp.WebAppContext">
<Set name="contextPath">/demo</Set>
<Set name="war">xx</Set> <!--你的web应用根目录-->
</New>
</Item>
</Array>
</Set>
</New>
</Set>
<Ref id="RequestLog">
<Set name="requestLog">
<New id="RequestLogImpl" class="org.mortbay.jetty.NCSARequestLog">
<Set name="filename"><SystemProperty name="jetty.logs"
/>/yyyy_mm_dd.request.log</Set>
<Set name="filenameDateFormat">yyyy_MM_dd</Set>
<Set name="retainDays">90</Set>
<Set name="append">true</Set>
<Set name="extended">true</Set>
<Set name="logCookies">false</Set>
<Set name="LogTimeZone">GMT</Set>
</New>
</Set>
</Ref>
<Set name="stopAtShutdown">true</Set>
<Set name="sendServerVersion">true</Set>
<Set name="sendDateHeader">true</Set>
<Set name="gracefulShutdown">1000</Set>
</Configure>
以上是一个相对完整的jetty.xml,应该可以根据需要简化,重要的是加入了
<Item>
<New class="org.mortbay.jetty.webapp.WebAppContext">
<Set name="contextPath">/demo</Set>
<Set name="war">xx</Set> <!--你的web应用根目录-->
</New>
</Item>
一个新的handler,实现了web应用发布目录和context的指定。
这个时候,如果在程序中启动jetty,只需要:
public static void main(String[] args) throws Exception {
Server server = new Server();
XmlConfiguration configuration = new XmlConfiguration(
new FileInputStream(
"xx/demo/jetty-web.xml")); //指定自定义的jetty.xml路径
configuration.configure(server);
server.start();
}
其实连这个main函数都可不必要了,只要用org.mortbay.xml.XmlConfiguration作为启动类,Program arguments中填上自定义的jetty.xml即可。这样连eclipse插件都可以丢掉不用了。
注意:
用嵌入式启动的方法会碰到NO JSP Support的异常
导入jetty-6.1.14\lib\jsp-2.1下面的包到环境里面就可以了。
分享到:
相关推荐
jetty 超轻松入门,按步奏就可以学习,下载就可以运行
NULL 博文链接:https://jose-bing.iteye.com/blog/1240881
Jetty 是一个开源的servlet容器,它为...Jetty是使用Java语言编写的,它的API以一组JAR包的形式发布。开发人员可以将Jetty容器实例化成一个对象,可以迅速为一些独立运行(stand-alone)的Java应用提供网络和web连接。
jetty快速入门与嵌入使用,简单、易懂,轻松学习!
资源名称:Jetty6入门教程资源截图: 资源太大,传百度网盘了,链接在附件中,有需要的同学自取。
引入到eclipse可以直接使用Starter类运行。 包含必备jar包:jetty-6.1.26.jar,jetty-util-6.1.26,servlet的相关jar包。 jetty入门级源代码。
Jetty入门(视频) 下载和安装Jetty 如何安装一个Jetty包 如何配置Jetty – 主要文档 如何运行Jetty 用JConsole监控Jetty 如何使用Jetty开发 Jetty HelloWorld教程 Jetty和Maven HelloWorld教程 Jetty(6)入门 ...
Hello Jetty 入门简单实例!可直接运行!
maven入门使用 maven创建web项目 maven生成eclipse项目 maven+jetty的使用 jetty在eclipse的配置和使用
里面包含的有jetty嵌入开发的全部jar包,还有一个jetty实战项目,非常适合刚入门的童鞋观看
使用 Maven、Jetty 和 Jersey 的入门代码 使用 maven-shade-plugin 构建一个带有依赖项的胖 jar 使用单一资源在 8080 上运行码头服务器 建造 安装 跑步 java -jar target/jetty-app-{{version}}.jar 测试资源
jetty7 核心架构 细致分析 入门到精通
该包适用于maven新手入门,因为作者本身也是一员maven新手,里面包括了maven的下载包,插件包,插件的配置,环境变量的设置,maven项目建立的详解及运行,里面集成了jetty,tomcat,struts2,当然还包括了直接运行jar包...
Intellij Idea使用入门,包括如何在web项目里集成maven、svn及使用jetty
J2EE入门级指导书
Maven使用实战-从入门到精通实践【张振华.Jack】 Maven简介、Maven主要做什么用? 项目对象模型(POM) 坐标(Coordinates)、打包方式(Packaging)、项目描述 项目子父级关系(parent) 依赖管理(dependency),冲突了...
使用到的外部jar包有(必须的部分,需要加到Web容器中) •neethi-3.0.2.jar •jsr311-api-1.1.1.jar •cxf-bundle-2.6.0.jar 使用到的外部jar包有(可选的部分,当且仅当作为一个独立的application运行时) ...
如果您使用Guice作为依赖项注入框架,并且想将自己的类注入到您创建的JAX-RS类中(例如“资源”和“过滤器”),则需要弥合这两个DI框架之间的鸿沟。 该模块旨在通过启动基于Jetty的Jersey服务器并初始化HK2与Guice...