Jetty的入门

一、开发环境

Eclipse  4.3.1

Maven  3.1

Jetty  9.0.6.v20130930

Jetty的下载地址:

http://download.eclipse.org/jetty/

二、新建Maven项目

1、新建webapp项目jettytest.

2、在Pom.xml中配置Jetty dependency和plugin

<!-- Jetty begin -->

       <dependency>

           <groupId>org.eclipse.jetty</groupId>

           <artifactId>jetty-server</artifactId>

           <version>9.0.6.v20130930</version>

       </dependency>

       <dependency>

           <groupId>org.eclipse.jetty</groupId>

           <artifactId>jetty-servlet</artifactId>

           <version>9.0.6.v20130930</version>

       </dependency>

       <!-- Jetty end -->

  <plugins>

           <!-- compiler插件, 设定JDK版本 -->

           <plugin>

              <groupId>org.apache.maven.plugins</groupId>

              <artifactId>maven-compiler-plugin</artifactId>

              <version>3.1</version>

              <configuration>

                  <source>1.7</source>

                  <target>1.7</target>

                  <showWarnings>true</showWarnings>

              </configuration>

           </plugin>

           <plugin>

              <groupId>org.mortbay.jetty</groupId>

              <artifactId>maven-jetty-plugin</artifactId>

           </plugin>

       </plugins>

三、新建SimpleServlet和JettyServer

1、新建一个简单的Servlet:SimlpeServlet

public class ServletServer {

    public static void main(String[] args) throws Exception {
Server server = new Server(8080); ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
server.setHandler(context); context.addServlet(new ServletHolder(new SimpleServlet()), "/firstservlet");
context.addServlet(new ServletHolder(new SimpleServlet("Hello, this is my first Jetty servlet!")), "/secondservlet"); server.start();
server.join();
} }

2、新建JettyServer

public class SimpleServlet extends HttpServlet {

    private static final long serialVersionUID = -8470943914753284049L;

    private String message = "Hello World!";

    public SimpleServlet() {
} public SimpleServlet(String message) {
this.message = message;
} @Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name = request.getParameter("name");
String password = request.getParameter("password"); response.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().println("<h1>" + message + "</h1>");
if (name != null) {
response.getWriter().println("名字:" + name + "</br>");
}
if (password != null) {
response.getWriter().println("密码:" + password + "</br>");
} }
}

四、运行结果

1、运行JettyServer,启动Jetty服务器。控制台出现下列信息表示服务器启动成功:

2013-10-12 22:20:36.281:INFO:oejs.Server:main: jetty-9.0.6.v20130930

2013-10-12 22:20:36.312:INFO:oejsh.ContextHandler:main: Started o.e.j.s.ServletContextHandler@18c2661{/,null,AVAILABLE}

2013-10-12 22:20:36.406:INFO:oejs.ServerConnector:main: Started ServerConnector@838143{HTTP/1.1}{0.0.0.0:8080}

2、打开浏览器输入地址:http://127.0.0.1:8080/firstservlet。出现下列运行结果:

3、还可以输入地址:http://127.0.0.1:8080/secondservlet。出现下列运行结果:

五、大功告成!

Jetty入门的更多相关文章

  1. Jetty入门(1-3)Eclipse集成gradle-Gretty插件或maven-jetty插件运行应用

    英文来源:  http://akhikhl.github.io/gretty-doc/Getting-started.html 一.gradle插件 1.使用gretty来运行jetty: gradl ...

  2. Jetty入门(1-2)eclipse集成jetty插件并发布运行应用

    一.eclipse集成jetty插件 1.从市场安装jetty插件 2.使用jetty插件发布应用和配置运行环境 debug配置默认共用上述run配置 3.使用jetty插件启动运行和停止运行选中的应 ...

  3. Jetty入门(1-1)Jetty入门教程

    一.Jetty是什么? 1.Jetty 是一个Java语言编写的,开源的Servlet容器和应用服务器. Jetty 极度轻量级.高便携性.功能强大.灵活和扩展性好,而且支持各种技术如SPDY.Web ...

  4. jetty 入门

    jetty因其能作为内嵌的应用服务器,随应用一起存在,在小批量应用中很受欢迎. jetty作为应用服务器: jetty下载: 在官网下载jetty:http://www.eclipse.org/jet ...

  5. Jetty使用教程(四:21-22)—Jetty开发指南

    二十一.嵌入式开发 21.1 Jetty嵌入式开发HelloWorld 本章节将提供一些教程,通过Jetty API快速开发嵌入式代码 21.1.1 下载Jetty的jar包 Jetty目前已经把所有 ...

  6. Jetty官方文档翻译

    最近在学习Jetty,没有找到合适的资料,所有只能看官方文档了,但是只有英文的,想着自己翻译着学也是学还不如把学习的过程放到网上,也可以给需要的人看,英文水平毕竟有限,也是用有道翻译着来的,不过也加了 ...

  7. Jetty 服务器的知识

    Jetty 服务器的知识 也许你还没有听说过这个Jetty服务器,不过它确实是一种比较轻量级的Java服务器,和Tomcat一样,专门解释JavaWeb程序的服务器.因为在之前学习Java Web 的 ...

  8. Jetty总览

    Jetty入门 基本功能介绍 配置概览-怎么配置Jetty 配置概览-须要配置什么 Jetty配置 部署到Jetty 配置上下文 配置连接器 配置安全 配置JSP支持 Jetty管理指导 启动Jett ...

  9. Servlet引擎Jetty之入门1

    Jetty与tomcat一样,HttpWeb容器,支持实现Servlet规范. 详细介绍参考:https://www.ibm.com/developerworks/cn/java/j-lo-jetty ...

随机推荐

  1. Runloop基础知识

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  2. R型思维模式对软件开发的影响(草稿)

    The pragmatic programmers 一直在工作之余读些书,之前主要是纯英文版的计算机相关的算法,编译器,数学等,想通过读这些书来提高自己每日工作效能,结果收效甚微.一是,因为纯英文的书 ...

  3. [Android]通过js方法回调部分native报错 Web Console: Uncaught TypeError: Object [object Object] has no method 'xxx'

    在android4.2以前,注入步骤如下: webview.getSetting().setJavaScriptEnable(true); class JsObject { public String ...

  4. pyhon之对memcached及redis操作

    Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度.Memcached ...

  5. Excel jxl导入导出

    JAVA EXCEL API简介 Java Excel是一开放源码项目,通过它Java开发人员可以读取Excel文件的内容.创建新的Excel文件.更新已经存在的Excel文件.使用该API非Wind ...

  6. Excel 日期转换

    public static void importExcel2(File file) throws Exception, IOException { Workbook book = Workbook. ...

  7. 在php中写接口时 对json格式的转换 简单的方法

    方法 一 方法二 可以通过urlencode();遍历出来

  8. LightOJ 1285 - Drawing Simple Polygon (几何,极角排序)

    1285 - Drawing Simple Polygon   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: ...

  9. html 表格的制作

    表格的制作 表格<table></table> <table  width=""  height=""  align=" ...

  10. Swift3.0基础语法学习<三>

    枚举和结构体: // // ViewController3.swift // SwiftBasicDemo // // Created by 思 彭 on 16/11/16. // Copyright ...