Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Cannot access jsp page when servlet is running

Posted on 2003-03-06
11
Medium Priority
?
176 Views
Last Modified: 2013-11-24
Hi

 I have a servlet that is started automatically when Tomcat starts.

This servlet starts a class (in the init method) that extends the Thread
class. There i have a method that does zipping of some files.
After that it goes to sleep for some time and continues the same
process. This works fine but when i try to connect to the main jsp page
it says "unable to find localohost:8080".

Does someone know why ?

Thanx alot
0
Comment
Question by:b3cf
  • 6
  • 5
11 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 8080386
Does your thread use ALL of the processor time?
0
 

Author Comment

by:b3cf
ID: 8080413
I am not sure. What should i do in the thread.

Thanx
0
 

Author Comment

by:b3cf
ID: 8080443
Looks like it. cpu usage is 100%. I dont have that
thread only 6%. How can solve this problem ?

Thanx
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 35

Expert Comment

by:TimYates
ID: 8080531
Hmmm....can you post your servlet and thread code?
0
 

Author Comment

by:b3cf
ID: 8080572
I just put yield() in the thread code. Now the cpu
usage is 1%. But still cannot access the jsp page.  


Servlet
-------

public class PremadeENCs extends HttpServlet {

   public void init (ServletConfig config) throws ServletException {

          String chartServerHome = "";
          super.init(config);

          try {
               Process p1 = Runtime.getRuntime().exec("cmd.exe /c echo %CHART_HOME%");
               InputStream is1 = p1.getInputStream();
               BufferedReader br1 = new BufferedReader (new InputStreamReader(is1));
               chartServerHome = br1.readLine();
               br1.close();
               is1.close();
          }
          catch (IOException e) {
                 System.out.println("PremadeENCs" + e.getMessage());
          }

          String     dbDriver="sun.jdbc.odbc.JdbcOdbcDriver";
          String     xmlFile = chartServerHome + "\\Support\\Servlet\\ENCZippingArgs.xml";
          long       DELAY = 7200000L; // 60*60*1000*2 every 2 hours 10000L;

          cMakePremadeENCs runner = new cMakePremadeENCs(dbDriver, xmlFile, DELAY);
          runner.run();

   }
}

Thread class
------------

import java.util.*;

public class cMakePremadeENCs extends Thread {

   private String dbDriver;
   private String xmlFile;
   private long DELAY;

   public cMakePremadeENCs(String dbDriver, String xmlFile, long DELAY) {
          super();
          this.dbDriver = dbDriver;
          this.xmlFile = xmlFile;
          this.DELAY = DELAY;
   }

   public void run() {
          while(true) {
               try {
                   doProcess();
                   sleep(this.DELAY);
                   yield();
               }
               catch (InterruptedException ex) {
                      ex.printStackTrace();  // shouldn't happen
               }
          } // while
   }

   public void doProcess() {

          ArrayList  argumentSetsList;
          DbBean db;

          cArgumentSets argumentSets = new cArgumentSets();
          argumentSets.setXMLFile(this.xmlFile);
          argumentSets.createFromXML();

          argumentSetsList = argumentSets.getArgumentSets();

          db = new DbBean();
          db.setDbDriver(this.dbDriver);

          for (int idx=0; idx <= 4; idx++) {
               cZipping zipping = new cZipping(db);
               cArgumentSet argumentSet = new cArgumentSet();
               argumentSet = (cArgumentSet)argumentSetsList.get(idx);
               zipping.makeConnection(argumentSet);
               zipping.setEncRoot();
               zipping.mainProcess(argumentSet);
               zipping.closeConnection();
          } // for

   }
}
0
 
LVL 35

Accepted Solution

by:
TimYates earned 200 total points
ID: 8080622
Try changing your thread code to:

public class cMakePremadeENCs implements Runnable
{
  private volatile Thread runner ;
  private String dbDriver;
  private String xmlFile;
  private long DELAY;

  public cMakePremadeENCs(String dbDriver, String xmlFile, long DELAY)
  {
    this.dbDriver = dbDriver;
    this.xmlFile = xmlFile;
    this.DELAY = DELAY;
  }

  public void start()
  {
    if( runner == null )
    {
      runner = new Thread( this ) ;
      runner.start() ;
    }
  }

  public void run()
  {
    while( runner != null )
    {
      try
      {
        doProcess();
        sleep(this.DELAY);
        yield();
      }
      catch (InterruptedException ex)
      {
        ex.printStackTrace();  // shouldn't happen
      }
    } // while
  }

  public void stop()
  {
    runner = null ;
  }

  private void doProcess()
  {
    ArrayList  argumentSetsList;
    DbBean db;

    cArgumentSets argumentSets = new cArgumentSets();
    argumentSets.setXMLFile(this.xmlFile);
    argumentSets.createFromXML();

    argumentSetsList = argumentSets.getArgumentSets();

    db = new DbBean();
    db.setDbDriver(this.dbDriver);

    for (int idx=0; idx <= 4; idx++)
    {
      System.out.println( "Doing " + idx ) ;
      cZipping zipping = new cZipping(db);
      cArgumentSet argumentSet = new cArgumentSet();
      argumentSet = (cArgumentSet)argumentSetsList.get(idx);
      zipping.makeConnection(argumentSet);
      zipping.setEncRoot();
      zipping.mainProcess(argumentSet);
      zipping.closeConnection();
    } // for
  }
}
0
 
LVL 35

Expert Comment

by:TimYates
ID: 8080625
it *might* help....

And it will show if it is running :-)
0
 

Author Comment

by:b3cf
ID: 8081088
Thanx alot Tim. It works fine.

But why ?

I set the  priority to 1. (lowest ?)
 runner.setPriority(1);

But when the thread is running i cannot run some things.
Any idea ?

Thanx alot. Appreciate alot your help.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 8081208
Not 100% sure why my one works, and yours doesn't...

I know that you should only extend Thread if you want to alter it's behaviour from the default -- but that's more of an Object Oriented design pattern than a hard and fast rule...

Maybe Tomcat was getting locked at the end of the init() method, as it was waiting for the Thread to end...

------------

> I set the  priority to 1. (lowest ?)
> runner.setPriority(1);

I can never remember which is min...

So I always use:

runner.setPriority( Thread.MIN_PRIORITY ) ;

If stuff still doesn't really work when it is running, I guess it is the fault of the DBBean or cZipping classes being too processor hungry :-(

Maybe the odd Thread.yield() in their most busy sections would ease the pain a little?

Glad I could help :-)

Even if I couldn't really explain ;-)

Tim
0
 

Author Comment

by:b3cf
ID: 8081520
Thanx alot again Time.
Have a nice day.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 8081581
:-)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will learn how to implement Singleton Design Pattern in Java.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month13 days, 9 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question