Equinox Servlet

本例采用扩展点的方式,注册Servlet和web资源。

   

Step1: 建立Eclipse plugin-in工程

Step2: 新建一个Servlet类

package servlet;

   

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;

   

@Override

protected void doGet(HttpServletRequest req, HttpServletResponse resp)

throws ServletException, IOException {

resp.setContentType("text/html");

resp.getWriter().println("hello");

   

}

}

   

Step3: Eclipse提示HttpServlet找不到,在MANIFEST.MF中添加对javax.servlet的依赖

后两项是扩展点中注册servlet时需要的bundle;

   

Step4: 在项目根目录下新建WebContent目录,并新建jsp目录,index.jsp文件

   

index.jsp文件

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

pageEncoding="ISO-8859-1"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Insert title here</title>

</head>

<body>

index...........

</body>

</html>

   

Step5: 在项目根目录下新建plugin.xml(在MANIFEST.MF的overview中点击Extensions链接可自动生成)

<?xml version="1.0" encoding="UTF-8"?>

<?eclipse version="3.4"?>

<plugin>

<extension point="org.eclipse.equinox.http.registry.resources">

<resource

alias="/images"

base-name="/WebContent/img"/>

</extension>

   

<extension point="org.eclipse.equinox.http.registry.servlets">

<servlet

         alias="/hello"

         class="servlet.HelloServlet"

         load-on-startup="true">         

</servlet>

<servlet

alias="/jsp/*.jsp"

class="org.eclipse.equinox.jsp.jasper.registry.JSPFactory:/WebContent/jsp/"/>

</extension>

</plugin>

   

Step6: Run Configure

新建一个OSGi Framework

   

取消Bundles的Target Platform, 点击Add Required Bundles:只需要添加我们需要的bundle。

   

注意,添加jetty

org.eclipse.equinox.http.jetty是手动添加的,其余的是add required Bundles自动添加的

   

Step7: run

在控制台中执行ss命令

osgi> ss

   

我们的bundle的状态时ACTIVE,表示已经可用;

   

在浏览器中,访问http://localhost/hellohttp://localhost/jsp/index.jsphttp://localhost/images/1.jpg

   

   

   

Step8: 更新源码

更新源码时,Eclipse不会自动刷新bundle,可以通过命令update 5(5是bundle的bundleID)强制执行

   

   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值