?
Solved

Cannot access jsp page when servlet is running

Posted on 2003-03-06
11
Medium Priority
?
170 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Linux Academy Android App Now Supports Chromecast

We have some fantastic news for our Android fans. We’re so excited to announce that the Linux Academy Android app is now available with Chromecast support. That’s right – simply download the latest update of the Linux Academy App and start casting your favorite course videos!

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month10 days, 4 hours left to enroll

762 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