在eclipse下面建一个java project
建立目录结构如下:
二级目录:
(备注jetty_test是工程的根目录,etc、lib、webRoot为其二级目录)
到jetty的官方网站(http://www.eclipse.org/jetty/)下载jetty的开发包;
我下载的jetty-hightide-8.0.0.v20110901.tar.gz;
把里面的lib目录的jar包和lib/jsp目录下面的jar包导入到工程里面
说明白,其实jetty.xml和webdefault.xml其实是在我下载的
jetty-hightide-8.0.0.v20110901.tar.gz里面的etc目录下面的jetty.xml和webdefault.xml
jetty.xml:
View Code
webdefault.xml:
View Code
而webdefault.xml基本可以不改,因为它是用来标识文件的语言格式,基本的一些配置;
jetty.xml我们可以做一定的修改;
例如说我们可以改下服务监听的端口号,如图:
web.xml:
(备注就是web请求的配置信息,例如说设置servlet的路由)
如下面配置:
View Code
好!我们现在就来建设一个如上面web.xml配置的servlet和index.jsp;
index.jsp页面
Index.jsp:
<%@ page language="java" contentType="text/html; charset=gb2312" 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=ISO-8859-1"><title>Insert title here</title></head><body>hello jetty! 中国!</body></html>
HelloWorldServlet.java文件:
View Code
JettyCustomServer:总的服务类,本质上是一个分发器
View Code
JettyServerStart.java:启动类,入口
package org.jetty.demo;//启动类,入口public class JettyServerStart { public static void main(String[] args) { JettyCustomServer server = new JettyCustomServer( "./jetty/etc/jetty.xml", "/testContext"); server.startServer(); }}
运行这个类相当于启动了整个工程:
访问index.jsp
http://localhost:8080/testContext/index.jsp
访问HelloWorldServlet: