1

I'm creating a GWT application that will be accessed by a POST request, which contains parameters I care about (user id, etc.).

All the reading I've done so far has led me to believe that I should create a servlet (I'm using tomcat) that will handle the POST parameters and then forward to my GWT application. I've gotten this working, but I'm still having trouble passing this data to my application. I've seen 3 suggested approaches:

  • Save data to context: I have this working right now, but I'm not happy with it. When the servlet is accessed, I parse the parameters and update the context of my GWT web application and then forward to the application where I make an RPC call to read the context. This does what I want it to, but this creates a race condition when multiple users try to access the application at the same and the context is rapidly changing.
  • Store data in session: I've tried saving the data to the request session in my servlet, and then accessing the session in my RPC, but I always get a new/different session, so I assume I'm mucking this up somewhere.

Save session on servlet

HttpSession session = request.getSession();
session.setAttribute("test", "testValue");
response.sendRedirect(response.encodeRedirectURL("/GWT_Application"));

Access session in RPC

HttpSession session = this.getThreadLocalRequest().getSession();
session.getAttribute("test");

This returns a different session, which results in the "test" attribute being null.

  • Pass data in URL: My application will be opened in an iframe, meaning Window.location.getParameter() will not be usable.

Any help would be greatly appreciated! I'm still learning GWT and web development in general so don't be afraid to call me out on any obvious or silly mistakes.

Thanks!

SOLUTION

I figured out what the issue was with my session approach: the servlet in which I was previously trying to save the session data was in a separate tomcat web app from my GWT application. Moving them to the same web app solved my problems and it now works. I'm not sure, but I'm guessing that this was a problem because redirecting to another web app switches the context. I'll outline my whole approach in the hopes this saves someone else some time later:

Put your servlet code in the server folder of your GWT project:

package GWTApplication.server;
public class myServlet extends HttpServlet {
  protected void doPost(HttpServletRequest request, HttpServletResponse response) {
    session.setAttribute("myAttribute", request.getParameter("myParam");
    // handle rest of POST parameters
    response.sendRedirect(response.encodeRedirectURL("/GWTApplication");
  }
}

Map servlet in your GWT application's web.xml:

<servlet>
  <servlet-name>myServlet</servlet-name>
  <servlet-class>GWTApplication.myServlet</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>myServlet</servlet-name>
  <url-pattern>/myServlet</url-pattern>
</servlet-mapping>

This servlet should now be accessible at .../GWTApplication/myServlet

Next make a standar RPC. Within whatever method you will be calling in the ServiceImpl class on the server:

HttpSession session = this.getThreadLocalRequest().getSession();
return session.getAttribute("myAttribute");

Finally, make your RPC call in the onModuleLoad() method of you GWT application. As a recap:

  1. Send the original POST request to the servlet
  2. Save POST parameters to session variables
  3. Redirect to GWT application
  4. Make RPC call in onModuleLoad()
  5. Read session variables in ServiceImpl class
quickity
  • 61
  • 1
  • 7
  • Not really sure if I understood the issue here. can you elaborate on the flow? – mithrandir Aug 27 '13 at 17:52
  • There is an existing application (Sakai - a learning management system), which makes a POST request to my GWT application and displays it inside an iFrame within the existing application. I'm basically just trying to find the best way to get the POST parameters to my GWT application. Did that help? – quickity Aug 27 '13 at 18:29

2 Answers2

1

You can talk with servlets through RPC call in GWT

You need to make a RPC call in the starting point of GWT application.

Set that data to serverside session and get the session data in servceImpl call of GWT which extends to RemoteServiceServlet.

Example :

YourServiceImpl extends RemoteServiceServlet  {

@ovveride
doGet(){
  //you can access session here
}

@ovveride
doPost(){
//you can access session here
}

@ovveride
doPut(){
//you can access session here
}

----your other methods




}

A brief Example I wrote here:How to make an GWT server call(GWT RPC?)

Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • Are you suggesting that I store the parameters to the session on my servlet, and then access the session within a GWT RPC call? I've tried that and have been unable to access the same session. – quickity Aug 27 '13 at 18:35
  • @quickity can you please show how you are accessing the `session` ??If you are unable to access the `session` ,It's weird and causes more and more problems further. – Suresh Atta Aug 27 '13 at 18:42
  • I've updated my question to show how I'm saving the session on my servlet, redirecting to my GWT application, and then accessing the session in my RPC. – quickity Aug 27 '13 at 18:52
  • That's really hard to believe..If you are not hesitate,could you please check the Id's of the both sessions ?? – Suresh Atta Aug 27 '13 at 19:01
  • I've printed both to my tomcat log and they're always different. Maybe I'm not calling my RPC in the correct place? I'm making my RPC call from within the `onModuleLoad` (the entry point) of my GWT application. Is that correct? – quickity Aug 27 '13 at 19:18
  • What you are doing is correct.You should do there.Is there any place that you are creating session explicitly in servlets ? like `req.getSession(true);` – Suresh Atta Aug 27 '13 at 19:40
  • Are both the applications running in the same domain? Potentially the issue could be that your learning solution is running on a different domain from your iFrame application. If the domain is the same then the browser would send the session information to your GWT application. – mithrandir Aug 27 '13 at 19:47
  • The servlet and GWT application are separate web apps on domain1, while the existing application (learning solution) is on domain2. Is this an issue? – quickity Aug 27 '13 at 20:30
  • @quickity So they are sub domains ?? or two completely separate domain ?? If both are two different domains :Session sharing is not possible.[read this](http://stackoverflow.com/questions/665941/any-way-to-share-session-state-between-different-applications-in-tomcat), If subdomains it's little easy to share with help of cookies: [see here](http://stackoverflow.com/questions/4811877/share-session-data-between-2-subdomains) – Suresh Atta Aug 27 '13 at 20:38
  • Your resources led me to my solution, which I've posted above. Thank you so much for all your help! I would've been lost without it. – quickity Aug 27 '13 at 23:36
  • So you know why that happened? You know that how servlets instantiate right ? When you call threads local request ..that not the local to to your running thread because those two apps are running on different threads .. – Suresh Atta Aug 28 '13 at 06:32
1

Since RemoteServiceServlet extends HttpServlet, you can just override doPost() method to access your POST requests. Don't forget to call super.doPost() EDIT: This doesn't work because the method is finalized in AbstractRemoteServiceServlet so it cannot be overridden.

Also, GWT Servlets POST data using the proprietary GWT RPC format. Read more about that format and how to interpret it here: GWT RPC data format

EDIT

There are several methods you can override in your ServiceImpl class that extends RemoteServiceServlet:

  1. public String processCall(String payload) will give you a String representation of the incoming request.
  2. protected void onAfterRequestDeserialized(RPCRequest rpcRequest) will give you a RPCRequest object that has an array of parameters, along with the method that was called.
  3. public void service(ServletRequest request, ServletResponse response) will give you all the Attributes of the HTTP request.
Community
  • 1
  • 1
Churro
  • 4,166
  • 3
  • 25
  • 26
  • All the reading I've done says that you cannot directly access POST parameters within a GWT application. – quickity Aug 27 '13 at 18:37
  • I'm sorry, you can override either the the `service(ServletRequest, ServletResponse)` method, or the `processCall(String)` method in your ServiceImpl class. If you use `service()`, you can do `request.getParameterMap()` to get parameters. If you use `processCall()`, you will see the POST request as a String, which includes the parameters. – Churro Aug 27 '13 at 19:57
  • Ok, I tried out some code, and here's what I found: If you override `protected void onAfterRequestDeserialized(RPCRequest rpcRequest)`, the `RPCRequest` object will give you a list of parameters. I think this is the best way. In this method, you don't have to call `super.onAfterRequestDeserialized()` because the super implementation has nothing in it since it was intended to be overridden. – Churro Aug 27 '13 at 20:08
  • Did you try it? Any luck? – Churro Aug 28 '13 at 23:00
  • Yeah, sorry for not getting back to you. I was unable to get it working when the servlet and the GWT applications were separate web apps on my server. I never tried after moving my servlet into the same web app as my GWT application, though, which may have been my issue. – quickity Aug 29 '13 at 00:37
  • I see. Yes, that is definitely why it wouldn't work. If you want to have the server code separate from client, you would have to write a basic `HttpServlet`, but then you can't take advantage of GWT RPC. – Churro Aug 29 '13 at 16:20