HelloWorld之jetty运行
fmms
13年前
jetty是一个轻便的嵌入式servlet容器。其启动运行非常简单。eclipse下运行jetty容器有如下几步, <br /> 一、建一个普通的java工程 <br /> 二、把jetty需要的包导入工程分别是jetty-6.1.15.rc3.jar,jetty-util-6.1.15.rc3.jar,servlet-api-2.5-20081211.jar,core-3.1.1.jar,jsp-2.1-glassfish-9.1.1.B51.25.p1.jar,jsp-2.1-jetty-6.1.15.rc3.jar,jsp-api-2.1-glassfish-9.1.1.B51.25.p1.jar(本人下载的是jetty-6.1.15.rc3.zip,后边是个jar是为了使工程支持jsp文件而加入的) <br /> 三、建立web目录,一个名字叫做web的文件夹里面有WEB-INF文件夹和web.xml文件 <br /> web.xml文件内容如下: <br /> <pre class="brush:xml; toolbar: true; auto-links: false;"><?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app> </pre> <br /> 四、编写一个java类代码如下 <br /> Java代码 <br /> <pre class="brush:java; toolbar: true; auto-links: false;">package test; import org.mortbay.jetty.Connector; import org.mortbay.jetty.Server; import org.mortbay.jetty.nio.SelectChannelConnector; import org.mortbay.jetty.webapp.WebAppContext; public class Bootstart { public static void main(String[] args) throws Exception { Server service = new Server(); Connector connector = new SelectChannelConnector(); //设置端口 connector.setPort(8080); //设置host地址 connector.setHost("127.0.0.1"); service.setConnectors(new Connector[] { connector }); //设置根路径 WebAppContext context = new WebAppContext("web", "/web"); service.addHandler(context); service.setStopAtShutdown(true); service.setSendServerVersion(true); //启动服务 service.start(); service.join(); } } </pre> <br /> <pre class="brush:java; toolbar: true; auto-links: false;">package test; import org.mortbay.jetty.Connector; import org.mortbay.jetty.Server; import org.mortbay.jetty.nio.SelectChannelConnector; import org.mortbay.jetty.webapp.WebAppContext; public class Bootstart { public static void main(String[] args) throws Exception { Server service = new Server(); Connector connector = new SelectChannelConnector(); //设置端口 connector.setPort(8080); //设置host地址 connector.setHost("127.0.0.1"); service.setConnectors(new Connector[] { connector }); //设置根路径 WebAppContext context = new WebAppContext("web", "/web"); service.addHandler(context); service.setStopAtShutdown(true); service.setSendServerVersion(true); //启动服务 service.start(); service.join(); } } </pre> <br /> 五、测试的jsp文件index.jsp <br /> <pre class="brush:html; toolbar: true; auto-links: false;"><%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!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> <% request.setAttribute("name","sun"); System.out.println("My name is:" + request.getAttribute("name")); %> </body> </html> </pre> <br /> 六、运行main方法并测试连接http://127.0.0.1:8080/web <br />