2 u011732005 u011732005 于 2016.05.03 09:14 提问

OSGI嵌入Http服务,报错Problem accessing/ProxyServlet

启动之后,在浏览器输入http://localhost:8080/demo/page/dictquery.html
结果如下所示


HTTP ERROR: 404

Problem accessing /demo/page/dictquery.html. Reason:

ProxyServlet: /demo/page/dictquery.html

Powered by Jetty://


初学者,不知道是哪个地方出问题了,具体的Activator的代码如下:

 public class Activator implements BundleActivator, ServiceListener {

    private static BundleContext context;

    private ServiceReference ref;

    private Servlet servlet;

    static BundleContext getContext() {
        return context;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.
     * BundleContext)
     */
    public void start(BundleContext bundleContext) throws Exception {

        context = bundleContext;

        // 创建Servlet对象
        servlet = new IndexServlet(context);

        // 向HttpService注册Servlet
        registerServlet();

        // 加入对HttpService的监听
        context.addServiceListener(this, "(objectClass=" + HttpService.class.getName() + ")");
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
     */
    public void stop(BundleContext bundleContext) throws Exception {
        try {
            // 注销Servlet
            unregisterServlet();
        } catch (Throwable t) {
            t.printStackTrace();
        }

        servlet = null;
        context = null;
        ref = null;
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.osgi.framework.ServiceListener#serviceChanged(org.osgi.framework.
     * ServiceEvent)
     */
    public void serviceChanged(ServiceEvent event) {
        switch (event.getType()) {
        case ServiceEvent.REGISTERED:
            // HttpService注册到OSGi容器的时候,进行Servlet的注册
            registerServlet();
            break;

        case ServiceEvent.UNREGISTERING:
            // HttpService从OSGi容器注销的时候,注销Servlet
            unregisterServlet();
            break;
        }
    }

    /*
     * 注册Web应用
     */
    private void registerServlet() {
        if (ref == null) {
            ref = context.getServiceReference(HttpService.class.getName());
        }

        if (ref != null) {
            try {
                HttpService http = (HttpService) context.getService(ref);
                if (null != http) {
                    // 注册Servlet
                    http.registerServlet("/demo/servlet", servlet, null, null);

                    // 注册资源
                    http.registerResources("/demo/page", "/page", null);
                    System.out.println("注册成功");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /*
     * 卸载Web应用
     */
    private void unregisterServlet() {
        if (ref != null) {
            try {
                HttpService http = (HttpService) context.getService(ref);
                if (null != http) {
                    http.unregister("/demo/servlet");
                    http.unregister("/demo/page");
                    System.out.println("已卸载字典查询web模块!");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}


在线狂等,哪位大侠看见,求给小弟指点迷津,谢谢!

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!