< Previous | Next >

Lesson 4: Create a servlet that accesses an OSGi service

About this task

In this lesson, you create a web enabled bundle to contain a servlet that accesses the counter service and displays the results of the service. The servlet accesses the OSGi service by looking up the service through a JNDI InitialContext.

Creating a Web enabled bundle

About this task

To create the web enabled bundle that contains the servlet:

Procedure

  1. Click File > New > Other and then expand OSGi.
  2. Click OSGi Bundle Project and then click Next. The New OSGi Bundle Project opens.
  3. In the Project name field, type CounterWebBundle.
  4. Select one of the following servers from the Target runtime list:
    • WebSphere Application Server v7.0
    • WebSphere Application Server v8.0
    • WebSphere Application Server v8.5
    • WebSphere Application Server V8.5 Liberty
  5. In the Configuration section, click Add Web support and select Web 2.5 from the web support list.
  6. Ensure that CounterApp is displayed in the Application project field and then click Finish.

Creating a servlet

About this task

To create the servlet that accesses the counter service:

Procedure

  1. In Enterprise Explorer, right-click CounterWebBundle and then select New > Servlet. The Create Servlet wizard opens.
  2. In the Java package field, type com.ibm.ws.eba.servlet.
  3. In the Class name field, type CounterServlet and then click Finish. The servlet is created in the bundle and it opens in the editor.

Adding required packages to the bundle manifest

About this task

To add the required packages to the bundle manifest:

Procedure

  1. Double-click Manifest: CounterWebBundle to open it in the editor.
  2. Switch to the Dependencies tab.
  3. In the Imported Packages section, click Add. The Package Selection dialog opens.
  4. In the Exported Packages field, type com.ibm.ws.eba.counter.
  5. Select com.ibm.ws.eba.counter from the list and then click OK. The Imported Packages section looks similar to the following diagram:
    Imported packages.
    Note: If your target runtime server is WebSphere Application Server V8.5 Liberty, then you must also import the javax.naming package. Importing the javax.naming package prevents a ClassNotFoundException error when you run the application. The following steps describe how to import the javax.naming package:
    1. In the Imported Packages section, click Add. The Package Selection dialog opens.
    2. In the Exported Packages field, type javax.naming.
    3. Select javax.naming from the list and then click OK.
    If you do not import the package, a ClassNotFoundException error occurs when you run the application on WebSphere Application Server with Liberty. The error occurs because the application expects the package with the missing class to be provided by the server runtime instead of being provided as a user package.
  6. Save the bundle manifest file.

Creating the implementation to look up an OSGi service

About this task

The following steps describe how to create the implementation for the doGet() method, which looks up an OSGi serving by using InitialContext:

Procedure

  1. In Enterprise Explorer, double-click CounterServlet.java to open it in the editor.
  2. Locate the doGet() method and add the following implementation:
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		Counter counter;
    		try {
    			InitialContext ic = new InitialContext();
    			counter = (Counter) ic.lookup("osgi:service/"+Counter.class.getName());
    			response.getOutputStream().println("counter="+counter.getCount());
    		}
    		catch (NamingException e) {
    			e.printStackTrace(System.out);
    		}
    	}
  3. In the main menu click Source > Organize Imports. The Organize Imports dialog opens.
  4. Select com.ibm.ws.eba.counter.Counter and then click Finish.
  5. Save CounterServlet.java.

Results

The servlet code looks similar to the following code:
package com.ibm.ws.eba.servlet;

import java.io.IOException;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ibm.ws.eba.counter.Counter;

/**
 * Servlet implementation class CounterServlet
 */
public class CounterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public CounterServlet() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Counter counter;
		try {
			InitialContext ic = new InitialContext();
			counter = (Counter) ic.lookup("osgi:service/"+Counter.class.getName());
			response.getOutputStream().println("counter="+counter.getCount());
		}
		catch (NamingException e) {
			e.printStackTrace(System.out);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}

Lesson checkpoint

You created the servlet that accesses an OSGi service by looking up the service through a JNDI InitialContext.

In this lesson, you learned about the following topics:
  • How to create a web enabled OSGi bundle.
  • How to create a servlet.
  • How to import bundle dependencies by using Imported Packages.
  • How to look up an OSGi service by using InitialContext.
< Previous | Next >
Icon that indicates the type of topic Tutorial lesson topic
Timestamp icon Last updated: July 17, 2017 21:58

File name: counter_lesson4.html