我想从我的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
}
}
提前致谢。 塔潘