一、Jetty 是一个开源的servlet容器,它为基于Java的web容器,例如JSP和servlet提供运行环境。Jetty是使用Java语言编写的,它的API以一组JAR包的形式发布。开发人员可以将Jetty容器实例化成一个对象,可以迅速为一些独立运行(stand-alone)的Java应用提供网络和web连接。
二、嵌入式的开发,可以将容器直接嵌入到代码中,这样启动容器,就不需要单独使用中间件了,比如:tomcat等
三、这里介绍两种jetty的启动方式,1:代码直接启动,2:spring的ClassPathXmlApplicationContext启动(推荐后一种启动方式)
四、具体的例子:
1、需要的依赖包
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.pinnet</groupId>
<artifactId>spring-jetty</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging> <dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>9.2.9.v20150224</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>9.2.9.v20150224</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-jsp</artifactId>
<version>9.2.9.v20150224</version>
</dependency>
</dependencies>
</project>
2、web.xml的配置,基本上没啥变化
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list> <servlet>
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/</url-pattern>
</filter-mapping>
</web-app>
3、启动方式:
1)代码手写启动
package com.pinnet.jetty; import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext; public class JettyStart { public static void main(String[] args) throws Exception {
//设置端口,可以设置多个端口
Server server = new Server();
//web应用参数设置
WebAppContext context = new WebAppContext();
//项目上下文
context.setContextPath("/");
//web.xml路径,主要是解析其中的配置
context.setDescriptor("./src/main/webapp/web.xml");
//静态资源路径
context.setResourceBase("./src/main/webapp");
context.setParentLoaderPriority(true);
//展示URL日志
context.setLogUrlOnStart(true);
server.setHandler(context);
server.start();
}
}
2)容器启动:
package com.pinnet.jetty; import org.springframework.context.support.ClassPathXmlApplicationContext; public class AppStart { public static void main(String[] args) {
new ClassPathXmlApplicationContext("spring-jetty.xml");
}
}
配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="webAppContext" class="org.eclipse.jetty.webapp.WebAppContext">
<property name="contextPath" value="/"/>
<property name="descriptor" value=".\src\main\webapp\web.xml"/>
<property name="resourceBase" value=".\src\main\webapp\"/>
<property name="parentLoaderPriority" value="true"/>
<property name="logUrlOnStart" value="true"/>
</bean> <bean id="server" class="org.eclipse.jetty.server.Server" init-method="start" destroy-method="stop">
<property name="connectors">
<list>
<bean class="org.eclipse.jetty.server.ServerConnector">
<constructor-arg ref="server"/>
<property name="port" value="8080"/>
</bean>
</list>
</property>
<property name="handler" ref="webAppContext"/>
</bean>
</beans>
具体:解释如代码启动一样!