通过Jetty搭建一个简单的Servlet运行环境
来源:互联网 发布:淘宝app怎么换皮肤 编辑:程序博客网 时间:2024/06/06 06:29
最近在做一些简单的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的开发和调试.
0 0
- 通过Jetty搭建一个简单的Servlet运行环境
- 通过Jetty搭建一个简单的Servlet运行环境
- Jetty的配置、部署与API使用——(2)通过Jetty的Java API运行一个Jetty服务器
- LAMP运行环境的简单搭建
- Jetty - 嵌入式运行Servlet
- 通过nginx搭建一个简单的代理服务器
- 搭建一个基于OSGI的可以运行helloworld.html和helloworld.jsp的简单web环境
- Jetty一个开源的servlet容器
- Servlet的运行环境
- i-jetty编译和在eclipse上的运行环境搭建
- JFinal及Jetty环境的搭建
- 搭建一个简单的SDN网络环境
- 一个简单的hibernate环境搭建
- Jetty实战之 嵌入式Jetty运行Servlet
- Jetty实战之 嵌入式Jetty运行Servlet
- Jetty实战之 嵌入式Jetty运行Servlet
- Jetty实战之 嵌入式Jetty运行Servlet
- Jetty实战之 嵌入式Jetty运行Servlet
- 新浪实时股票数据接口http://hq.sinajs.cn/list=code
- jquery 控制html页面表格当中奇偶行颜色不同 鼠标悬停变色
- vim乱码的解决
- PB数据窗口其他知识
- MacOS 下安装Gerrit
- 通过Jetty搭建一个简单的Servlet运行环境
- Oralce 日期函数
- java 对象 与json字符串的相互转化
- VS2010 C# 你得学会并且学得会的Socket编程基础知识(Silverlight Socket编程)
- 解决alaert.builder二次调用报错的bug
- emacs在win8乱码
- 16.0~16.8 多任务 后台运行 Multitasking
- android 判断点击的位置是不是在指定的view上
- 集团信息化之路——关于网络电子采购系统的需求报告