학습 4: OSGi 서비스에 액세스하는 서블릿 작성
이 태스크 정보
이 학습서에서 카운터 서비스에 액세스하고 서비스 결과를 표시하는 서블릿을 포함할 웹 사용 번들을 작성합니다. 서블릿은 JNDI InitialContext를 통해 서비스를 찾아서 OSGi 서비스에 액세스합니다.
OSGi 서비스에 액세스하는 서블릿을 작성하려면 다음을 수행하십시오.
웹 사용 번들 작성
이 태스크 정보
프로시저
번들 Manifest에 필수 패키지 추가
이 태스크 정보
프로시저
OSGi 서비스를 찾아보기 위한 구현 작성
이 태스크 정보
프로시저
결과
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
}
}
학습 체크포인트
JNDI InitialContext를 통해 서비스를 검색하여 OSGi 서비스에 액세스하는 서블릿을 작성했습니다.
이 학습에서 다음 주제에 대해 학습했습니다.
- 웹 사용 OSGi 번들 작성 방법.
- 서블릿 작성 방법.
- 가져온 패키지를 사용하여 번들 종속 항목을 가져오는 방법.
- InitialContext를 사용하여 OSGi 서비스를 검색하는 방법.
