ここで重要なことは、getリクエストを実装する方法が異なるため、使用する必要のあるapacheのパッケージを明示的に言うことです。
たとえば、Apache Commons
またはを使用できますHttpComponents
。この例では、HttpComponents (org.apache.http.*)
リクエストクラス:
package request;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import Task;
public void sendRequest(Task task) throws URISyntaxException {
URIBuilder uriBuilder = new URIBuilder();
uriBuilder.setScheme("http")
.setHost("localhost")
.setPort(8080)
.setPath("/TesteHttpRequest/TesteDoLucas")
.addParameter("className", task.getClassName())
.addParameter("dateExecutionBegin", task.getDateExecutionBegin())
.addParameter("dateExecutionEnd", task.getDateExecutionEnd())
.addParameter("lastDateExecution", task.getDateLastExecution())
.addParameter("numberExecutions", Integer.toString(task.getNumberExecutions()))
.addParameter("idTask", Integer.toString(task.getIdTask()))
.addParameter("numberExecutions" , Integer.toString(task.getNumberExecutions()));
URI uri = uriBuilder.build();
HttpGet getMethod = new HttpGet(uri);
CloseableHttpClient httpclient = HttpClients.createDefault();
CloseableHttpResponse response = null;
try {
response = httpclient.execute(getMethod);
} catch (IOException e) {
//handle this IOException properly in the future
} catch (Exception e) {
//handle this IOException properly in the future
}
}
Tomcat v7.0サーバーを使用している場合、上記のクラスはタスクを受信し、リンクhttp:// localhost:8080 / TesteHttpRequest/TesteDoLucasの特定のサーブレットに送信します。
私の動的Webプロジェクトの名前はTesteHttpRequestで、サーブレットはURL /TesteDoLucasに参加しています。
タスククラス:
package bean;
public class Task {
private int idTask;
private String taskDescription;
private String dateExecutionBegin;
private String dateExecutionEnd;
private String dateLastExecution;
private int numberExecutions;
private String className;
public int getIdTask() {
return idTask;
}
public void setIdTask(int idTask) {
this.idTask = idTask;
}
public String getTaskDescription() {
return taskDescription;
}
public void setTaskDescription(String taskDescription) {
this.taskDescription = taskDescription;
}
public String getDateExecutionBegin() {
return dateExecutionBegin;
}
public void setDateExecutionBegin(String dateExecutionBegin) {
this.dateExecutionBegin = dateExecutionBegin;
}
public String getDateExecutionEnd() {
return dateExecutionEnd;
}
public void setDateExecutionEnd(String dateExecutionEnd) {
this.dateExecutionEnd = dateExecutionEnd;
}
public String getDateLastExecution() {
return dateLastExecution;
}
public void setDateLastExecution(String dateLastExecution) {
this.dateLastExecution = dateLastExecution;
}
public int getNumberExecutions() {
return numberExecutions;
}
public void setNumberExecutions(int numberExecutions) {
this.numberExecutions = numberExecutions;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
}
サーブレットクラス:
package servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/TesteDoLucas")
public class TesteHttpRequestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String query = request.getQueryString();
System.out.println(query);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
送信されたクエリパラメータはコンソールに表示されます。
className = java.util.Objects%3B&dateExecutionBegin = 2016%2F04%2F07 + 22%3A22%3A22&dateExecutionEnd = 2016%2F04%2F07 + 06%3A06%3A06&lastDateExecution = 2016%2F04%2F07 + 11%3A11%3A11&numberExecutions = 10
エンコーディングを修正するには、次を参照してください:HttpServletRequest UTF-8 Encoding