简体   繁体   中英

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

I have a doubt with servlets and the doGet method. I'm sorry if this is a wrong question, hopefully somebody will help me with this.

I have a servlet, which I invoke using this code to execute something in it:

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

That makes the servlet to execute certain code. Then the servlet will receive calls from other applications, and will store that information.

I would like to know, if it is possible to access that information stored on the servlet calling the doGet method, or in some other way.

Any help will be appreciated

Thanks in advance.

now my question is, how can invoke the doGet method and get the information from the response?

It's available by the getResponseXxx() methods on HttpMethod class. See also its javadoc .

Eg

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

Unrelated to the concrete problem. HTTP client 3.x is rather legacy. Consider moving to HTTP client 4.x.

I believe I've used the URL class to achieve this in the past.

You can use its openStream() method to get an InputStream you can read.

The returned InputStream should represent the output written by the servlet.

Typically in a web application, the data sent from clients will be stored in some sort of database, either an relational database system, a document based system like couchdb, etc.

You servlet methods should invoke service methods (which you write), which both save and load data as needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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