使用jetty服务器比tomcat服务器更方便,更快捷,但还是tomcat更适合,不过在某些时候使用jetty还是很不错的。

首先是建立一个webapp的maven项目

在eclipse中使用jetty服务器

这里不管直接下一步。

在eclipse中使用jetty服务器

选择maven-archetype-webapp,然后下一步。

在eclipse中使用jetty服务器

输入好group id、artifact id和package 点击finish。

然后在pom.xml中加入plugin。

<plugin>
				<groupId>org.mortbay.jetty</groupId>
				<artifactId>jetty-maven-plugin</artifactId>
				<configuration>
					<scanIntervalSeconds>5</scanIntervalSeconds>
					<connectors>
						<connector
							implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
							<port>8080</port>
							<maxIdleTime>60000</maxIdleTime>
						</connector>
					</connectors>
					<webAppConfig>
					<!-- 默认路径是 / 加了jettytest后访问变成http://localhost:8080/jettytest/index.jsp
						 默认是:  http://localhost:8080/index.jsp
					-->
						<contextPath>/jettytest</contextPath>
					</webAppConfig>
				</configuration>
			</plugin>

jetty如果还有其他需要的配置,那么就在这个pom.xml中自己配置。

然后是点击运行,选择maven build...进行运行设置。

在eclipse中使用jetty服务器

在Goals中输入jetty:run后点击下面的run。

在eclipse中使用jetty服务器

然后看日志里面是否有报错,没有报错说明启动成功。

在eclipse中使用jetty服务器

在console中的日志还有端口等信息。

然后在浏览器中访问测试是否成功。

http://localhost:8080/jettytest/index.jsp

在eclipse中使用jetty服务器

在eclipse中使用jetty服务器

访问成功说明配置正确。虽然jetty没有tomcat更适合,但是在其它方面还是比tomcat优秀的。

相关文章: