如何在GWT中调用一个servlet到另一个servlet使用Java [英] How to call one servlet to another servlet in GWT Using Java
问题描述
$ b $ 2我想在GAE上使用任务队列。所以通过阅读 https://cloud.google.com可以理解任务队列/应用服务引擎/文档/ JAVA /任务队列/概述推。在本文档中,Enqueue是创建任务的servlet,Worker是执行Task Queue代码的另一个Servlet。那么如何在不使用html代码的情况下调用enqueue servlet。
任何帮助?
预先致谢
(从服务器上的servlet)
$ b
调用调用enqueue servlet而不使用html代码,您使用 RequestDispatcher 和转发请求。
字符串enqueueURL =/ enqueue;
RequestDispatcher dispatcher = getServletContext()。getRequestDispatcher(destination);
dispatcher.forward(request,response);
要发送List到另一个servlet,请使用 request.setAttribute
request.setAttribute(myList,list);
要在不同的servlet中获取它,请使用request.getAttribute
列表值=(列表)request.getAttribute(myList)
$从设计的角度来看,如果你的servlet在模型 - 视图 - 控制器系统中起着控制器的作用,那么它的使用就是合适的。
b
(来自GWT客户端代码)
- 方法A]
只需进行RPC调用,并在处理它的方法中放入队列代码:
public class MyServiceImpl extends RemoteServiceServlet implements
MyService {
$ b $ public void myMethod(String key){
队列队列= QueueFactory.getDefaultQueue();
byte [] buf;
TaskOptions taskOptions = TaskOptions.Builder.withUrl(/ tasks / worker)。method(Method.POST);
taskOptions.payload(buf);
queue.add(taskOptions);
$ b如果你需要帮助将List转换为一个字节[ ],请参阅这个或类似的东西(即将一个Java Object转换为一个byte []数组..不要忘记包含一个强制转换以将其返回到List中)
要从GWT中的客户端代码中调用一个servlet,只需简单地使用
使用 RequestBuilder
import com.google.gwt.http.client。*;
...
字符串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 '连接到服务器(可能是超时,违反SOP等)
}
public void onResponseReceived(Request request,Response response){
if(200 == response .getStatusCode()){
//在response.getText()中处理响应
} else {
//处理错误,可以从response.getStatusText()获取状态文本
}
}
});
} catch(RequestException e){
//无法连接到服务器
}
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
解决方案 (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
这篇关于如何在GWT中调用一个servlet到另一个servlet使用Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!