aspose file tools*
The moose likes Servlets and the fly likes Displaying servlet output immediately Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Displaying servlet output immediately" Watch "Displaying servlet output immediately" New topic
Author

Displaying servlet output immediately

Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
Hello,

I have written a servlet that reading and inserting records into database in shape of loop. When loop finishes then it showing the results that's taking too long to display the results.
I am interested to show the result while the loop is running

Is there a way around?

Thanks & best regards
Hebert Coelho
Ranch Hand

Joined: Jul 14, 2010
Posts: 754

You would need to use Ajax for that.

Is the last query taking to long? Or all the process take too much time?


[uaiHebert.com] [Full WebApplication JSF EJB JPA JAAS with source code to download] One Table Per SubClass [Web/JSF]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18669
    
    8

Have a look at this thread for a related question.
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
Hebert Coelho wrote:You would need to use Ajax for that.

Can you please refer me to a Ajax tutorial/example website where I can overcome this issue

Is the last query taking to long? Or all the process take too much time?

Its a loop that reading files and inserting them into DB that may be one or 10K. Its probably time consuming task

Thanks & best regards
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
Paul Clapham wrote:Have a look at this thread for a related question.

Thanks Paul your post is useful but want to clear some points


The initial request starts a thread running and puts a reference to the thread, or to some proxy for the thread, into the user's session.
Subsequent requests get the thread from the session. If it is still alive, the response says "Still running", otherwise the response says "Finished".

Please correct me if I am wrong that for saving browser timeout when the loop starts I add something in session to prevents page timeout?

Of course you can expand on this basic structure to return the result of the long-running process, or to return the percentage complete if the long-running process can produce that information.

Here its a problem that its showing percentage complete after doing all processes done that I want to fix it

Thanks again and best regards
hemantha kumar k
Greenhorn

Joined: Apr 20, 2010
Posts: 27

how you want to display?
do you want to display as graph? or you want to display the content what you are inserting into the data base?
and most important thing is how much data you want to display?
if you want to display the data while inserting, it is very easy to display the data, you don't have to make a call at all,
when it comes to the fetching the data an you want to display the data then it would take long time to display, to it's all about the how much data you want to display
if your question some more specific, the it'll be easy to give the solution, if it is possible post the servlet code.
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
Thanks tinku for your favorable reply

The user will upload a zip file and my code will:
1) unzip the file
2) Check the file extensions if they are doc,docx or pdf
3) Code will parse file and convert into XML
4) Then XML tags will be converted into variables
5) Here will check from DB either email or Tel exists or not. if exists reject the file otherwise
6) Insert into DB
7) if successfully inserted then display message inserted successfully

all this will run in a loop to read the file and loop may be in thousands (files).

I just want to show the messages of:
1) Already exists
2) Inserted into DB successfully

but my code displays these messages when it finishes all the tasks within loop not print the messages for files one by one

Thanks again
hemantha kumar k
Greenhorn

Joined: Apr 20, 2010
Posts: 27

it seems you are using the same servlet to generate the display view purpose also, this is happening because when the execution of the servlet completes then only we gets the view
it is not possible to get the view from the servlet in the middle of the execution and once again go back to the execution point, all container generates the view after complete execution of the servlet.


Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
tinku rider wrote:it seems you are using the same servlet to generate the display view purpose also, this is happening because when the execution of the servlet completes then only we gets the view
it is not possible to get the view from the servlet in the middle of the execution and once again go back to the execution point, all container generates the view after complete execution of the servlet.


This is what am looking for. Is that could be implemented?

Thanks again
hemantha kumar k
Greenhorn

Joined: Apr 20, 2010
Posts: 27

how you want to generate the view? you have to explain in detail then only i can help you out
in the sense do you want to give the alert box to the user?

this can be implemented using Filters,Ajax and Comet, you have to use the these three component to generate your requirement
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
tinku rider wrote:how you want to generate the view? you have to explain in detail then only i can help you out
in the sense do you want to give the alert box to the user?


Can we generate simple text/html page view instead of alert box?

thanks again for giving me your precious time
hemantha kumar k
Greenhorn

Joined: Apr 20, 2010
Posts: 27

This can be implemented using Filters,Ajax and Comet, you have to use the these three component to generate your requirement.

Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
Can you please refer any example/tutorials that is nearest to my situation?

thanks again
hemantha kumar k
Greenhorn

Joined: Apr 20, 2010
Posts: 27

1. when you want to display the massage save massage in Request Attributes
2. Using the ServletRequestAttributeListener to listen the changes and use Comet push the data to clientside


comet is the technique pushing the data from server side to client side without receiving request from the client side

http://martin.ankerl.com/2007/08/21/ajax-dojo-comet-tutorial/

this is the link about example of coment it won't fully solve your problem. but it solves the part of your problem
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
Tinku rider I am thankful to you from the bottom of my heart. I am studying the both referred docs

Best regards
abani patra
Ranch Hand

Joined: Oct 11, 2011
Posts: 70
Hi,
For showing you the data use navigation so have to read and insert a large data at once.
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
Hello,

sorry for late reply

I tried this example and html page and its working fine. I have written my servlet as follow:

but getting this error:


Thanks again
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

Didn't you solve that yesterday in this thread?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
Thanks Rob Spoor for replying but yesterday was trying to solve another case. By inserting public class now its throwing the following exception


Thanks again
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

The stack trace says the error occurs on line 21. There are two references there: Bayeux b and Channel c. As b has already been successfully used before (on line 16), that means that your Channel is null.
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
Thanks Rob Spoor from the bottom of my heart.

Best regards
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
Rob Spoor wrote:There are two references there: Bayeux b and Channel c. As b has already been successfully used before (on line 16), that means that your Channel is null.

Thanks again for continously helping me



but same NPE:


When I test as html + push servlet then its working fine

Thanks again & best regards
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
May I open as a new question as this is marked resolved?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

It isn't anymore.

Since you don't seem to know how exception stack traces work I'll give you a little lesson.

The stack trace starts with the exception class name and message (if not null). In this case that's java.lang.NullPointerException. Below that is the exact chain of method calls, the last called method first. This is the method that is actually throwing the exception. Behind it is the file name and line number of the source code where the exception came from. In this case that's BulkUpload.doPost(BulkUpload.java:116) - line 116 of file BulkUpload.java. Since this is your own class, you don't need to look any further; you've found the exact line where the problem occurs. Now all you need to do is figure out which variable on that line is null.

Sometimes the top of the stack trace isn't your code but someone else's; sometimes even the core API. This can mean there is a bug in that code, but more often you're calling some method with incorrect arguments. You then move to the second, third, fourth etc line until you hit a line that indicates your code. That's where you've supplied an incorrect arguments.
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
I seen line No. 116 and this line is:

That's why I sorted help with comet by posting my code

Regards
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

c is still null.
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
How can I open Channel manually? this will fix my problem

regards
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Displaying servlet output immediately