Jetty实战之 嵌入式Jetty运行Servlet
http://blog.csdn.net/kongxx/article/details/7230080
-
Jetty实战之 嵌入式Jetty运行Servlet
分类:JettyJava
(19530) (3)
本文链接:http://blog.csdn.net/kongxx/article/details/7230080
在嵌入式Jetty中,有时候我们想运行一些的Servlet,此时就需要创建创建Context,然后让自己的Servlet运行在这些ServletContext中。
1. 首先创建一个ServletContextServer类,用来初始化web应用程序的Context,并且指定Servlet和Servlet匹配的url。这里指定了两个Servlet,分别是HelloServlet和GoodbyeServlet,并分别对应/hello/*和/goodbye/*。
- 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();
- }
- }
2. 两个简单的Servlet:HelloServlet和GoodbyeServlet:
- 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());
- }
- }
3. 运行ServletContextServer类,然后分别访问以下四个url
http://localhost:8080/hello
http://localhost:8080/hello/kongxx
http://localhost:8080/goodbye
http://localhost:8080/goodbye/kongxx4. 除了上面的方式外,也可以创建两个个Context,分别绑定到"/hello"和"/goodbye",如下:
- package com.google.code.garbagecan.jettystudy.sample5;
- import org.eclipse.jetty.server.Handler;
- import org.eclipse.jetty.server.Server;
- import org.eclipse.jetty.server.handler.ContextHandlerCollection;
- import org.eclipse.jetty.servlet.ServletContextHandler;
- import org.eclipse.jetty.servlet.ServletHolder;
- public class MultiContextServer {
- public static void main(String[] args) throws Exception {
- Server server = new Server(8080);
- // http://localhost:8080/hello/kongxx
- ServletContextHandler context1 = new ServletContextHandler(ServletContextHandler.SESSIONS);
- context1.setContextPath("/hello");
- context1.setResourceBase(".");
- context1.setClassLoader(Thread.currentThread().getContextClassLoader());
- context1.addServlet(new ServletHolder(new HelloServlet("Hello Kongxx!")), "/kongxx");
- // http://localhost:8080/goodbye/kongxx
- ServletContextHandler context2 = new ServletContextHandler(ServletContextHandler.SESSIONS);
- context2.setContextPath("/goodbye");
- context2.setResourceBase(".");
- context2.setClassLoader(Thread.currentThread().getContextClassLoader());
- context2.addServlet(new ServletHolder(new GoodbyeServlet("Goodbye kongxx!")), "/kongxx");
- ContextHandlerCollection contexts = new ContextHandlerCollection();
- contexts.setHandlers(new Handler[] { context1, context2 });
- server.setHandler(contexts);
- server.start();
- server.join();
- }
- }
版权声明:本文为博主原创文章,未经博主允许不得转载。
Jetty实战之 嵌入式Jetty运行Servlet的更多相关文章
- Jetty实战之 嵌入式Jetty运行web app
Jetty实战之 嵌入式Jetty运行web app 博客分类: 应用服务器 jettywar 转载地址:http://blog.csdn.net/kongxx/article/details/72 ...
- Jetty实战之 安装 运行 部署
本文地址:http://blog.csdn.net/kongxx/article/details/7218767 1. 首先从Jetty的官方网站http://wiki.eclipse.org/Jet ...
- (转)Jetty实战之 安装 运行 部署
http://blog.csdn.net/kongxx/article/details/7218767 本文地址:http://blog.csdn.NET/kongxx/article/details ...
- 嵌入式jetty的HTTP实现
2 嵌入式jetty的HTTP实现 布拉君君 2.1 简单HTTP实现 2.1.1 HTTP SERVER端实现 2.1.1.1 HTTP SERVER端实现概述 在代码中嵌入一个Jetty s ...
- web项目嵌入Jetty运行的两种方式(Jetty插件和自制Jetty服务器)
在开发Java web项目时候,可以在项目中嵌入Jetty服务的方式来运行web程序. 由于最近开发web项目,自己使用的是比较旧的eclipse不支持导入tomcat来运行项目,于是就学习了下使用项 ...
- 嵌入式jetty
一.maven依赖 pom配置 <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId&g ...
- Jetty实战(杂七杂八)
最近开始选择JETTY作为服务器了,乘这现在空闲点学习了些JETTY的部署知识,原来她真的跟TOMCAT很类似,先总结如下: 部署应用方法(下载好jetty); 方法一: 直接将应用的 war包放在j ...
- jetty 通过配置文件嵌入式启动web服务
定义 jetty.xml 启动文件 <?xml version="1.0"?><!DOCTYPE Configure PUBLIC "-//Jetty/ ...
- Jetty 开发指南:Jetty 内嵌开发
Jetty的口号是“不要在Jetty中部署你的应用程序,在你的应用程序中部署Jetty!” 这意味着,作为将应用程序捆绑为要部署在Jetty中的标准WAR的替代方案,Jetty旨在成为一个软件组件,可 ...
随机推荐
- socket小解
要理解socket,首先得理解TCP/IP协议族, TCP/IP (Transmission Control Protocol/Internet Protocol)传输控制协议/网间协议 定义: TC ...
- wchar_t * 与 char * 互相转换小记
wchar_t * 与char *之间的转化只需要借助标准库里面的std::wcstombs和std::mbstowcs就能实现了
- linux的学习系列 1---简介
Linux简介 严格的来讲,Linux 不算是一个操作系统,只是一个 Linux 系统中的内核,即计算机软件与硬件通讯之间的平台:Linux的全称是GNU/Linux,这才算是一个真正意义上的Linu ...
- THOUGHTS: programming in linux... with third_party open sources... methods
Actually I do not have experiences in programming with open sources/third party libs.. in linux.. I ...
- stdafx文件介绍
MSDN介绍: These files are used to build a precompiled header file Projname.pch and a precompiled types ...
- java vector 和ArrayList的区别
相同点: 1.都是使用数组存储数据 不同点: 1.Vector是显示安全的,ArrayList是线程不安全的 Vector部分代码: public synchronized boolean add(E ...
- HDU1253--胜利大逃亡--HDU1240--Asteroids!--简单三维BFS
胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- java导出和读取excel数据
使用的是poi的jar包 下载地址http://poi.apache.org/download.html 主要是把jar包导入,直接新建一个列子测试一下就明白了.使用起来还是比较方便的,代码里面的原理 ...
- MyEclipse使用总结——修改MyEclipse默认的Servlet和jsp代码模板
http://www.cnblogs.com/xdp-gacl/p/3769058.html 孤傲苍狼 只为成功找方法,不为失败找借口! MyEclipse使用总结——修改MyEclipse默认的 ...
- java集合框架map
Map<K,V> K key V value Map集合:该集合存储键值对.一对一对往里存,而且要保证键的唯一性. 1,添加. 2,删除. 3,判断. 4,获取. Map |--Hasht ...