zoukankan      html  css  js  c++  java
  • Jetty 开发指南: 嵌入式开发之HelloWorld

    Jetty 嵌入式之 HelloWorld

    本节提供一个教程,演示如何快速开发针对Jetty API的嵌入式代码。

    1. 下载 Jar 包

    Jetty被分解为许多jar和依赖项,通过选择最小的jar集来实现最小的占用空间。 通常最好使用像Maven这样的东西来管理jar,但本教程使用一个聚合Jar,它包含一个Jar中所有必需的Jetty类。 您可以使用curl或浏览器手动下载聚合jetty-all.jar。

    注意:中央Maven仓库已经开始积极拒绝/拒绝从wget命令行工具访问存储库(由于某些组滥用该工具)。 中央maven存储库的管理员已经声明推荐的命令行下载工具现在是 curl 。

    重要:本节中引用的jetty-all jar仅用于示例目的,不应在此之外使用。 请考虑使用Maven来管理项目依赖项。

    使用 curl 如下所示:

    > mkdir Demo
    > cd Demo
    > curl -o jetty-all-uber.jar https://repo1.maven.org/maven2/org/eclipse/jetty/aggregate/jetty-all/9.4.12-SNAPSHOT/jetty-all-9.4.12-SNAPSHOT-uber.jar

    2. 写一个 HelloWorld 示例

    Embedding Jetty部分包含许多针对Jetty API编写的示例。 本教程使用一个简单的HelloWorld处理程序和一个main方法来运行服务器。 您可以在编辑器中下载或创建具有以下内容的文件HelloWorld.java:

    //
    //  ========================================================================
    //  Copyright (c) 1995-2018 Mort Bay Consulting Pty. Ltd.
    //  ------------------------------------------------------------------------
    //  All rights reserved. This program and the accompanying materials
    //  are made available under the terms of the Eclipse Public License v1.0
    //  and Apache License v2.0 which accompanies this distribution.
    //
    //      The Eclipse Public License is available at
    //      http://www.eclipse.org/legal/epl-v10.html
    //
    //      The Apache License v2.0 is available at
    //      http://www.opensource.org/licenses/apache2.0.php
    //
    //  You may elect to redistribute this code under either of these licenses.
    //  ========================================================================
    //
    
    package org.eclipse.jetty.embedded;
    
    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 HelloWorld extends AbstractHandler
    {
        @Override
        public void handle( String target,
                            Request baseRequest,
                            HttpServletRequest request,
                            HttpServletResponse response ) throws IOException,
                                                          ServletException
        {
            // 声明 response 编码和类型 
            response.setContentType("text/html; charset=utf-8");
    
            // 声明 response 状态码
            response.setStatus(HttpServletResponse.SC_OK);
    
            // 写入 response 内容
            response.getWriter().println("<h1>Hello World</h1>");
    
            // 通知 jetty 这个请求已经被处理过
            baseRequest.setHandled(true);
        }
    
        public static void main( String[] args ) throws Exception
        {
            Server server = new Server(8080);
            server.setHandler(new HelloWorld());
    
            server.start();
            server.join();
        }
    }

    3. 编译Hello World示例

    以下命令编译HelloWorld类:

    > mkdir classes
    > javac -d classes -cp jetty-all-uber.jar HelloWorld.java

    4. 运行 Handler 和 Server

    以下命令运行HelloWorld示例:

    > java -cp classes:jetty-all-uber.jar org.eclipse.jetty.embedded.HelloWorld

    您现在可以在浏览器地址栏输入 http:// localhost:8080 以查看您的 hello world 页面。

    下一节:Jetty 开发指南:(2)Jetty 内嵌开发

    参考资料:http://www.eclipse.org/jetty/documentation/9.4.x/advanced-embedding.html#jetty-helloworld

  • 相关阅读:
    Mybatis oracle多表联合查询分页数据重复的问题
    The Decade of Deep Learning
    D3S – A Discriminative Single Shot Segmentation Tracker
    Recent Advancements in NLP
    A list of datasets directly related to Music Information Retrieval Datasets
    Best Resources for Imbalanced Classification
    Attention-based Dropout Layer for Weakly Supervised Object Localization
    Learning a Discriminative Filter Bank within a CNN for Fine-grained Recognition
    Batch DropBlock Network for Person Re-identification and Beyond
    Cross-channel Communication Networks
  • 原文地址:https://www.cnblogs.com/mymelody/p/9667540.html
Copyright © 2011-2022 走看看