首页 > 代码库 > jetty 内嵌服务

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());      }  }  

  

jetty 内嵌服务