Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Servlet request time out error

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Servlet request time out error

    Hy all

    our development environment is:Struts2.0,JSp,Servlet,HTML,JQUERY,

    we have issue regarding the request time out in one of our struts action handler method.

    Scenario of the issue is below:

    Following method is our strut's action handler

    public String processDataAndReturn() throws Exception
    {

    Process1() --in this process we calling server code for perform the some security process on the large data [approx. more than 100mb size data]
    //Here Process1 Process on the large data and it is return resulted data...then we pass this resulted data to the Process2() for further processing
    //At here we can not improve the Process1() speed...it is up to date.

    //Here the process1() take approx. 4 minute for request processing, that's why we get the request timeout error in the browser.

    HttpServlet request is time out but the following process is still going on,due to the request time out error ,user can not get the file for download.
    Process2()---this process write that data to the file, and then we give that file to user .

    return success
    }

    Above issue is not occurs when the data is not larger.It is working perfect for the small size data.
    And for the solution we can not skip the process1() in any case.We get the above issue with more than 100mb size file process.

    How can i solve the above issue?Is there any other development strategy to resolve the above issue?
    any input will be appreciated for the issue.

    Thanks in advance
    Yatin Baraiya


  2. #2
    Member
    Join Date
    Apr 2012
    Location
    Superior, CO, USA
    Posts
    80
    Thanks
    0
    Thanked 14 Times in 14 Posts

    Default Re: Servlet request time out error

    So effectively you've got an asynchronous process but you're trying to treat it as a synchronous process.

    My recommendation would be two fold. First, no user wants to wait 4 minutes for a result in a web environment. You're going to have users reloading and/or giving up and sending in bug reports. So, at the very least, send the request off from the browser to start the process and then, via JavaScript, poll the back end to see if the process is done. You only have to poll like every 10 seconds or so which isn't very expensive. Of course, that depends on many factors but without support of something like WebSockets a poll is the safest way.

    The second recommedation which is closely related to the first is to have process1() be able to post some sort of progress. Even if it's just an estimate your front end can then display "10% done", "20% done" and so on. Again, the last thing you want is for your web page to look like it is hung or dead.
    Need Java help? Check out the HotJoe Java Help forums!

  3. #3
    Junior Member
    Join Date
    May 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Re: Servlet request time out error

    Quote Originally Posted by stdunbar View Post
    So effectively you've got an asynchronous process but you're trying to treat it as a synchronous process.

    My recommendation would be two fold. First, no user wants to wait 4 minutes for a result in a web environment. You're going to have users reloading and/or giving up and sending in bug reports. So, at the very least, send the request off from the browser to start the process and then, via JavaScript, poll the back end to see if the process is done. You only have to poll like every 10 seconds or so which isn't very expensive. Of course, that depends on many factors but without support of something like WebSockets a poll is the safest way.

    The second recommedation which is closely related to the first is to have process1() be able to post some sort of progress. Even if it's just an estimate your front end can then display "10% done", "20% done" and so on. Again, the last thing you want is for your web page to look like it is hung or dead.

    hello stdunbar

    First of thanks for the reply...

    As per your first recommendation , you will be said that divide the whole process in two request. am i right? and what is means of the poll ? i did not fully understand your first recommendation.

    and as per the second recommendation , you said that whole operation process should be in some progress.
    is there any available api or any code to achieve the progressive operation ?

    Hello can you suggest me any api or technical solution which is helpful me for my case?

    Thanks & Regards
    Yatin Baraiya

  4. #4
    Member
    Join Date
    Apr 2012
    Location
    Superior, CO, USA
    Posts
    80
    Thanks
    0
    Thanked 14 Times in 14 Posts

    Default Re: Servlet request time out error

    Sorry if it wasn't clear. Let's try it again.

    In your Servlet you likely have at least a doGet() method - I'm going to guess that that is what you are showing. You need to break up the work into two different parts. Something about like:

    public class ProcessService extends HttpServlet {
    // in this example the Result class holds whatever the result of your compuation is
        private static HashMap<String, Result> results = new HashMap<String, Result>();
     
    public void doGet(HttpServletRequest request, HttpServletResponse response) {
        String action = request.getParameter( "action" );
        if( action.equals( "processData" ) )
            processDataAndReturn( request, response );
        else if( action.equals( "getResults" ) )
            getResults( request, response );
    }
     
    private String processDataAndReturn(HttpServletRequest request, HttpServletResponse response) throws Exception {
        ComputationThread compThread = new ComputationThread( request.getSession().getId() );
        compThread.start();
     
        response.sendRedirect( "inProgress.jsp" );
    }
     
    private void getResults( HttpServletRequest request, HttpServletResponse response ) {
        Result result = results.get( request.getSession().getId() );
     
        if( result != null ) {
            // return result to browser in whatever way you do now.
        }
    }
     
     
    private static class ComputationThread extends Thread {
        private String sessionId;
     
        public ComputationThread( String id ) {
            sessionId = id;
        }
     
        @Override
        public void run() {
            Process1();
            Result result = Process2();
     
            results.put( sessionId, result );
        }
    }
    }

    Now you have the framework to handle multiple simultaneous requests. The request from the user to start the process would be to a URL something like:

    http://your.host.name/compute?action=processData

    Then, in a JSP that shows the end user that the system is in the process of working (I called it inProgress.jsp above) you'd have some code that does:

    function doPoll(){
        $.get('compute?action=getResults', function(data) {
            alert(data);  // process results here, may be empty
            setTimeout(doPoll,5000);
        });
    }

    This snippet of code tries to get results every 5 seconds - it polls every 5 seconds. If there are none available you would have to handle that and likely do nothing. If there are results then you show them the way you do now.

    This isn't too bad to implement but there are a few moving parts. I've also left out error checking and clean up (what if a computation is started but then the user closes their browser? The result is taking memory for nothing).

    Does this make any more sense?
    Need Java help? Check out the HotJoe Java Help forums!

  5. #5
    Junior Member
    Join Date
    May 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Servlet request time out error

    Yes sure,Now i am going to implement your way, Thanks stdunbar for describe very clearly.

Similar Threads

  1. Replies: 1
    Last Post: January 13th, 2012, 09:02 AM
  2. unencrypted login request- Appscan error
    By krish in forum Member Introductions
    Replies: 0
    Last Post: January 5th, 2012, 04:26 AM
  3. Use Servlet To Request Network Access
    By bgroenks96 in forum Java Theory & Questions
    Replies: 0
    Last Post: August 1st, 2011, 12:34 AM
  4. JDBC Error in servlet
    By shuklagirish in forum Java Servlet
    Replies: 2
    Last Post: May 30th, 2011, 08:13 AM
  5. Replies: 0
    Last Post: March 9th, 2011, 02:58 AM