通过Jetty搭建一个简单的Servlet运行环境
最近在做一些简单的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运行环境的更多相关文章
- 尝试自己搭一个简单的typescript运行环境
开发typescript项目有一些现成的脚手架,比如:typescript-library-starter,它的配置齐全,更适合用在实际项目开发上.其实在学习阶段可以自己搭建一个简单的typescri ...
- iBatis第二章:搭建一个简单的iBatis开发环境
使用 iBatis 框架开发的基本步骤如下:1.新建项目(iBatis是持久层框架,可以运用到java工程或者web工程都可以) 这里我们建立一个 web 工程测试. 2.导入相应的框架 jar 包 ...
- Tomcat剖析(二):一个简单的Servlet服务器
Tomcat剖析(二):一个简单的Servlet服务器 1. Tomcat剖析(一):一个简单的Web服务器 2. Tomcat剖析(二):一个简单的Servlet服务器 3. Tomcat剖析(三) ...
- 模拟搭建Web项目的真实运行环境(一)
序言 最近尝试完整搭建一个Web项目的运行环境,总结一下这几个月学到的知识点. 后面的文章主要包括一下几个内容: A. 搭建一个Linux服务器,用来部署Redis.Mongo等数据存储环境: B. ...
- 【Head First Servlets and JSP】笔记6:什么是响应首部 & 快速搭建一个简单的测试环境
搭建简单的测试环境 什么是响应首部 最简单的响应首部——Content-Type 设置响应首部 请求重定向与响应首部 在浏览器中查看Response Headers 1.先快速搭建一个简单的测试环境, ...
- Servlet复习1: 一个简单的Servlet的使用
Servlet学习 1. Servlet与JSP的关系 2. Servlet的声明周期 3. 一个简单的Servlet的使用方法 什么是Servlet? 什么又是JSP? 继承了javax.servl ...
- 开发部署一个简单的Servlet
Servlet是一个执行在服务器端的Java Class文件,载入前必须先将Servlet程序代码编译成.class文件,然后将此class文件放在servlet Engline路径下.Servlet ...
- 使用gitblit搭建一个简单的局域网服务器
使用gitblit搭建一个简单的局域网服务器 1.使用背景 现在很多使用github管理代码,但是github需要互联网的支持,而且私有的git库需要收费.有一些项目的代码不能外泄,所以,搭建一个局域 ...
- Golang学习-第二篇 搭建一个简单的Go Web服务器
序言 由于本人一直从事Web服务器端的程序开发,所以在学习Golang也想从Web这里开始学起,如果对Golang还不太清楚怎么搭建环境的朋友们可以参考我的上一篇文章 Golang的简单介绍及Wind ...
随机推荐
- Java如何访问Axis2服务端
import javax.xml.namespace.QName; import org.apache.axis2.AxisFault; import org.apache.axis2.address ...
- REDHAT、CenterOS使用安装Linux系统时的光盘镜像来安装软件
使用安装Linux系统时的光盘镜像来安装软件 (1)以虚拟机上,安装mysql为例: 查看mysql是否安装 rpm -qa|grep -i mysql 显示下面,证明mysql已安装客户端,下 ...
- hdu 5610 Baby Ming and Weight lifting
Problem Description Baby Ming is fond of weight lifting. He has a barbell pole(the weight of which c ...
- Android 四大组件之 BroadcastReceiver
0 简介 BroadcastReceiver也就是“广播接收者”的意思,顾名思义,它就是用来接收来自系统和应用中的 广播. 在Android系统中,广播体现在方方面面,例 ...
- 如何统一删除word中的超链接
[摘要] 我们从别处拷贝文字,或从网上复制的文字,里面有很多超级链接,如何可以批量删除这些链接呢?这里介绍两种批量删除链接的方法. [正文] 方法一:使用快捷键删除超链接 有个神奇的快捷键,可以帮我们 ...
- BeeswaxException 以及其他问题
Could not read table BeeswaxException(handle=QueryHandle(log_context='ae18ae74-518f-400b-b4b0-d399ed ...
- Median of Two Sorted Arrays(Java)
求2个数组的中位数 方法很多 但是时间复杂度各异 1利用数组copy方法先融合两个数组,然后排序,找出中位数 import java.lang.reflect.Array; import java.u ...
- 认证和注册,提交到App Store:
账号分为个人和公司个人付费后2天左右,收到激活邮件,可以使用 member center certificates,identifiers&profiles:certificates 认证:d ...
- IE6、7 a链接内图片加滤镜后导致a标签链接失效问题解决
今天在项目中遇到一个ie6.7浏览器下a链接失效的问题,查询大量资料,最终找到完美的解决方案,如下: 解决方法: 为a标签加样式{*background:url(#);*zoom:1;} 为img外部 ...
- Android studio checkstyle 使用
首先要安装CheckStyle-IDEA插件,官网:https://plugins.jetbrains.com/plugin/1065 打开Android studio进入设置->Plugins ...