心血来潮,决定使用Jetty来作为新的web容器.于是到网上http://download.eclipse.org/jetty/下载了Jetty7,由于8/9两个版本都支持Servlet3.0规范了.而现在绝大多数生产环境的Websphere都还是采用7系列,即Servlet2.5规范.所以我只下载了Jetty7.
创建环境,使用eclipse新建一个动态Web工程.注意在勾选的时候不要选择目标容器,因为我这里要使用的是嵌入的方式启动.如图所示:
将Jetty7自带的jar包都引用到工程中,这里要特别注意把lib/jsp/子包下面的jar也要引用进来,否则不会编译.jsp文件.如图所示:
编写启动类:
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;
public class JettyStarter {
public static void main(String[] args) throws Exception {
// 服务器的监听端口
Server server = new Server(8080);
// 关联一个已经存在的上下文
WebAppContext context = new WebAppContext();
// 设置描述符位置
context.setDescriptor("./WebContent/WEB-INF/web.xml");
// 设置Web内容上下文路径
context.setResourceBase("./WebContent");
// 设置上下文路径
context.setContextPath("/jetty");
context.setParentLoaderPriority(true);
server.setHandler(context);
// 启动
server.start();
server.join();
}
}
在/WebContent/目录下,创建一个默认的jsp页面
然后启动这个启动器,访问浏览器如图: