0

I have a Greasemonkey script that reads contents of a file from one site and sends it via HTTP POST method to a servlet in my GWT application. Once the content is available in my servlet I want to pass on the file content to the GWT client(i.e. trigger open the application with the file contents).

For triggering the application, I use this in my servlet code:

response.sendRedirect("/path/to/my/application");

Is there any way to read the file contents in the onModuleLoad() of my GWT entry point class? Because I am redirecting the response from servlet to the client, will the response contain the string file that was read from the other site?

Currently what I do is,

  • read the file from the site and send it via HTTP-POST to my server.

  • store the String contents in session

  • send a cookie to the client to indicate that a file is available in server session to be read

  • client on reading the cookie, sends a request to the server to get the file.

I find that this method seems to be sort of round about. Is there an easier way to do this, by accessing the file contents directly by reading the response content in client side?

somesh
  • 2,509
  • 3
  • 16
  • 24

2 Answers2

0

Use Timer that will continuously look into user session using GWT RPC call. If file content is found in session then just send the content to the client.

Put below code in your Entry point class.

    Timer timer = new com.google.gwt.user.client.Timer() {

        @Override
        public void run() {
             //GWT RPC call to check the user session
             // if you want then cancel the timer
             //timer.cancel();
        }

    };
    timer.scheduleRepeating(5000); // 5 seconds

Or you can try this one also

If /path/to/my/application is redirecting to a JSP file then you can read session attribute in JSP file.

Server side:

session.setAttribute("keyname",fileContent);

JSP:

<div id="myHiddenDiv" style="visibility: hidden;"><%=session.getAttribute("keyname")%></div>

Entry Point:

System.out.println(getElementById("myHiddenDiv").getInnerHTML());
...

public static final native Element getElementById(String id) /*-{
    return $wnd.document.getElementById(id);
}-*/;
Braj
  • 46,415
  • 5
  • 60
  • 76
  • Timer is fine. But wouldn't it add an overhead, for it to keep running in the background continuously? And also, I was looking at if there are ways to access the HttpResponse object(with which the response.sendRedirect call was made) in the onModuleLoad part? – somesh Apr 10 '14 at 15:25
  • Just cancel the timer after getting the contents. There is no overhead on using Timer because its just like a `JavaScript` function that is called after a specific time. – Braj Apr 10 '14 at 16:36
  • What about second option? – Braj Apr 10 '14 at 16:40
  • I am not using JSP in my application. Looks like a nice idea though. – somesh Apr 11 '14 at 02:11
0

You might want use load-on-startup in your web.xml file and then override the init() method in your servlet to do the required task.

<servlet>
    <servlet-name>startupTasks</servlet-name>
    <servlet-class>xxx.xxxxxx.server.StartupTasksServlet</servlet-class>
    <load-on-startup>0</load-on-startup>
</servlet>

Additional Info can be found here

Community
  • 1
  • 1
pratZ
  • 3,078
  • 2
  • 20
  • 29
  • This is for specifying start up order, right? My query was like on a simpler way of accessing contents via a redirection. – somesh Apr 10 '14 at 15:29
  • This is for specifying the things you want to do on start of the application. i.e. before onModuleLoad(). It will run when your server is started. If your application doesn't run without the file content you might try it. – pratZ Apr 11 '14 at 02:51