簡體   English   中英

我可以調用doGet方法並從servlet中檢索信息嗎?

[英]Can I invoke the doGet method and retrieve information from a servlet?

我對servlet和doGet方法有疑問。 如果這是一個錯誤的問題,我很抱歉,希望有人會幫助我。

我有一個servlet,我使用此代碼調用它來執行其中的內容:

public static void sendBeingRequestFromSimulator(String param2, String message) throws Exception
   {
      HttpClient client = new HttpClient();
      GetMethod method = new GetMethod(SERVER_URL);

      NameValuePair[] parameterArray = new NameValuePair[3];
      parameterArray[0] = new NameValuePair("param1", "begin");
      parameterArray[1] = new NameValuePair("param2", param2);
      parameterArray[2] = new NameValuePair("msg", message);
      method.setQueryString(parameterArray);
      client.executeMethod(method);
}

這使得servlet執行某些代碼。 然后servlet將接收來自其他應用程序的調用,並將存儲該信息。

我想知道,如果可以訪問存儲在調用doGet方法的servlet上的信息,或者以其他方式。

任何幫助將不勝感激

提前致謝。

現在我的問題是,如何調用doGet方法並從響應中獲取信息?

它可以通過HttpMethod類上的getResponseXxx()方法獲得。 另見它的javadoc

例如

// ...
client.executeMethod(method);
int status = method.getStatusCode();
Header[] headers = method.getResponseHeaders();
String body = method.getResponseBodyAsString();

具體問題無關 HTTP客戶端3.x相當傳統。 考慮轉移到HTTP客戶端4.x.

我相信我過去曾使用過URL類來實現這一目標。

您可以使用其openStream()方法獲取可以讀取的InputStream。

返回的InputStream應表示servlet寫入的輸出。

通常在Web應用程序中,從客戶端發送的數據將存儲在某種數據庫中,可以是關系數據庫系統,基於文檔的系統,如couchdb等。

servlet方法應該調用服務方法(你編寫的),它們根據需要保存和加載數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM