quickity
quickity

Reputation: 61

Access GWT POST parameters via servlet?

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 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.

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

Upvotes: 1

Views: 4798

Answers (2)

Churro
Churro

Reputation: 4376

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.

Upvotes: 1

Suresh Atta
Suresh Atta

Reputation: 122026

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?)

Upvotes: 1

Related Questions