问题
I currently have a GWT application which uses the RequestBuilde
to
send messages to a servlet I have (using POST
and GET
), and my servlet
(in doPost
and doGet
) "pauses" the request (this is done by using
Servlets 3.0 spec) and adds it to a queue.
Additionally I have a
Daemon thread which runs in the background and "plays" the request
when there is new data.
This works great for me apart from the fact that I'm just sending
strings for now and I'd like to utilize the RPC mechanism to send
Objects.
My question is this:
If I create my myServiceImpl
class which extends
RemoteServiceServlet
how should I handle the doPost
and doGet
if at all?
I need to pause my request to utilize the asynchronous support, where should this be accomplished? I though maybe to call this.getThreadLocalRequest()
method in my RPC method in myServiceImpl
class but I'm not sure how wise that will be.
I'm not
sure I even understand how GWT handles the call from the client side
of that asynchronous interface.
Does it by any chance call the doGet
for example of the servlet and
makes sure it calls the required RPC method?
I'm sorry if I made a mess of things it's just that I can't find more
detailed technical information as exactly how this RPC business works.
Appreciate any lending hand
Ittai
回答1:
To understand RPC forget about POST and GET it works differently (that is from a programming perspective. Internally it does use it, but you don't see or need to understand it, unless you want to do something exotic). A good starting point is on RPC is the GWT docs: http://code.google.com/webtoolkit/tutorials/1.6/RPC.html
To give you a summary. When using RPC your servlet myServiceImpl
needs to implement the methods of an interface named myService
, besides extending the RemoveServiceServlet
. The methods get as arguments the data you want to send to the server.
Next to the myService
interface you need to create an myServiceAsync
interface (both interface's should be in a client subpackage). This myServiceAsync
inteface should contain the same methods as the myService
interface except each method returns void
and has an additional last argument AsyncCallback callback
.
In you client you need to instrument GWT to generate the RPC via GWT.create (see the documentation for the details.
To use RPC, call the methods on the myServiceAsync
interface in your client code and GWT takes care of sending it to the servlet. The servlet will then call the matching method with the arguments you passed on the client. This is done asynchronous. Thus the client return directly from the call.
When the server sends the result back the callback you passed myServiceAsync
is used or OnError
or OnSuccess
is called. OnError
if the method on the server side threw an error, else OnSuccess
. The OnSuccess
will have as argument the return value from what you returned in the method implemented by your servlet.
回答2:
I think the main misunderstanding I had at the time was related to the fact that GWT RPC builds on the servlet mechanism (internally) and so trying to use that mechanism with asynchronous servlets was, at the time, impossible since GWT RPC was built on servlets 2.5 (again, at the time).
来源:https://stackoverflow.com/questions/1425525/using-gwt-rpc-mechanism-with-my-customized-servlet