码迷,mamicode.com
首页 > 系统相关 > 详细

【Servlet】使用org.eclipse.jetty实现小型的Servlet服务器

时间:2018-03-16 14:02:10      阅读:652      评论:0      收藏:0      [点我收藏+]

标签:prot   oge   exce   cte   地址   hello   handle   jetty   doget   

import java.io.IOException;

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

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

public class EmbeddingJettyWithServlet {

    public static void main(String[] args) throws Exception {

        Server server = new Server(8080);

        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
        context.setContextPath("/hello");
        server.setHandler(context);

        context.addServlet(new ServletHolder(new HelloServlet()), "/*");
        server.start();

    }

    public static class HelloServlet extends HttpServlet {

        private static final long serialVersionUID = -6154475799000019575L;

        private static final String greeting = "Hello World";

        protected void doGet(HttpServletRequest request,
                HttpServletResponse response) throws ServletException,
                IOException {

            response.setContentType("text/html");
            response.setStatus(HttpServletResponse.SC_OK);
            response.getWriter().println(greeting);
        }

    }

}

访问URL:http://myhost:8080/hello/

输出:Hello World

上面的程序需要导入servlet-api.jar和jetty-all.jar,jetty的下载地址可以参照:http://www.eclipse.org/jetty/[][]

【Servlet】使用org.eclipse.jetty实现小型的Servlet服务器

标签:prot   oge   exce   cte   地址   hello   handle   jetty   doget   

原文地址:https://www.cnblogs.com/HDK2016/p/8580646.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!