简体   繁体   English

执行一个servlet,以便调用我的doGet方法

[英]Executing a servlet so that my doGet method is called

I'm catching a device event, and based on info coming from it it I need to do a login. 我正在捕获一个设备事件,根据它的信息,我需要登录。 If the login is verified, I want to move to the next page of my application. 如果登录经过验证,我想移至应用程序的下一页。

I tried doing it with HttpURLConnection but it doesn't actually execute the servlet. 我尝试使用HttpURLConnection进行此操作,但实际上并未执行servlet。

This is the genetic code I'm using: 这是我正在使用的遗传密码:

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();
}

I also tried 我也试过

InputStream responseInputStream = connection.getInputStream();

but with the same result. 但结果相同。 The doGet method is not being called, and obviously the app doesn't move to the next page. 没有调用doGet方法,显然该应用程序不会移至下一页。

I'm pretty sure this can be done by catching the event inside the JSP page but I was hoping there's an easier solution. 我很确定可以通过在JSP页面中捕获事件来完成此操作,但我希望有一个更简单的解决方案。

EDIT: When submitting the same URL in the browser it all works. 编辑:在浏览器中提交相同的URL时,所有的工作。 The doGet is called and the app moves to the next page. 调用doGet,应用程序移至下一页。

From the 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();
   }
 }

Try with "readStream(in);" 尝试使用“ readStream(in);”

If it still doesn works, the error must be from the URL. 如果仍然无法正常运行,则错误必须来自URL。 u say u have a doGet method at this url, try with your browser to be sure the URL reponds. 您说您在此URL上有一个doGet方法,请尝试使用您的浏览器以确保URL能够响应。 Do you really need a different doGet/doPost ? 您真的需要其他的doGet / doPost吗? use "service" method instead 使用“服务”方法代替

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM