使用jetty实现小型的Servlet服务器详解编程语言

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/输出

原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/11049.html

(0)
上一篇 2021年7月19日 10:39
下一篇 2021年7月19日 10:39

相关推荐

发表回复

登录后才能评论