引入jetty依赖
<dependency> <groupId>org.eclipse.jetty.aggregate</groupId> <artifactId>jetty-webapp</artifactId> <version>${jetty.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-jsp</artifactId> <version>${jetty.version}</version> <scope>test</scope> </dependency>
配置maven jetty插件
<plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>${jetty.version}</version> <configuration> <scanIntervalSeconds>10</scanIntervalSeconds> <stopPort>9966</stopPort> <stopKey>foo</stopKey> <useTestClasspath>true</useTestClasspath> <webAppConfig> <contextPath>/${project.artifactId}</contextPath> </webAppConfig> <connectors> <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector"> <port>8088</port> <maxIdleTime>60000</maxIdleTime> </connector> </connectors> <systemProperties> <systemProperty> <name>spring.profiles.active</name> <value>development</value> </systemProperty> </systemProperties> </configuration> </plugin>
( 1 )配置 Jetty 容器(支持所有 goals )
<connectors> :可选参数,配置org.eclipse.jetty.server.Connector( Jetty 端口监听器)列表,如果不指定该参数,将会连接默认的 8080 端口。
<jettyXml> :可选参数,指定 Jetty 配置文件的路径
<scanIntervalSeconds>:可选参数,配置 Web 应用扫描的暂停时间, Web 应用扫描如果发现修改了程序,会自动重新部署。该参数默认值为 0 ,即不启动热部署
<systemProperties>:可选参数,设置插件执行时的系统参数,比如上面的配置示例中指定了 spring 的 profile 为development,如果不设置该参数,就需要配置 maven 与 spring 的 profile 一致,同时在 mvn 命令中增加 -P development选项,或者直接在 spring 配置文件中设置spring.profiles.active为development
<systemPropertiesFile>:可选参数,设置系统参数配置文件的位置,将会批量执行其中的系统参数配置
<loginServices>:可选参数,配置org.eclipse.jetty.security.LoginService实现类的列表。
<requestLog>:可选参数,配置请求日志接口:org.eclipse.jetty.server.RequestLog的实现类,配置请求日志的处理格式
( 2 )配置 Web 应用程序(不支持run-forked、stop两个 goals )
<webApp>:从jetty6.1.6rc0起使用webAppConfig, web 应用程序配置根节点
<contextPath>:设置 web 应用程序的 context 路径,默认情况下,它被设置成该项目的pom.xml的<artifactId>
<descriptor>:设置 web 应用程序主配置文件 web.xml 的路径,默认该配置文件位于 WEB-INF 目录下
<defaultsDescriptor>:设置先于 web.xml 执行的webdefault.xml配置文件的路径
<overrideDescriptor>:设置在 web.xml 读取之后执行的配置文件,使用该参数可以覆盖或增加 web.xml 中的配置
<tempDirectory>: Web 应用的临时目录, Jetty 可以在此目录编译 jsp 文件或者复制 jar 包,默认路径为${project.build.outputDirectory}/tmp
<baseResource>:指定 Web 应用静态资源所在的路径,默认路径为src/main/webapp
<resourceBases>:指定多个 Web 应用静态资源所在的路径,使用逗号分隔
<baseAppFirst>:可选参数,默认值为 true ,控制是否可以在 Web 应用的原始资源之前或之后叠加多个 war 包
<jettyEnvXml>:可选参数,指定jetty-env.xml配置文件的路径
<containerIncludeJarPattern>:jetty-8.1.x之后的版本可以使用,可选参数,配置加载到 Jetty 容器 Classloader 中的 Jar 包的路径或匹配模式,符合条件的 jar 包将会被检测META-INF、资源、 tld 和类的继承关系
<webInfIncludeJarPattern>:jetty-8.1.x之后的版本可以使用,可选参数,配置加载到 Web 应用程序的 Classloader (WEB-INF classpath )中的 Jar 包的路径或匹配模式,符合条件的 jar 包将会被检测META-INF、资源、 tld 和类的继承关系
<contextXml>:可选参数,指定context xml配置文件的路径
嵌入式jetty
package org.springside.examples.quickstart; import java.io.File; import java.net.URL; import java.security.ProtectionDomain; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.webapp.WebAppContext; /** * Main Class for standalone running. * * @author calvin */ public class Main { public static void main(String[] args) throws Exception { String contextPath = "/"; int port = Integer.getInteger("port", 8080); Server server = createServer(contextPath, port); try { server.start(); server.join(); } catch (Exception e) { e.printStackTrace(); System.exit(100); } } private static Server createServer(String contextPath, int port) { // use Eclipse JDT compiler System.setProperty("org.apache.jasper.compiler.disablejsr199", "true"); Server server = new Server(port); server.setStopAtShutdown(true); ProtectionDomain protectionDomain = Main.class.getProtectionDomain(); URL location = protectionDomain.getCodeSource().getLocation(); String warFile = location.toExternalForm(); WebAppContext context = new WebAppContext(warFile, contextPath); context.setServer(server); // 设置work dir,war包将解压到该目录,jsp编译后的文件也将放入其中。 String currentDir = new File(location.getPath()).getParent(); File workDir = new File(currentDir, "work"); context.setTempDirectory(workDir); server.setHandler(context); return server; } }
this.class.getProtectionDomain().getCodeSource().getLocation()
如果直接执行.class文件那么会得到当前class的绝对路径。
如果封装在jar包里面执行jar包那么会得到当前jar包的绝对路径。
通过以上配置,已经可以在 Web 应用程序内嵌入 Jetty 容器了,但还需要注意以下几点
Maven Pom 中的 Jetty 依赖注意 scope 修改为provided,防止 Jetty 的 Jar 包被打到WEB-INF/lib中。
如果需要解析 jsp 页面,需要在依赖中加入jsp-2.1-glassfish包的引用,注意其 scope 不能设置为provided
<dependency> <groupId>org.mortbay.jetty</groupId> <artifactId>jsp-2.1-glassfish</artifactId> <version>2.1.v20100127</version> </dependency>
简单:
运行war:
package com.google.code.garbagecan.jettystudy.sample6; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.webapp.WebAppContext; public class WebAppContextWithWarServer { public static void main(String[] args) throws Exception { Server server = new Server(8080); WebAppContext context = new WebAppContext(); context.setContextPath("/myapp"); context.setWar("E:/share/test/struts2-blank.war"); server.setHandler(context); server.start(); server.join(); } }
运行webApp
package com.google.code.garbagecan.jettystudy.sample6; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.webapp.WebAppContext; public class WebAppContextWithFolderServer { public static void main(String[] args) throws Exception { Server server = new Server(8080); WebAppContext context = new WebAppContext(); context.setContextPath("/myapp"); context.setDescriptor("E:/share/test/struts2-blank/WEB-INF/web.xml"); context.setResourceBase("E:/share/test/struts2-blank"); context.setParentLoaderPriority(true); server.setHandler(context); server.start(); server.join(); } }
相关推荐
Jetty使用总结
Java Web服务器Jetty使用手册
Jetty9 配置使用HTTPS证书,访问你的服务器更安全,更好的配置方法。
NULL 博文链接:https://szcs10138456.iteye.com/blog/1011072
jetty是什么 jetty配置 jetty使用 jetty嵌入 jetty启动 jetty部署 jetty教程 jetty嵌入式 jetty
jetty axis2.war文件在axis2-1.7.8-bin.zip下; jetty安装目录下的webapp文件夹下面需要用到此war,jetty才能支持axis2
Jetty cometd(Continuation)学习笔记,自己用的,别人那down的网页
Jetty 这个项目成立于 1995 年,现在已经有非常多的成功产品基于 Jetty,比如 Apache Geromino, JBoss, IBM Tivoli, Cisco SESM 等。Jetty 可以用来作为一个传统的 Web 服务器,也可以作为一个动态的内容服务器,...
引入到eclipse可以直接使用Starter类运行。 包含必备jar包:jetty-6.1.26.jar,jetty-util-6.1.26,servlet的相关jar包。 jetty入门级源代码。
jetty
jetty-security-9.4.8.v20171121.jar,jetty-io-9.4.8.v20171121.jar,jetty-continuation-9.4.8.v20171121.jar,jetty-client-9.4.8.v20171121.jar,jetty-jmx-9.4.8.v20171121.jar,jetty-plus-9.4.8.v20171121....
嵌入式jetty服务器开发的所有用到的jar包 包括基础jetty使用的和编译使用的ant的jar全都全了 一次性导入再也不用为找jetty依赖jar包而麻烦了
eclipse jetty插件,从...下载run-jetty-run.zip文件,解压后再编写个links文件丢到eclipse的dropins目录下即可,省去了使用eclipse update方式安装的麻烦。 link文件样例如: path=d:\\eclipse_plugins\\run-jetty-run
在javaweb项目中嵌入式使用jetty作为web服务器,下载即可运行
gradle的jetty插件使用例子,详细参考:http://blog.csdn.net/xiejx618/article/details/38307289
jetty 服务器使用
文章讲的很详细
jetty嵌入式服务器开发所必须的jar包,本人使用jetty版本为6.1.3,jar包总数为9个,来自jetty:commons-el-1.0.jar,jasper-compiler-5.5.15,jasper-compiler-jdt-5.5.15.jar,jasper-runtime-5.5.15.jar,jetty-...
Jetty配置支持HTTPS以及受信网站证书生成方式