繁体   English   中英

我们可以在webservice方法中调用servlet吗

[英]Can we invoke a servlet inside a webservice method

我有一个网络服务方法

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

我的servlet类是:

   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
}

我们可以在该webservice方法内调用我的servlet类吗?

为了调用该类,您必须创建它的一个实例,自己构造HttpServletRequest对象,然后调用它。 或者,您可以从Web服务向它发出Http请求。

不推荐两种解决方案。 您最好创建一个将实现所需功能的类,从两个地方调用它。

您也许可以,但这是有问题的。 您需要做的是创建一个都可以调用以执行所需功能的方法。

您可以使用将用于从直接HTTP请求中调用servlet的路径来发出一个include:

@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);
}

另请参见: 如何从JAX-WS Web服务中访问ServletContext?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM