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/*。

  1. package com.google.code.garbagecan.jettystudy.sample5;
  2. import org.eclipse.jetty.server.Server;
  3. import org.eclipse.jetty.servlet.ServletContextHandler;
  4. import org.eclipse.jetty.servlet.ServletHolder;
  5. public class ServletContextServer {
  6. public static void main(String[] args) throws Exception {
  7. Server server = new Server(8080);
  8. ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
  9. context.setContextPath("/");
  10. server.setHandler(context);
  11. // http://localhost:8080/hello
  12. context.addServlet(new ServletHolder(new HelloServlet()), "/hello");
  13. // http://localhost:8080/hello/kongxx
  14. context.addServlet(new ServletHolder(new HelloServlet("Hello Kongxx!")), "/hello/kongxx");
  15. // http://localhost:8080/goodbye
  16. context.addServlet(new ServletHolder(new GoodbyeServlet()), "/goodbye");
  17. // http://localhost:8080/goodbye/kongxx
  18. context.addServlet(new ServletHolder(new GoodbyeServlet("Goodbye kongxx!")), "/goodbye/kongxx");
  19. server.start();
  20. server.join();
  21. }
  22. }

2. 两个简单的Servlet:HelloServlet和GoodbyeServlet:

  1. package com.google.code.garbagecan.jettystudy.sample5;
  2. import java.io.IOException;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. public class HelloServlet extends HttpServlet {
  8. private static final long serialVersionUID = 1L;
  9. private String msg = "Hello World!";
  10. public HelloServlet() {
  11. }
  12. public HelloServlet(String msg) {
  13. this.msg = msg;
  14. }
  15. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  16. response.setContentType("text/html");
  17. response.setStatus(HttpServletResponse.SC_OK);
  18. response.getWriter().println("<h1>" + msg + "</h1>");
  19. response.getWriter().println("session=" + request.getSession(true).getId());
  20. }
  21. }
  22. package com.google.code.garbagecan.jettystudy.sample5;
  23. import java.io.IOException;
  24. import javax.servlet.ServletException;
  25. import javax.servlet.http.HttpServlet;
  26. import javax.servlet.http.HttpServletRequest;
  27. import javax.servlet.http.HttpServletResponse;
  28. public class GoodbyeServlet extends HttpServlet {
  29. private static final long serialVersionUID = 1L;
  30. private String msg = "Goodbye!";
  31. public GoodbyeServlet() {
  32. }
  33. public GoodbyeServlet(String msg) {
  34. this.msg = msg;
  35. }
  36. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  37. response.setContentType("text/html");
  38. response.setStatus(HttpServletResponse.SC_OK);
  39. response.getWriter().println("<h1>" + msg + "</h1>");
  40. response.getWriter().println("session=" + request.getSession(true).getId());
  41. }
  42. }

3. 运行ServletContextServer类,然后分别访问以下四个url

http://localhost:8080/hello
  http://localhost:8080/hello/kongxx
  http://localhost:8080/goodbye
  http://localhost:8080/goodbye/kongxx

4. 除了上面的方式外,也可以创建两个个Context,分别绑定到"/hello"和"/goodbye",如下:

  1. package com.google.code.garbagecan.jettystudy.sample5;
  2. import org.eclipse.jetty.server.Handler;
  3. import org.eclipse.jetty.server.Server;
  4. import org.eclipse.jetty.server.handler.ContextHandlerCollection;
  5. import org.eclipse.jetty.servlet.ServletContextHandler;
  6. import org.eclipse.jetty.servlet.ServletHolder;
  7. public class MultiContextServer {
  8. public static void main(String[] args) throws Exception {
  9. Server server = new Server(8080);
  10. // http://localhost:8080/hello/kongxx
  11. ServletContextHandler context1 = new ServletContextHandler(ServletContextHandler.SESSIONS);
  12. context1.setContextPath("/hello");
  13. context1.setResourceBase(".");
  14. context1.setClassLoader(Thread.currentThread().getContextClassLoader());
  15. context1.addServlet(new ServletHolder(new HelloServlet("Hello Kongxx!")), "/kongxx");
  16. // http://localhost:8080/goodbye/kongxx
  17. ServletContextHandler context2 = new ServletContextHandler(ServletContextHandler.SESSIONS);
  18. context2.setContextPath("/goodbye");
  19. context2.setResourceBase(".");
  20. context2.setClassLoader(Thread.currentThread().getContextClassLoader());
  21. context2.addServlet(new ServletHolder(new GoodbyeServlet("Goodbye kongxx!")), "/kongxx");
  22. ContextHandlerCollection contexts = new ContextHandlerCollection();
  23. contexts.setHandlers(new Handler[] { context1, context2 });
  24. server.setHandler(contexts);
  25. server.start();
  26. server.join();
  27. }
  28. }
 
 

版权声明:本文为博主原创文章,未经博主允许不得转载。

Jetty实战之 嵌入式Jetty运行Servlet的更多相关文章

  1. Jetty实战之 嵌入式Jetty运行web app

    Jetty实战之 嵌入式Jetty运行web app 博客分类: 应用服务器 jettywar  转载地址:http://blog.csdn.net/kongxx/article/details/72 ...

  2. Jetty实战之 安装 运行 部署

    本文地址:http://blog.csdn.net/kongxx/article/details/7218767 1. 首先从Jetty的官方网站http://wiki.eclipse.org/Jet ...

  3. (转)Jetty实战之 安装 运行 部署

    http://blog.csdn.net/kongxx/article/details/7218767 本文地址:http://blog.csdn.NET/kongxx/article/details ...

  4. 嵌入式jetty的HTTP实现

    2    嵌入式jetty的HTTP实现 布拉君君 2.1 简单HTTP实现 2.1.1 HTTP SERVER端实现 2.1.1.1 HTTP SERVER端实现概述 在代码中嵌入一个Jetty s ...

  5. web项目嵌入Jetty运行的两种方式(Jetty插件和自制Jetty服务器)

    在开发Java web项目时候,可以在项目中嵌入Jetty服务的方式来运行web程序. 由于最近开发web项目,自己使用的是比较旧的eclipse不支持导入tomcat来运行项目,于是就学习了下使用项 ...

  6. 嵌入式jetty

    一.maven依赖 pom配置 <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId&g ...

  7. Jetty实战(杂七杂八)

    最近开始选择JETTY作为服务器了,乘这现在空闲点学习了些JETTY的部署知识,原来她真的跟TOMCAT很类似,先总结如下: 部署应用方法(下载好jetty); 方法一: 直接将应用的 war包放在j ...

  8. jetty 通过配置文件嵌入式启动web服务

    定义 jetty.xml 启动文件 <?xml version="1.0"?><!DOCTYPE Configure PUBLIC "-//Jetty/ ...

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

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

随机推荐

  1. drupal7为admin/config页面添加自己开发的模块

    1.实现显示模块 //admin/config配置页面添加journal块 $items['admin/config/journal'] = array(//注意格式为'admin/config/模块 ...

  2. JdbcTemplate学习笔记

    JdbcTemplate学习笔记 1.使用JdbcTemplate的execute()方法执行SQL语句 Java 代码 jdbcTemplate.execute("CREATE TABLE ...

  3. Segments POJ 3304 直线与线段是否相交

    题目大意:给出n条线段,问是否存在一条直线,使得n条线段在直线上的投影有至少一个公共点. 题目思路:如果假设成立,那么作该直线的垂线l,该垂线l与所有线段相交,且交点可为线段中的某两个交点 证明:若有 ...

  4. aX+bY+cZ=n(非负整数解存在性)

    题意: a*1234567+b*123456+c*1234=n 非负整数解得存在性. 题解: 看代码. #include<iostream> #include<cstdio> ...

  5. php 版本比较

    判断当前运行的 PHP 版本是否高于或等于你提供的版本号. function is_php($version) { static $_is_php; $version = (string) $vers ...

  6. 简易控制中心,angular的简单使用

    <html> <head> <meta charset='utf-8'> <script src="js/angular.js">& ...

  7. POJ 2115 C Looooops (扩展欧几里德 + 线性同余方程)

    分析:这个题主要考察的是对线性同余方程的理解,根据题目中给出的a,b,c,d,不难的出这样的式子,(a+k*c) % (1<<d) = b; 题目要求我们在有解的情况下求出最小的解,我们转 ...

  8. magento head.phtml 加载<a target=_parent

    <script language="javascript">function alinks(){  var links = document.getElementsBy ...

  9. C++零食:使用Unicode版的预定义宏__FUNCTION__

    在C++中支持一种预定义宏.比如: __FILE__: 就是当前源代码文件名 __LINE__: 就是当前源代码的行号 这些宏可以在printf等语句中直接作为字符串使用,调试的时候很方便. 如下代码 ...

  10. CentOS/RHEL 7中的firewall控制

    从CentOS/RHEL 7开始firewall的使用.很多人卸载了firewall重装iptables.但是有时候只是为了开放端口什么的,没有那个闲工夫卸载重装: 永久打开一个新端口(如TCP/80 ...