本例采用扩展点的方式,注册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/hello, http://localhost/jsp/index.jsp,http://localhost/images/1.jpg
Step8: 更新源码
更新源码时,Eclipse不会自动刷新bundle,可以通过命令update 5(5是bundle的bundleID)强制执行