Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Calling JSP from a servlet  RSS feed

 
Anup Khanvilkar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,
I am calling a jsp page from my servlet using the requestdispatcher.forward(myjsp.jsp) method. myjsp.jsp is creating a new thread which is parallely processing along with the servlet. The issue is that the jsp page is not displayed until the servlet finishes its execution, although the new thread is created by jsp and is executing in parallel.
How do we have the jsp page displayed even when the servlet is executing.

Thanks,
Anup
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

For starters, don't ever start a thread in a JSP. JSPs are for generating the view of a web app, but not for processing, I/O, DB access, or anything else.

The design sounds odd. Please tell us what the underlying problem is that you're trying to solve. Why do you think you need to start a new thread, and why do you think the JSP would be the right place to do so? (I already said it's not, but I'd like to understand the issue.)
 
Anup Khanvilkar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ulf,
I want to keep the program invocation executed by the JSP page (thread) separate from the servlet.
My JSP is displaying the variable information which is modified inside the servlet or the variables are auto incremented in the jsp. This does not need the jsp to be always called from the servlet. If I want to invoke the thread by just running the jsp I should be able to do so as well.

Issue:
Everytime I trigger the jsp from the servlet, first thing the servlet does is to forward the request to the jsp. The jsp shows the variable information. The servlet which is running in a different thread can modify this data.
As per the logging in place, the thread is getting spawned, the variables are getting auto incremented, the servlet can even alter those variables and I can see the altered variables values logged.

But, the jsp page does not displays until the entire servlet finishes itself.

Thanks,
Anup



 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I want to invoke the thread by just running the jsp I should be able to do so as well.

Actually, no. JSPs are the "V" in "MVC" - they are absolutely the wrong place to start threads.

Everytime I trigger the jsp from the servlet, first thing the servlet does is to forward the request to the jsp. The jsp shows the variable information. The servlet which is running in a different thread can modify this data.

Forwarding a request to the JSP should be the last thing the servlet does, not the first thing, after all data modification has been done. And starting a thread would be the wrong approach even for the servlet to do, because the servlet is not going to wait until a thread it spawns finishes. (And if it did, what would be the point of creating a different thread?)
 
Anup Khanvilkar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  • Forwarding a request to the JSP should be the last thing the servlet does, not the first thing, after all data modification has been done. And starting a thread would be the wrong approach even for the servlet to do, because the servlet is not going to wait until a thread it spawns finishes. (And if it did, what would be the point of creating a different thread?)


  • What is the issue in forwarding the request to the JSP before the servlet is executed?
    In my scenario, JSP thread needs to run parallely to the servlet application. As mentioned earlier, it displays the data which my servlet uses/modifies.
    Consider an example of a simple progress bar which would run in parallel with a servlet application, and which would show the progress made by servlet over time.
    It is necessary that the request is forwarded to the JSP at the start as it is necessary that the user gets to view the original data before modification by the servlet, and the data as the servlet would modify it while processing.


    We leave the point of invoking a new thread from another jsp for now, as the issue is persistent even though we invoke it from the servlet.

    Thanks,
    Anup
     
    Paul Clapham
    Sheriff
    Posts: 22374
    42
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    No, that isn't going to work. Data is sent from the JSP to the client sequentially, so once the JSP has sent the data it isn't possible for the servlet to subsequently tell the JSP to modify that data. So servlet-driven progress bars are not practical.
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Posts: 66142
    141
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I still have no idea what you are trying to accomplish with this --just that whatever it is is bound for failure using this approach.

    If you need to update the data in the JSP, you'll need to use something like Ajax to get updated data from the server.
     
    Anup Khanvilkar
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hello ,
    Below is my requirement.
    The servlet is getting invoked from another application. The servlet processes the request, which is a time consuming task. Now suppose I wish to display the progress of the execution of this servlet, how would we accomplish this?

    Earlier design which I prototyped was as follows:
    I was using a simple jsp progress bar, which would run as a thread parallel to the servlet thread. The progress bar thread would invoke at the start of the servlet (which it did) and would show me the progress correctly in logs.
    The servlet would modify the progress bar variables as per the actual progress of the servlet.
    However, I found that the jsp is only visible only after the servlet finishes its execution, although the progress bar thread runs well in parallel. Thus the display a user gets is a completed action even after we induce sleep in the servlet thread.

    Your suggestions, comments are welcome.

    Thanks,
    Anup
     
    Paul Clapham
    Sheriff
    Posts: 22374
    42
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Your first request starts the long-running process, which immediately returns a simple page which says something like "Processing..." and then continues with the process.

    That simple page then repeatedly sends requests to another process which gathers progress information about the long-running process and returns that progress information. As Bear said, you could use Ajax to send those requests and update the page with the progress information which is returned.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!