最近在做一些简单的Servlet开发的时候,感觉每次调试的时候都要发布到tomcat上很麻烦,把程序共享给同事也很麻烦,需要帮他设置本地的tomcat环境. 在网上找了找其他的Servlet运行环境,发现用Jetty可以很方便的实现嵌入式Web container.这里我记录一下通过Jetty搭建简单Servlet运行环境的过程,希望对有同样需要的朋友有所帮助.

整个环境的代码可以在https://github.com/mcai4gl2/jettysetup找到. 代码包括了IntelliJ的项目文件,如果需要eclipse项目文件,请在下载代码后运行 mvn eclipse:eclipse 来生成eclipse项目文件. (当然, 请在本地安装Maven).

  • 设置Maven Dependency:

    <dependencies>
    <!-- jetty -->
    <dependency>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-server</artifactId>
    <version>${jetty.version}</version>
    </dependency>
    <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>
  • 设置servlet-context.xml:
    <?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>
  • 一个简单的Main Class:
    public static void main(String[] args) throws Exception {
    try {
    DOMConfigurator.configure(Thread.currentThread().getContextClassLoader().getResource("log4j.xml")); Server server = new Server(); SelectChannelConnector connector = new SelectChannelConnector();
    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;
    }
    }

在JettyLauncher运行后,我们可以访问http://localhost:7411/ping来查看Jetty是否成功运行. http://localhost:7411/ping将运行以下Spring MVC Servlet:

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

通过Jetty,我们可以很容易的在本地运行和调试Servlet,而生成好的Servlet我们可以直接发布到Tomcat. 这样,我们可以简化Servlet的开发和调试.

通过Jetty搭建一个简单的Servlet运行环境的更多相关文章

  1. 尝试自己搭一个简单的typescript运行环境

    开发typescript项目有一些现成的脚手架,比如:typescript-library-starter,它的配置齐全,更适合用在实际项目开发上.其实在学习阶段可以自己搭建一个简单的typescri ...

  2. iBatis第二章:搭建一个简单的iBatis开发环境

    使用 iBatis 框架开发的基本步骤如下:1.新建项目(iBatis是持久层框架,可以运用到java工程或者web工程都可以) 这里我们建立一个 web 工程测试. 2.导入相应的框架 jar 包 ...

  3. Tomcat剖析(二):一个简单的Servlet服务器

    Tomcat剖析(二):一个简单的Servlet服务器 1. Tomcat剖析(一):一个简单的Web服务器 2. Tomcat剖析(二):一个简单的Servlet服务器 3. Tomcat剖析(三) ...

  4. 模拟搭建Web项目的真实运行环境(一)

    序言 最近尝试完整搭建一个Web项目的运行环境,总结一下这几个月学到的知识点. 后面的文章主要包括一下几个内容: A. 搭建一个Linux服务器,用来部署Redis.Mongo等数据存储环境: B. ...

  5. 【Head First Servlets and JSP】笔记6:什么是响应首部 & 快速搭建一个简单的测试环境

    搭建简单的测试环境 什么是响应首部 最简单的响应首部——Content-Type 设置响应首部 请求重定向与响应首部 在浏览器中查看Response Headers 1.先快速搭建一个简单的测试环境, ...

  6. Servlet复习1: 一个简单的Servlet的使用

    Servlet学习 1. Servlet与JSP的关系 2. Servlet的声明周期 3. 一个简单的Servlet的使用方法 什么是Servlet? 什么又是JSP? 继承了javax.servl ...

  7. 开发部署一个简单的Servlet

    Servlet是一个执行在服务器端的Java Class文件,载入前必须先将Servlet程序代码编译成.class文件,然后将此class文件放在servlet Engline路径下.Servlet ...

  8. 使用gitblit搭建一个简单的局域网服务器

    使用gitblit搭建一个简单的局域网服务器 1.使用背景 现在很多使用github管理代码,但是github需要互联网的支持,而且私有的git库需要收费.有一些项目的代码不能外泄,所以,搭建一个局域 ...

  9. Golang学习-第二篇 搭建一个简单的Go Web服务器

    序言 由于本人一直从事Web服务器端的程序开发,所以在学习Golang也想从Web这里开始学起,如果对Golang还不太清楚怎么搭建环境的朋友们可以参考我的上一篇文章 Golang的简单介绍及Wind ...

随机推荐

  1. Python的ASCII, GB2312, Unicode , UTF-8 相互转换

    ASCII 是一种字符集,包括大小写的英文字母.数字.控制字符等,它用一个字节表示,范围是 0-127 Unicode分为UTF-8和UTF-16.UTF-8变长度的,最多 6 个字节,小于 127 ...

  2. 企业OA面临的问题,以及解决问题的推荐

          现在的企业不管大小都趋于软件话,而办公用的OA软件更是成为了企业中不可获取的一环,一个好的软件能让企业发展的更加顺利,而一个不合适的软件可能让公司哀声怨道反而起了反作用!       OA ...

  3. DICOM:C-GET与C-MOVE对照剖析

    背景: 之前专栏中介绍最多的两款PACS各自是基于dcmtk的dcmqrscp以及Orthanc.和基于fo-dicom的DicomService(自己开发的).该类应用场景都是针对于局域网,因此在使 ...

  4. BCD码、十六进制与十进制互转

    在做嵌入式软件的设计中,常常会遇到十六进制.BCD码与十进制之间的转换,近期做M1卡的应用中,涉及了大量的十六进制.BCD码与十进制之间的转换.通过对BCD码.十六进制 权的理解,轻松的实现了他们之间 ...

  5. 启动tomcat后struts框架报异常严重: Exception starting filter struts2 Unable to load configuration. - Class: java.net.PlainSocketImpl

    今天刚好宿舍断网,打开电脑,打开ide工具,启动tomcat后,访问web项目工程,页面显示404,查看控制台,运行报错信息如下: 严重: Exception starting filter stru ...

  6. 安装Php时候报错信息:virtual memory exhausted: Cannot allocate memory (不能分配内存)

    原因是fileinfo这个函数在编译时非常消耗内存,而系统内存又不够了,所以才会出现此问题. 网上找了方法: 1,关闭其他占用大内存的进程. 2,在编译是添加参数 --disable-fileinfo

  7. HTML中IE条件注释判断语句(<!--[if XX IE X]><![endif]-->)

    <!--[if XX IE X]><![endif]-->是IE专门提供的一种语法,其他浏览器会将其作为注释而忽略这些语句.   作用: 根据不同的IE版本加载对应的CSS或者 ...

  8. ASP.NET基于donetCHARTING的自动报表

    1,首先需要添加引用ChartExtents.dll和donetCHARTING.dll,资源百度大把. 2,配置图片生成类. using System; using System.Data; usi ...

  9. A - 487-3279

    Description Businesses like to have memorable telephone numbers. One way to make a telephone number ...

  10. java口算器

    package dd;import javax.swing.*; import java.awt.*;import java.awt.event.*;class Main extends JFrame ...