我想从我的 httpClient 调用 http servlet 类,我的 httpClient 代码如下
public class ConnectionClientHttp {
public static void main(String[] args) throws Exception {
HttpParams params = new SyncBasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
//Set more parameter if necessary
HttpProcessor httpproc = new ImmutableHttpProcessor(new HttpRequestInterceptor[] {
// Required protocol interceptors
new RequestContent(),
new RequestTargetHost(),
// Recommended protocol interceptors
new RequestConnControl(),
new RequestUserAgent(),
new RequestExpectContinue()});
HttpRequestExecutor httpexecutor = new HttpRequestExecutor();
HttpContext context = new BasicHttpContext(null);
HttpHost host = new HttpHost("IPAddress", Port);
DefaultHttpClientConnection conn = new DefaultHttpClientConnection();
ConnectionReuseStrategy connStrategy = new DefaultConnectionReuseStrategy();
context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host);
try {
if (!conn.isOpen()) {
Socket socket = new Socket(host.getHostName(), host.getPort());
conn.bind(socket, params);
}
BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("POST","/");
StringEntity myEntity = new StringEntity("Some string to send at server");
request.setEntity(myEntity);
request.setParams(params);
httpexecutor.preProcess(request, httpproc, context);
//Response from Server
HttpResponse response = httpexecutor.execute(request, conn, context);
System.out.println("<< Response: " + response.getStatusLine());
} catch (Exception e){
e.printStackTrace();
} finally {
conn.close();
}
}// end of static void main
}// end of class
当我使用
BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("POST","<uri>");
在上面的 URI 中,我需要给出目标类的路径,但在我的例子中,它只是一个部署在服务器上的 servlet 文件,它将从此文件获取请求并在控制台显示内容。
我的问题是应该给出什么路径?根据我对 URI 的了解,它采用路径,http://localhost:8080/erik/loginSubmit!loginDetails.do
但给出的示例在 Struts2 中,但如果我只想执行一些扩展HttpServlet的 servlet 文件,那么我应该给出什么路径以及该文件如何知道哪个文件是发送数据的目标?
我创建了如下目标文件:
public class ExampServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//Received Request and display it at console
}
}
提前致谢。塔班