开发者

Can we invoke a servlet inside a webservice method

I have a w开发者_运维百科ebservice method

  @WebMethod
 public void getCapturedImages(String image)
 System.out.println(" image " + image);
 }

And my servlet class is :

   public class GetWebApplicationPathServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static ServletContext context;

public static ServletContext getContext() {
    return context;
}

public static void setContext(ServletContext context) {
    GetWebApplicationPathServlet.context = context;
}

/**
 * @see HttpServlet#HttpServlet()
 */
public GetWebApplicationPathServlet() {
    super();
    // TODO Auto-generated constructor stub
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
 *      response)
 */
protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {

    System.out.println("doGet(HttpServletRequest");
    String path = getServletContext().getRealPath("");

    context = getServletContext();
    String path1 = context.getRealPath("/images");
    System.out.println("path1"+path1);

    /*
     * PrintWriter writer = response.getWriter();
     * writer.println("Application path: " + path);
     */
}

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

can we invoke my servlet class inside that webservice method.


In order to call the class you would have to create an instance of it, construct the HttpServletRequest object yourself and then call it. Or you could make an Http request to it from the web service.

Both solutions aren't recommended. You are better off creating a class that will implement the desired functionality an call it from both places.


You can issue an include using the path that would be used to invoke the servlet from a direct HTTP request:

@Resource
private WebServiceContext context;

private void invokeServlet() throws IOException, ServletException
{
    ServletContext servletContext = (ServletContext) context.getMessageContext().get(MessageContext.SERVLET_CONTEXT);
    HttpServletRequest request = (HttpServletRequest) context.getMessageContext().get(MessageContext.SERVLET_REQUEST);
    HttpServletResponse response = (HttpServletResponse) context.getMessageContext().get(MessageContext.SERVLET_RESPONSE);
    servletContext.getRequestDispatcher("/path/to/servlet").include(request, response);
}

See also: How can I access the ServletContext from within a JAX-WS web service?


You might be able to, but it's problematic. What you need to do is create a method that both can call to perform the functionality you require.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜