zoukankan      html  css  js  c++  java
  • jetty 内嵌服务

    pom.xml
    
    
    <properties>
    		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    		<jetty.version>7.0.2.v20100331</jetty.version>
    		<slf4j.version>1.7.5</slf4j.version>
    		<log4j.version>1.2.14</log4j.version>
    		<fastjson.version>1.2.20</fastjson.version>
    		
    	</properties>
    
    	<dependencies>
    		<!-- 内置服务器配置 -->
    		<dependency>
    			<groupId>org.eclipse.jetty.aggregate</groupId>
    			<artifactId>jetty-all-server</artifactId>
    			<version>${jetty.version}</version>
    		</dependency>
    		<dependency>
    			<groupId>org.slf4j</groupId>
    			<artifactId>slf4j-log4j12</artifactId>
    			<version>${slf4j.version}</version>
    			<scope>provided</scope>
    		</dependency>
    		<dependency>
    			<groupId>log4j</groupId>
    			<artifactId>log4j</artifactId>
    			<version>${log4j.version}</version>
    			<scope>provided</scope>
    		</dependency>
    		<dependency>
    			<groupId>org.freemarker</groupId>
    			<artifactId>freemarker</artifactId>
    			<version>2.3.25-incubating </version>
    			<scope>provided</scope>
    		</dependency>
    		<dependency>
    			<groupId>com.alibaba</groupId>
    			<artifactId>fastjson</artifactId>
    			<version>${fastjson.version}</version>
    			<scope>provided</scope>
    		</dependency>
    

      

    代码:

    package com.google.code.garbagecan.jettystudy.sample5;  
      
    import org.eclipse.jetty.server.Server;  
    import org.eclipse.jetty.servlet.ServletContextHandler;  
    import org.eclipse.jetty.servlet.ServletHolder;  
      
    public class ServletContextServer {  
        public static void main(String[] args) throws Exception {  
            Server server = new Server(8080);  
      
            ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);  
            context.setContextPath("/");  
            server.setHandler(context);  
      
            // http://localhost:8080/hello  
            context.addServlet(new ServletHolder(new HelloServlet()), "/hello");  
            // http://localhost:8080/hello/kongxx  
            context.addServlet(new ServletHolder(new HelloServlet("Hello Kongxx!")), "/hello/kongxx");  
      
            // http://localhost:8080/goodbye  
            context.addServlet(new ServletHolder(new GoodbyeServlet()), "/goodbye");  
            // http://localhost:8080/goodbye/kongxx  
            context.addServlet(new ServletHolder(new GoodbyeServlet("Goodbye kongxx!")), "/goodbye/kongxx");  
              
            server.start();  
            server.join();  
        }  
    }  
    
    
    
    
    package com.google.code.garbagecan.jettystudy.sample5;  
      
    import java.io.IOException;  
    import javax.servlet.ServletException;  
    import javax.servlet.http.HttpServlet;  
    import javax.servlet.http.HttpServletRequest;  
    import javax.servlet.http.HttpServletResponse;  
      
    public class HelloServlet extends HttpServlet {  
        private static final long serialVersionUID = 1L;  
        private String msg = "Hello World!";  
      
        public HelloServlet() {  
        }  
      
        public HelloServlet(String msg) {  
            this.msg = msg;  
        }  
      
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
            response.setContentType("text/html");  
            response.setStatus(HttpServletResponse.SC_OK);  
            response.getWriter().println("<h1>" + msg + "</h1>");  
            response.getWriter().println("session=" + request.getSession(true).getId());  
        }  
    }  
      
    package com.google.code.garbagecan.jettystudy.sample5;  
      
    import java.io.IOException;  
    import javax.servlet.ServletException;  
    import javax.servlet.http.HttpServlet;  
    import javax.servlet.http.HttpServletRequest;  
    import javax.servlet.http.HttpServletResponse;  
      
    public class GoodbyeServlet extends HttpServlet {  
        private static final long serialVersionUID = 1L;  
        private String msg = "Goodbye!";  
      
        public GoodbyeServlet() {  
        }  
      
        public GoodbyeServlet(String msg) {  
            this.msg = msg;  
        }  
      
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
            response.setContentType("text/html");  
            response.setStatus(HttpServletResponse.SC_OK);  
            response.getWriter().println("<h1>" + msg + "</h1>");  
            response.getWriter().println("session=" + request.getSession(true).getId());  
        }  
    }  
    

      Tips:以上仅供测试servlet使用,如果支持js css等静态资源,可按如下配置:

    context.setResourceBase(webDir);
  • 相关阅读:
    命令创建.net core3.0 web应用详解(超详细教程)
    安装Visual Studio Code并汉化
    Visual Studio2019及.NET CORE3.0的安装教程
    序列化对象设置字段首字母小写(驼峰命名法)
    解决锁定图层后不能淡显的问题
    C#中的委托和事件
    C#常用方法——委托和事件详解
    C#常用方法——List<T>泛型列表解析
    C#常用方法——strng.Format()和$用法详解
    C#常用方法——通过WebServices接口读取json字符并解析示例
  • 原文地址:https://www.cnblogs.com/sprinng/p/6048976.html
Copyright © 2011-2022 走看看