66305 HI, I want to invoke a Servlet from the client side, as a Onclick event on a Button in java programming language. I开发者_StackOverflow中文版 read somewhere through anchor tag we can call a servlet, but i did not figure out the solution or Syntax properly.
Anyone please me.
Thanks, sekhar
Typically you use a RemoteService
to communicate with a server. To do that, you create an interface that extends RemoteService
and an implementation on the server that implements that interface and extends RemoteServiceServlet
. All that is described in more detail here. If you need to make a call to some servlet that isn't a remote service servlet, you can use RequestBuilder
to send an HTTP request to that servlet's URL.
An Anchor
is basically just a normal HTML <a>
tag which could link to the URL of a servlet.
You have to manually create one httpClient & call GET/POST method.
Below is sample code using POST method:
//---
HttpClient client = new HttpClient();
BufferedReader br = null;
PostMethod method = new PostMethod("http://someUrl.com");
method.addParameter("p", "\"parameter\"");
int statusCode = client.executeMethod(method);
if(statusCode == HttpStatus.SC_NOT_IMPLEMENTED) {
System.out.println("Not Supported");
method.getResponseBodyAsString();
} else {
br = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream()));
String readLine;
while(((readLine = br.readLine()) != null)) {
System.out.println(readLine);
}
}
//---
精彩评论