jetty简介

         jetty分两种Jetty@eclipseJetty@codehaus,这两种也都可以在maven库里面http://repo2.maven.org/maven2/org/eclipse/jetty/jetty-distribution/。下载很方便。个人感觉两种有不同之处,但是大体差不多。

         jetty有个口号,不要部署你的应用到jetty,让jetty部署到你的应用上。这就意味着你的应用不是一个传统的war包部署在jetty上面,jetty被设计成一个软件控件,他可以被应用到任何POJO,也就是说可以在应用程序上加上http模块,而不是把应用放到http服务器了。也就是说你可以用java写个main方法都能起个服务。非常方便。光想想就很诱人了。对于小服务(强调下是很小的服务)非常方便。

            附上一个jetty的helloword,使用handler

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;


public class HelloHandler extends AbstractHandler {
    public void handle(String target, Request baseRequest,
	    HttpServletRequest request, HttpServletResponse response)
	    throws IOException, ServletException {
	response.setContentType("text/html;charset=utf-8");
	response.setStatus(HttpServletResponse.SC_OK);
	baseRequest.setHandled(true);
	response.getWriter().println("<h1>Hello World,,ooo</h1>");
    }
    
    public static void main(String[] args) throws Exception
    {
        Server server = new Server(8081);
        server.setHandler(new HelloHandler());
     
        server.start();
        server.join();
    }
}
依赖的jar包jetty下载文件都有,当然也可以不手动依赖jar包,使用maven最好,强烈推荐使用maven。附上maven的配置。

<project xmlns="http://maven.apache.org/POM/4.0.0" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.example</groupId>
  <artifactId>hello-world</artifactId>
  <version>0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>Jetty HelloWorld</name>
 
  <properties>
    <jettyVersion>7.2.0.v20101020</jettyVersion>
  </properties>
 
  <dependencies>
    <dependency>
      <groupId>org.eclipse.jetty</groupId>
      <artifactId>jetty-server</artifactId>
      <version>${jettyVersion}</version>
    </dependency>
  </dependencies>
 
  <build>
    <plugins>
      <plugin>
        <!-- This plugin is needed for the servlet example -->
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <version>${jettyVersion}</version>
      </plugin>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.1</version>
        <executions>
          <execution><goals><goal>java</goal></goals></execution>
        </executions>
        <configuration>
          <mainClass>org.example.HelloWorld</mainClass>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>
main方法跑起来后,访问http://localhost:8081/连接就能在页面上看到我们程序员钟爱的 Hello World了。。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值