我有一个网络服务方法
@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类。
答案 0 :(得分:2)
为了调用该类,您必须创建它的实例,自己构造HttpServletRequest对象然后调用它。或者您可以从Web服务向它发出Http请求。
不建议使用这两种解决方案。你最好创建一个实现所需功能的类,并从两个地方调用它。
答案 1 :(得分:1)
你可能会,但这是有问题的。您需要做的是创建一个方法,两者都可以调用以执行您需要的功能。
答案 2 :(得分:1)
您可以使用从直接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);
}
另请参阅:How can I access the ServletContext from within a JAX-WS web service?