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

  Tips:以上仅供测试servlet使用,如果支持js css等静态资源,可按如下配置:

context.setResourceBase(webDir);

jetty 内嵌服务的更多相关文章

  1. Jetty 开发指南:Jetty 内嵌开发

    Jetty的口号是“不要在Jetty中部署你的应用程序,在你的应用程序中部署Jetty!” 这意味着,作为将应用程序捆绑为要部署在Jetty中的标准WAR的替代方案,Jetty旨在成为一个软件组件,可 ...

  2. 使用ActiveMQ 传输文件 以及使用Jetty搭建内嵌文件服务器

    使用Active发送文件 ActiveMq 本身提供对于传输文件的支持. 1. 直接传输文件: 使用connection.createOutputStream 的形式.这种方式适合小文件.不能传输大文 ...

  3. spring内嵌jetty容器,实现main方法启动web项目

    Jetty 是一个开源的servlet容器,它为基于Java的web容器,例如JSP和servlet提供运行环境.Jetty是使用Java语言编写的,它的API以一组JAR包的形式发布.开发人员可以将 ...

  4. jetty作为内嵌服务器自启动

    为了完成web工程的测试,最近内嵌jetty也要搞起来.第一次搞还是挺焦头烂额的.直接上成果: package com.test.action; import java.io.File; import ...

  5. Message高级特性 & 内嵌Jetty实现文件服务器

    1. Messaage Properties  常见属性 更多的属性以及介绍参考:http://activemq.apache.org/activemq-message-properties.html ...

  6. IDEA内嵌Jetty启动SpringMvc项目

    这段时间本意是想要研究一下Netty的多线程异步NIO通讯框架,看完原理想要做下源码分析.查找资料发现Jetty框架底层支持用Netty做web请求的多线程分发处理,于是就筹备着将Jetty框架内嵌到 ...

  7. 使用jetty作为内嵌服务器启动项目

    http://blog.csdn.net/robinpipi/article/details/7557035 需求:把jetty作为内嵌的一个服务器,直接启动,web项目不用部署在应用服务器中.在网上 ...

  8. Winform/WPF中内嵌BeetleX的HTTP服务

    在新版本的BeetleX.FastHttpApi加入了对netstandard2.0支持,如果程序基于.NetFramework4.6.1来构建WinForm或WPF桌面程序的情况下可以直接把Beet ...

  9. Spring Boot 内嵌容器 Tomcat / Undertow / Jetty 优雅停机实现

    Spring Boot 内嵌容器 Tomcat / Undertow / Jetty 优雅停机实现 Anoyi 精讲JAVA 精讲JAVA 微信号 toooooooozi 功能介绍 讲解java深层次 ...

随机推荐

  1. socket编程知识

    Socket语法及相关: Socket Familile(地址簇) socket.AF_UNIX unix本机进程间通信 socket.AF_INET     IPV4     经常用的还是这里 so ...

  2. jQuery之XML的加载和解析

    1.XML(eXtensible Markup Language)即可扩展标记语言,与HTML一样,都是SGML标准通用语言.语法如下: 任何起始标签都必须有一个结束标签. 可以采用另一种简化语法,即 ...

  3. Java部署_IntelliJ创建一个可运行的jar包(实践)

    一.本文目的:使用Intellij Idea 13生成一个简单可执行的jar,用于快速在linux验证某个功能 二.项目源码 1.结构图  2.StaticC1.java 1 2 3 4 5 6 7 ...

  4. 使用VS2010创建WebService 发布、测试

    http://blog.sina.com.cn/s/blog_45eaa01a0102vopl.html 1 打开VS2010,菜单    文件->新建->项目 2 选择[ASP.net ...

  5. [Unity3D]引擎学习之注意事项(持续更新中)

    调试相关 如果是想在触发粒子系统效果的时候播放声音(比如爆炸的特殊发生时也播放声音),则需要将爆炸效果的粒子系统保持为Prefab后,添加Audio Source组件,在组件中添加声音文件并且确保pl ...

  6. linux下apache各种跳转(包括伪静态)的配置

      1.404跳转: vi /etc/httpd/conf/httpd.conf 在虚拟主机配置里添加一行:ErrorDocument 404 /404.html 2.301跳转: 1)将不带www的 ...

  7. safari浏览器在window下 打开控制台

    有时候需要在window下测试safari浏览器的兼容性 然后需要打开错误控制台 以下是完整打开的图文教程 1.显示菜单栏 2.打开偏好设置 3.然后切换到高级标签 勾选 在菜单栏显示开发菜单 4.打 ...

  8. VTK初学一,动画加AVI录制终于做出来了

      #ifndef INITIAL_OPENGL #define INITIAL_OPENGL #include <vtkAutoInit.h> VTK_MODULE_INIT(vtkRe ...

  9. tyvj1203 机器分配

    描述 总公司拥有高效生产设备M台,准备分给下属的N个公司.各分公司若获得这些设备,可以为国家提供一定的盈利.问:如何分配这M台设备才能使国家得到的盈利最大?求出最大盈利值.其中M<=100,N& ...

  10. LYDSY模拟赛day2 Divisors

    /* 注意分解质因数,如果i是,那么n/i也是,这样就可以解决分解质因数的时间问题了 当 k ≥ 1 时,只有这些数的约数才会对答案产生贡献. 求出 m 个数的所有不超过 n 的约数,去重后统计即可. ...