Warren Crasta
Warren Crasta

Reputation: 488

Output results of a form submission to a JSP page dynamically in real time

I have this form in keyword.jsp.

Form

When the user clicks submit, it calls KeywordPostServlet.java to do some computation. When the computation is done, the user is directed to followMessage.jsp:

enter image description here

Depending on the "number of users to follow", it could be a very long time between when the user clicks submit and when the user sees the results. This is the computation in KeywordPostServlet.java:

for (int i = 0; i < tweets.size(); i++){
    Status s = tweets.get(i);
    twitter.createFriendship(s.getUser().getId());
    msg += i+1 + ". Following @" + s.getUser().getScreenName() + ".<br/>";
}
request.setAttribute("message", msg);
request.getRequestDispatcher("/followMessage.jsp").forward(request, response);

And here is the code to display the results in followMessage.jsp:

<body>
<%
    String msg = (String) request.getAttribute("message");
%>
<%=msg%>
</body>

How can I make it so that the user sees each person they're following in real-time, as opposed to just clicking submit and seeing the results all at once?

Upvotes: 2

Views: 528

Answers (2)

devops
devops

Reputation: 9179

You are working with Twitter API createFrienship.

This seems to be a HTTP-Request. So you are firing in your for-loop too many requests. This costs time.

Try to do it in an extra thread. This is an optimistic strategy, but I believe there is no other way if you want to follow so many users:

new Thread(new Runnable(){

    @Override
    public void run(){
        // your loop with createFriendship(..)
    }

}).start();

And. Use StringBuilder if you are creating a string in a loop

Upvotes: 0

John
John

Reputation: 4006

You should be able to do it with something like this in your server side code:

package org.myorg.example.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class StreamingServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        response.setHeader("Connection", "keep-alive");
        response.setHeader("Transfer-Encoding", "chunked");
        for (int i = 0; i < 5; i++) {
            String str = "Line " + (i + 1) + "<br/>";
            response.getWriter().write(str);
            response.flushBuffer();
            sleep();
        }
        response.getWriter().write("DONE<br/>");
    }

    private void sleep() {
        try {
            System.out.println("Sleeping");
            Thread.sleep(500);
            System.out.println("Done sleeping");
        } catch (Exception exp) {
            throw new RuntimeException(exp);
        }
    }

}

Upvotes: 1

Related Questions