我有以下代码,但是当我尝试访问/ data-upload网址时,我收到错误“此方法不支持HTTP方法GET。”
Java servlet代码:
package xyz.controllers;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.entity.StringEntity;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServlet;
import javax.servlet.annotation.WebServlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
@WebServlet("/data-upload")
public class GetLocalAreaIds extends HttpServlet {
// HTTP POST request
public void doPost(HttpServletRequest request, HttpResponse response) throws ServletException, IOException {
System.out.println("#VH in doPost method ");
String url = "http://xyz.xyz/search";
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
// add header
post.setHeader("Content-Type", "application/xml");
String elementLocalNameType = request.getParameter("elementLocalNameType");
System.out.println("#VH elementLocalNameType: " + elementLocalNameType);
String localAreaName = request.getParameter("localAreaName");
System.out.println("#VH localAreaName: " + localAreaName);
StringEntity params = null;
try {
params = getStringEntityParams(elementLocalNameType, localAreaName);
System.out.println("#VH params: " + params);
}
catch (Exception e) {
System.out.println("Error while getting elementLocalNameType");
}
post.setEntity(params);
response = client.execute(post);
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + post.getEntity());
System.out.println("Response Code : " +
response.getStatusLine().getStatusCode());
BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
System.out.println("#VH result.toString(): " + result.toString());
}
public void doGet(HttpServletRequest request, HttpResponse response) throws ServletException, IOException {
doPost(request, response);
}
private StringEntity getStringEntityParams(String elementLocalNameType, String localAreaName) throws Exception {
StringEntity params = new StringEntity("<request><workflow>get-element-values-workflow</workflow><get-element-values><element-localname>"+elementLocalNameType+"</element-localname><starts-with>"+localAreaName+"</starts-with><is-csv>True</is-csv></get-element-values></request>");
return params;
}
}
JSP代码:
<%@ taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles" %>
<%@ taglib prefix="user" uri="/WEB-INF/tlds/user.tld" %>
<%@ page import="org.apache.http.client.HttpClient" %>
<%@ page import="org.apache.http.client.methods.HttpGet" %>
<%@ page import="org.apache.http.impl.client.DefaultHttpClient" %>
<%@ page import="GetLocalAreaIds" %>
<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>
<tiles:insertDefinition name="layout">
<tiles:putAttribute name="title">Title</tiles:putAttribute>
<tiles:putAttribute name="main">
<main id="content" role="main" class="group category-page">
<header class="page-header group">
<div class="full-width">
<h1>Data Upload</h1>
</div>
</header>
<div class="browse-container full-width group">
<div id="error">
<p style="color: #ff0000">${error}</p>
</div>
<user:current-local-area msg="<b>LA:</b> !{#localAreaName}"/>
<c:set var = 'la_id' scope = 'session' value = '<user:current-local-area msg="!{#localAreaName}">'/>
<c:set var = "la_id"><user:current-local-area msg="!{#localAreaName}"/></c:set>
<jsp:useBean id="GetLocalAreaIds" class="GetLocalAreaIds"/>
<form action="${pageContext.request.contextPath}/data-upload" method="POST">
<span>Do you want to download the LA IDs for families or individuals?</span><br />
<input type="radio" name="elementLocalNameType" value="la-family-id"> Family IDs<br />
<input type="radio" name="elementLocalNameType" value="la-individual-id"> Individual IDs<br />
<input type="hidden" name="localAreaName" value="${la_id}">
<input class="button" type="submit" value="Submit">
</form>
</div>
</main>
</tiles:putAttribute>
</tiles:insertDefinition>
我已阅读其他一些处理同一问题的帖子,但他们的解决方案对我不起作用。当我尝试将@Override添加到doPost和doGet方法时,我收到一条错误,说“方法不会覆盖或实现超类型中的方法”,即使我正在扩展HttpServlet。
答案 0 :(得分:1)
问题是,get和post都不是从HttpServlet
继承的
改变
doPost(HttpServletRequest request, HttpResponse response)
到
doPost(HttpServletRequest request, HttpServletResponse response)
和
doGet(HttpServletRequest request, HttpResponse response)
到
doGet(HttpServletRequest request, HttpServletResponse response)
修改强> 下一期是
response = client.execute(post);
正如您所说"incompatible types: org.apache.http.HttpResponse cannot be converted to javax.servlet.http.HttpServletResponse"
将其更改为
org.apache.http.HttpResponse my_response = client.execute(post);
...
System.out.println("Response Code : " +
my_response.getStatusLine().getStatusCode());
BufferedReader rd = new BufferedReader(
new InputStreamReader(my_response.getEntity().getContent()));
另请注意,您不会向用户响应输出任何内容,因此如果您在调用客户端(例如浏览器)时什么都没有,这是正常的。或者你用stdout
写下你做的所有事情,然后回复。