执行一个servlet,以便我的doGet方法被调用
问题描述:
我正在捕捉一个设备事件,并根据来自它的信息我需要进行登录。如果登录已验证,我想转到我的应用程序的下一页。执行一个servlet,以便我的doGet方法被调用
我试着用HttpURLConnection
来做,但它并没有真正执行servlet。
这是我使用的遗传密码:
public static String getResponseFromUrl(String url) throws Exception {
URL website = new URL(url);
HttpURLConnection connection = (HttpURLConnection)website.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Content-Type", "application/json; charset=utf-8");
connection.setUseCaches(false);
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String response = "";
String inputLine;
while ((inputLine = in.readLine()) != null)
response += inputLine;
in.close();
return response.toString();
}
我也试过
InputStream responseInputStream = connection.getInputStream();
但具有相同的结果。 doGet
方法没有被调用,显然该应用程序不会移动到下一页。
我很确定这可以通过在JSP
页面内捕获事件来完成,但我希望有一个更简单的解决方案。
编辑:当在浏览器中提交相同的URL它一切正常。 doGet被调用并且应用程序移动到下一页。
答
从DOC:
URL url = new URL("http://www.android.com/");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
readStream(in);
finally {
urlConnection.disconnect();
}
}
与试 “readStream(在);”
如果它仍然无效,错误必须来自URL。 你说你有这个URL的doGet方法,请尝试使用你的浏览器来确保URL响应。你真的需要一个不同的doGet/doPost吗?使用“服务”方法代替
+0
你有没有试过这段代码?也许doGet方法被调用,但不移动到下一页 – MeGoodGuy
doGet()not called mean doGet()not invoked或getResponseFromUrl()not invoke?或其他东西 –
getResponseFromUrl()被调用并且没有错误地运行。但doGet没有被调用。 – Eddy
这意味着在当时获取请求时doGet()没有被调用正确的时候开始?如果doGet not invoke意味着所有其他bla bla不执行。我正确的想法? –