java - osgi框架下开发的web应用,可以启动,但是无法访问

查看:246
本文介绍了java - osgi框架下开发的web应用,可以启动,但是无法访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

osgi初学者,刚刚接触,还不算入门,才跑成功hello world;最近在参照博客访问,是没有任何内容的,提示This site can’t be reached,原因实在找不到。
特别说明:
在运行时候,bundles--target platform这里,我没按照作者那样去添加

org.eclipse.equinox.http.servlet
org.eclipse.equinox.http.jetty
org.mortbay.jetty.server
org.mortbay.jetty.util

这四个我没有添加,因为添加时候,发现org.mortbay.jetty.server,org.mortbay.jetty.util是没有的,如果添加了org.eclipse.equinox.http.servlet,与org.eclipse.equinox.http.jetty,运行会报错org.osgi.framework.BundleException: Could not resolve module: org.eclipse.equinox.http.jetty [10]
Unresolved requirement: Import-Package: org.eclipse.jetty.http; version="[9.0.0,10.0.0)"。而且我在新建工程时候,选择的是standard osgi framwork。不知道是不是需要选择equinox这个。但是选择equinox,在下面的osgi_web项目中,依然同样的问题。

代码如下:
Activator.java

public class Activator implements BundleActivator, ServiceListener {
    private BundleContext context;
    private ServiceReference<HttpService> ref;

    /*
     * (non-Javadoc)
     * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
     */
    public void start(BundleContext context) throws Exception {
        System.out.println("Hello World!!");
        this.context=context;
        context.addServiceListener(this, "(objectClass="+HttpService.class.getName()+")");
    }
    
    /*
     * (non-Javadoc)
     * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
     */
    public void stop(BundleContext context) throws Exception {
        System.out.println("Goodbye World!!");
        context.removeServiceListener(this);
    }

    @Override
    public void serviceChanged(ServiceEvent event) {
        switch(event.getType()){
        case ServiceEvent.REGISTERED:registerServlet();break;
        case ServiceEvent.UNREGISTERING:unregisterServlet();break;
        }
        
    }
    
    private void registerServlet(){
        if(ref==null){
            ref=context.getServiceReference(HttpService.class);
            if(ref!=null){
                try{
                    HttpService service=context.getService(ref);
                    service.registerServlet("/demo/hello", new HelloServlet(context),null,null);
                    System.out.println("/demo/hello已经被注册");
                }catch(Exception ex){
                    ex.printStackTrace();
                }
            }
        }
    }
    
    private void unregisterServlet(){
        if(ref!=null){
            try{
                HttpService httpservice=context.getService(ref);
                httpservice.unregister("/demo/hello");
                System.out.println("/demo/hello已经被卸载");
                ref=null;
            }catch(Exception ex){
                ex.printStackTrace();
            }
        }
    }

}

helloServlet.java

public class HelloServlet extends HttpServlet{
    private BundleContext context;
    
    public HelloServlet(BundleContext context){
        this.context=context;
    }
    
    public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException{
        HttpSession session=request.getSession();
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html");
        PrintWriter out=response.getWriter();
        out.println("<html>");
        out.println("<head>");
        out.println("<title>hello world</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("hello wolrd!");
        out.println("</body>");        
    }
    

}

尝试好几遍,实在没有找到问题的原因所在。然后试着换了一种方式,参照网页https://www.ibm.com/developer...
,按照将 HTTP Server 置于 Equinox 框架中开发 Web 应用一步一步操作,但是结果一样,这几个地址无法访问

http://localhost/images/1.jpg
http://localhost/jsp/index.jsp
http://localhost/servlet/myfirstservlet?userName=Levin

工程也是成功启动了的,截图如下:
成功名是osgi_web

最终发现是Caused by: java.lang.NullPointerException: A null service reference is not allowed.

解决方案

勾选的Bundle不够导致的,已经解决了。我在这里添加了一些bundle,然后就可以了。还有,不要使用默认的80端口,否则访问不了,参数中添加 -Dorg.osgi.service.http.port=9080


备注:另外,那个Activator.java里面那样的代码,我半天都不知道原来是Caused by: java.lang.NullPointerException: A null service reference is not allowed. 造成的问题,改成这样,就快速发现了问题:

System.out.println("Hello World!!");
        this.context=context;
        //context.addServiceListener(this, "(objectClass="+HttpService.class.getName()+")");
        System.out.println("Hello World end!!");
        
        ServiceReference serviceReference = context.getServiceReference(HttpService.class.getName());
        HttpService service = (HttpService) context.getService(serviceReference);
        
        //ref=context.getServiceReference(HttpService.class);
        //HttpService service=context.getService(ref);
        service.registerServlet("/demo/hello", new HelloServlet(context),null,null);
        System.out.println("/demo/hello已经被注册");

这篇关于java - osgi框架下开发的web应用,可以启动,但是无法访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆