Build a simple Servlet runtime environment using Jetty

Source: Internet
Author: User

Recently, when I was doing some simple Servlet development, I felt it was very troublesome to publish the program to tomcat every time I debug it. It was also very troublesome to share the program with my colleagues, you need to help him set up the local tomcat environment. I found other Servlet runtime environments on the Internet and found that using Jetty can easily implement Embedded Web container. here I will record the process of building a simple Servlet runtime environment through Jetty, and hope to help anyone who needs it. the code for the entire environment can be found at https://github.com/mcai4gl2/jettysetup. the Code includes IntelliJ project files. If you need an eclipse project file, run mvn eclipse: eclipse after downloading the code to generate an eclipse project file. (Of course, install Maven locally ). set Maven Dependency: [plain] <dependencies> <! -- Jetty --> <dependency> <groupId> org. eclipse. jetty </groupId> <artifactId> jetty-server </artifactId> <version >$ {jetty. version }</version> </dependency> <groupId> org. eclipse. jetty </groupId> <artifactId> jetty-servlet </artifactId> <version >$ {jetty. version }</version> </dependency> <! -- Spring --> <dependency> <groupId> org. springframework </groupId> <artifactId> spring-webmvc </artifactId> <version >$ {spring. version }</version> </dependency> <! -- Log4j --> <dependency> <groupId> log4j </groupId> <artifactId> log4j </artifactId> <version> 1.2.17 </version> </dependency> <! -- Utils --> <dependency> <groupId> org. apache. commons </groupId> <artifactId> commons-io </artifactId> <version> 1.3.2 </version> </dependency> </dependencies> set servlet-context.xml: [html] <? Xml version = "1.0" encoding = "UTF-8"?> <Beans: beans xmlns = "http://www.springframework.org/schema/mvc" xmlns: beans = "http://www.springframework.org/schema/beans" xmlns: context = "http://www.springframework.org/schema/context" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation = "http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <interceptors> <interceptor> <mapping path = "/*"/> <beans: bean class = "weblog. examples. jettysetup. loggingInterceptor "/> </interceptor> </interceptors> <context: annotation-config/> <context: component-scan base-package =" weblog. examples. jettysetup. serlvet "/> </beans: beans> A simple Main Class: [java] public static void main (String [] args) throws Exception {try {DOMConfigurator. configure (Thread. currentThread (). getContextClassLoader (). getResource ("log4j. xml "); Server server = new Server (); SelectChannelConnector conne= new selectchannelconne(); connector. setPort (7411); server. setConnectors (new Connector [] {connector}); DispatcherServlet servlet = new DispatcherServlet (); servlet. setContextConfigLocation ("classpath: servlet-context.xml"); ServletContextHandler context = new ServletContextHandler (); context. setContextPath ("/"); context. addServlet (new ServletHolder ("baseServlet", servlet), "/"); HandlerCollection handlers = new HandlerCollection (); handlers. setHandlers (new Handler [] {context, new DefaultHandler ()}); server. setHandler (handlers); XmlWebApplicationContext wctx = new XmlWebApplicationContext (); wctx. setConfigLocation (""); wctx. setServletContext (servlet. getServletContext (); wctx. refresh (); context. setAttribute (XmlWebApplicationContext. ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wctx); server. start (); log.info ("Jetty embedded server started"); log.info ("Press any key to stop"); System. in. read (); log.info ("Stopping Server"); server. stop (); log.info ("Server stopped");} catch (Exception ex) {log. error ("Failed to run Jetty Server", ex); throw ex ;}} after running JettyLauncher, we can access http: // localhost: 7411/ping to check whether Jetty runs successfully. http: // localhost: 7411/ping will run the following Spring MVC Servlet: [java] @ Controller public class TestServlet {private static Logger log = Logger. getLogger (TestServlet. class); @ RequestMapping (value = "/ping", method = RequestMethod. GET) public void ping (HttpServletResponse response) throws IOException {log.info ("ping page is called"); IOUtils. write ("Embedded Jetty Server is Up and Running", response. getOutputStream () ;}} through Jetty, we can easily run and debug the Servlet locally, and the generated Servlet can be directly published to Tomcat. in this way, we can simplify Servlet development and debugging.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.