时间能够带走一切 ---路html
jetty是一个轻量级的servlet容器,有点相似tomcat,可是用起来比tomcat方便。java
jetty下载连接:http://download.eclipse.org/jetty/web
个人开发环境 :浏览器
操做系统:UBuntu 14.04 LTStomcat
集成工具:Eclipse(Version: Luna Service Release 1 (4.4.1) Build id: 20140925-1800)app
jetty版本:jetty-9.2.6eclipse
下面让咱们进入Hello World之旅吧。webapp
打开eclipse,在【Package Explorer】中单击右键【New】-->【Java Project】,在弹出框中填上工程名称“jettyWebApp”,【Finish】。 如图是建好的工程jsp
按图所示,建好jetty目录和webRoot目录。工具
工程建好了,如今最重要的不要忘了包依赖,在下载的jetty包中找到以下jar包,并导入包(Referenced Libraries)。
加上javax.el-3.0.0.jar(截图把它截掉了)
如下是源码
Start.java
package start; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.webapp.WebAppContext; public class Start { public static void main(String[] args){ Server server = new Server(8080); WebAppContext context = new WebAppContext(); context.setContextPath("/jettyWebApp"); context.setDescriptor("webRoot/WEB-INF/web.xml"); context.setResourceBase("webRoot"); context.setParentLoaderPriority(true); server.setHandler(context); try { server.join(); server.start(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
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" 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"> <display-name>jettyWebApp</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> hello jetty! 中国! </body> </html>
到此就部署完了。
打开浏览器,在地址栏输入:http://localhost:8080/jettyWebApp/