2013年8月22日 星期四

從 web service 取得 ServletContext 物件

// ServletContextListener Class


public void contextInitialized(ServletContextEvent sce) {
  try {
    sce.getServletContext().setAttribute("myBook", "Learning java");
  } catch (Exception ex) {
  }
}


// WebService


@Resource
WebServiceContext wsContext;

@WebMethod(operationName = "test1")
public String test1() {
  ServletContext servletContext = (ServletContext) wsContext.getMessageContext().get(MessageContext.SERVLET_CONTEXT);
  String myBook = (String) servletContext.getAttribute("myBook");
}

沒有留言: