jennifer
jennifer

Reputation: 8261

Can we invoke a servlet inside a webservice method

I have a webservice 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.

Upvotes: 0

Views: 4904

Answers (3)

Stefano Travelli
Stefano Travelli

Reputation: 1927

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?

Upvotes: 1

kgiannakakis
kgiannakakis

Reputation: 104178

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.

Upvotes: 2

trojanfoe
trojanfoe

Reputation: 122391

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.

Upvotes: 1

Related Questions