你好,游客 登录 注册 搜索
背景:
阅读新闻

Jetty Hello World

[日期:2014-11-19] 来源:Linux社区  作者:fan_hai_ping [字体:大 中 小]

本文网址:http://wiki.eclipse.org/Jetty/Tutorial/Jetty_HelloWorld

本章节教你如何使用CLASSPATH下Jetty类提供的Jetty API来开发代码。如果你希望使用Maven或者标准Web应用,参考Jetty和MavenHelloWorld教程。

下载Jar包

      Jetty分解成许多Jar和依赖,通过选择最小的Jar集合得到最小的内存占用。通常最好使用Maven来管理Jar包。(参考Jetty和Maven Helloworld教程)。但是对于本教程,我们使用集中的Jar包,在一个Jar包中包括所有Jetty类。使用Wget获取,如下:

mkdir Demo

cd Demo

JETTY_VERSION= 9.2.3.v20140905
wget -U none http://repo1.maven.org/maven2/org/eclipse/jetty/aggregate/jetty-all/$JETTY_VERSION/jetty-all-$JETTY_VERSION.jar

wget -U nonehttp://repo1.maven.org/maven2/javax/servlet/servlet-api/2.5/servlet-api-2.5.jar

编写HelloWorld示例

      嵌入式Jetty教程包含许多使用JettyAPI编写的示例。对于本教程,我们将在main方法中使用简单的HelloWorld处理器来运行服务。在编译器或IDE中,编辑HelloWorld.java文件,并且添加下面的内容:

package cn.uway.jetty; 

import java.io.IOException;

 

import javax.servlet.ServletException;

import javax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

 

import org.eclipse.jetty.server.Request;

import org.eclipse.jetty.server.Server;

importorg.eclipse.jetty.server.handler.AbstractHandler;

 

public class HelloWorld extendsAbstractHandler {

 

      @Override

      publicvoid handle(String target, Request baseRequest, HttpServletRequest request,

                    HttpServletResponseresponse) throws IOException, ServletException {

              response.setContentType("text/html;charset=utf-8");

              response.setStatus(HttpServletResponse.SC_OK);

              baseRequest.setHandled(true);

              response.getWriter().println("<h1>HelloWorld</h1>");

      }

     

      publicstatic void main(String[] args) throws Exception {

              Serverserver = new Server(8080);

              server.setHandler(newHelloWorld());

              server.start();

              server.join();

      }

}

编译HelloWorld示例

下面的命令编译HelloWorld类:

javac -cpservlet-api-2.5.jar:jetty-all-$JETTY_VERSION.jar HelloWorld.java

运行处理器和服务

      下面的命令运行HelloWorld示例:

java -cp.:servlet-api-2.5.jar:jetty-all-$JETTY_VERSION.jar HelloWorld

你现在在浏览器中打开http://localhost:8080看到Hello world的页面。

下一步

1) 学习Jetty/Tutorial/EmbeddinJetty教程了解更多Jetty API

http://wiki.eclipse.org/Jetty/Tutorial/Embedding_Jetty

2)浏览完整的JettyJavadoc。

http://download.eclipse.org/jetty/stable-7/apidocs/

http://download.eclipse.org/jetty/stable-8/apidocs/

http://download.eclipse.org/jetty/stable-9/apidocs/

3) 考虑使用Jetty和Menven来管理你的Jar和依赖

http://wiki.eclipse.org/Jetty/Tutorial/Jetty_and_Maven_HelloWorld

4)学习其它选择“如何使用Jetty开发”。

http://wiki.eclipse.org/Jetty/Howto/Develop

http://wiki.eclipse.org/Jetty

使用Jetty作为嵌入式服务器 http://www.linuxidc.com/Linux/2013-07/86983.htm

Jetty 源码分析 http://www.linuxidc.com/Linux/2013-10/90986.htm

Jetty安装学习并展示 http://www.linuxidc.com/Linux/2014-05/101993.htm

Jetty在Eclipse中的安装 http://www.linuxidc.com/Linux/2013-10/90991.htm

Linux(RedHat 5.8)下 安装Jetty 部署 使用  http://www.linuxidc.com/Linux/2014-10/108342.htm

Jetty 的详细介绍:请点这里
Jetty 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-11/109574.htm

linux
相关资讯       Jetty 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款