使用jetty实现小型的Servlet服务器

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用
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/输出

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:Java提取汉字的拼音

下一篇:使用DOMParser 解析xml的Java代码