1. 程式人生 > >【Servlet】使用org.eclipse.jetty實現小型的Servlet服務器

【Servlet】使用org.eclipse.jetty實現小型的Servlet服務器

服務器 onu ets get span iter() servlet final status

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服務器