0

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

2 Answers2

0

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);
Thom
  • 14,013
  • 25
  • 105
  • 185
  • Thank you The Thom for your valuable comment. Actually i want to implement Task queue in my application so to Create task queue first we create one servlet and its implemention code we write in another servlet so How can i achieve this And how can i call first servlet using by RPC call or anything else – Vijay Chougule Sep 23 '15 at 14:24
  • You move your task queue outside of your servlets. An object does one thing. A servlet must communicate with the JEE server and kick off a task. Managing a Task Queue is beyond the scope of a single object. That's why it needs to be moved outside. The problem you're experiencing points to a design flaw. You don't want to solve it by calling servlet to servlet. – Thom Sep 23 '15 at 14:31
  • Thanks Thom but if i move Task queue from servlet so how can create it and how can execute it and how can user initiate it could you please explain me in brief. and also give an example if possible – Vijay Chougule Sep 23 '15 at 14:42
  • Without seeing your code, I can't begin to answer this question. You may need to use a singleton. – Thom Sep 23 '15 at 14:49
  • just now i implement task queue as given in this docs https://cloud.google.com/appengine/docs/java/taskqueue/overview-push just i dont want to use html code. So without using html code How can i execute Enqueue class – Vijay Chougule Sep 23 '15 at 14:52
  • Thanks a lot Thom for your example but I want to send a List to Startworker method how can i send it and how to add list in Param method – Vijay Chougule Sep 24 '15 at 06:55
0

(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

Community
  • 1
  • 1
user1258245
  • 3,639
  • 2
  • 18
  • 23
  • Thanks for your valuable comment but In GWT we can not use RequestDispatcher – Vijay Chougule Sep 24 '15 at 10:04
  • Actually I want call Enqueue sevlet without using html code as given example https://cloud.google.com/appengine/docs/java/taskqueue/overview-push so how can i call Enqueue servlet from user request – Vijay Chougule Sep 24 '15 at 10:06
  • RequestDispatcher will run in a servlet and should work fine with GWT. It doesn't run in client code. To call from client code, simply use an RPC call and then schedule the task in the method handling the call in your ServiceImpl class. Otherwise you can call a servlet from GWT client code, but it's easier just to make a RPC call if you are using it already. – user1258245 Sep 24 '15 at 11:50
  • Thank you so much just i want one more information as per Method A RPC call in MyserviceImpl i want to send one list to param parameter how can i send becuase in worker i want save this list using ofy().save.entities(list).now and param only accepts string i want to send on list how can i send ?? – Vijay Chougule Sep 24 '15 at 13:42
  • I want to send arraylist to worker how can i send it? by using param it only accept string value. any idea please help me – Vijay Chougule Sep 24 '15 at 14:32
  • Your question is "How to call one servlet to another servlet in GWT Using Java". I think that is answered. Anyway, convert the arraylist to byte[] and send it via payload(byte[] payload). – user1258245 Sep 24 '15 at 15:18
  • Thanks a lot for your valuable comments – Vijay Chougule Sep 25 '15 at 07:33