问题
1) Hi I am new gwt. I want to execute one servlet to another servlet. for example i want to execute servlet2 from servlet1 one. I can execute sevlet1 using RPC call so from servlet1 i want to execute servlet2 which have doPost method i want to execute.
2) I want to use task queue on GAE. so can understood the task queue by reading https://cloud.google.com/appengine/docs/java/taskqueue/overview-push. In this document Enqueue is servlet which create task and worker is another servlet which executes Task Queue code. So how can call enqueue servlet without using html code.
any help?
Thanks in advance
回答1:
(From a servlet on the server)
To call the call enqueue servlet without using html code, you use a RequestDispatcher and forward the request.
String enqueueURL = "/enqueue";
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(destination);
dispatcher.forward(request, response);
To sent a List to another servlet, use request.setAttribute
request.setAttribute("myList", list);
To obtain it in a different servlet, use request.getAttribute
List value = (List)request.getAttribute("myList")
From a design perspective, if your servlet is functioning as a Controller such as in a Model-View-Controller system, then it's use is appropriate.
(from GWT client side code)
- Method A]
Simply make an RPC call and in the method that handles it put your queue code:
public class MyServiceImpl extends RemoteServiceServlet implements
MyService {
public void myMethod(String key) {
Queue queue = QueueFactory.getDefaultQueue();
byte[] buf;
TaskOptions taskOptions= TaskOptions.Builder.withUrl("/tasks/worker").method(Method.POST);
taskOptions.payload(buf);
queue.add(taskOptions);
}
}
If you need help converting the List to a byte[], see this or something like it (i.e. coverting a Java Object to an byte[] array ..don't forget to include a cast to get it back into a List)
- Method B]
To call a servlet from client code in GWT, simply use a RequestBuilder
import com.google.gwt.http.client.*;
...
String url = "http://www.myurl.com/enqueue";
RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, URL.encode(url));
try {
Request request = builder.sendRequest(null, new RequestCallback() {
public void onError(Request request, Throwable exception) {
// Couldn't connect to server (could be timeout, SOP violation, etc.)
}
public void onResponseReceived(Request request, Response response) {
if (200 == response.getStatusCode()) {
// Process the response in response.getText()
} else {
// Handle the error. Can get the status text from response.getStatusText()
}
}
});
} catch (RequestException e) {
// Couldn't connect to server
}
See GWT Docs on RequestBuilder
回答2:
Servlets only be communication between the client and the server. Your server should do whatever it needs to to marshal your information and then pass it to the business layer of your application.
So, really, having servlet1 call servlet2 is the wrong approach.
Both servlets would return the result of the same method in your business layer. This makes your code infinitely easier to test and maintain.
For example, if you want to enqueue the same request from two different servlets, you could create a QueueManager like
public class QueueManager {
public static void startWorker(String key){
Queue queue = QueueFactory.getDefaultQueue();
queue.add(TaskOptions.Builder.withUrl("/worker").param("key", key));
}
}
Then call it from your servlet by
QueueManager.startWorker(aKey);
来源:https://stackoverflow.com/questions/32741692/how-to-call-one-servlet-to-another-servlet-in-gwt-using-java