• Post Reply Bookmark Topic Watch Topic
  • New Topic

Implementing Thread in a JSP custom tag

 
Sanjay Mohanty
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I am writing a JSP tag that will basically execute certain tasks and update the status in JSP page after completion of each task.

Lets take below example -

I want to verify different softwares like Tomcat/MySql/Java... before doing some operation. In my index.jsp page when I will click on button "Start Verification" at back end the custom tag will keep on verifying the softwares and display the status in same jsp page (which is already loaded)

Below is my tag support class - when its executing below highlighted line, throw error - Stream is closed.

Is there any way to create a separate thread in JSP custom tag ?

@Override
public void doTag() throws JspException
{
try
{
Thread thread = new Thread(new RecoveryLoggerThread(),"RecoveryLoggerThread");
thread.start();
}
catch (Exception e)
{
e.printStackTrace();
}

//return SKIP_BODY;
}

class RecoveryLoggerThread implements Runnable
{

@Override
public void run()
{
try
{
PageContext pageContext = (PageContext) getJspContext();
JspWriter out = pageContext.getOut();

out.println(getLogData("Tomcat", false) +"\t\t");

out.println("OK");

}
catch (Exception e)
{

e.printStackTrace();
}

}

}



 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65520
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sanjay Mohanty wrote:I am writing a JSP tag that will basically execute certain tasks and update the status in JSP page after completion of each task.

That's not going to happen. It's just not the way that JSP or even HTTP works. The response is closed once the JSP executes and the result is sent as the response; you cannot update it later. You need a different approach.

If you want to periodically perform an operation and display results in a loaded page, you probably want to use Ajax.
 
Sanjay Mohanty
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot.

I will try Ajax option.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!