How to execute servlet code contained in the doGet method from run() method.
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   la_du
Posted On:   Wednesday, November 21, 2001 08:26 AM

I need to execute my servlet on schedule, so I created a thread in the init method. In the run() method I want to execute my servlet code which is contained in the doGet method. Is there another way to do this or how can I accomplish this task. I do not have access to response /request in the run() method. Here is my code: import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; import sun.net.smtp.SmtpClient; import java.sql.*; public class InitMsgServlet extends HttpServlet implements Runnable{ static Thread t = null; public void init   More>>

I need to execute my servlet on schedule, so I created a thread in the init method.
In the run() method I want to execute my servlet code which is contained in the doGet method.
Is there another way to do this or how can I accomplish this task. I do not have access to
response /request in the run() method.
Here is my code:

import javax.servlet.*;

import javax.servlet.http.*;


import java.io.*;


import java.util.*;


import sun.net.smtp.SmtpClient;


import java.sql.*;



public class InitMsgServlet extends HttpServlet implements Runnable{



static Thread t = null;



public void init(ServletConfig config) throws ServletException{


super.init(config);


if (t==null){


t = new Thread(this);


t.start();


}


}





Connection con = null;



public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{


response.setContentType("text/plain");


PrintWriter out = response.getWriter();



try{


// Establish connection with database


Class.forName(jdbcclass).newInstance();


con = DriverManager.getConnection(url, user, pass);



//Create and execute SQL statement


Statement statement = con.createStatement();





ResultSet rs = statement.executeQuery("SELECT * FROM " + table +
" WHERE flag = " + "'" + 0 + "'");



while(rs.next()){


// Send Email Code


// Update Database Code


}



}// End Try Block


catch(SQLException sqle){


System.out.println

(sqle.getMessage());


}



catch(Exception e){


System.out.println

(e.getMessage());


}


finally{


try{


if(con !=null)


con.close();


}


catch(SQLException sqle){


System.err.println

(sqle.getMessage());


}


}//End Finally Block



}



public void run(){


HttpServletRequest request = null;


HttpServletResponse response = null;



while (true){


try{


Thread.sleep(5000);


}


catch (InterruptedException ie){


ie.printStackTrace();


}





try{


// Perform Processing Here


doGet(request, response);


}


catch (Exception e){


System.out.println(e.getMessage());


}


}// End While


}// End run



public void destroy(){


t.destroy();


}


}

   <<Less

Re: How to execute servlet code contained in the doGet method from run() method.

Posted By:   Florin_Rapan  
Posted On:   Monday, February 4, 2002 12:30 PM

In your doGet(request, response) method you are not using the "request" and "response" object references at all. You are instantiating a PrintWriter "out" object, but I do not see it been used anywhere in the code. The calls to System.out.println
will work fine
You can easily send email and update your database when the thread wakes up by simply not passing references to these objects in your doGet().

Re: How to execute servlet code contained in the doGet method from run() method.

Posted By:   Alex_Chaffee  
Posted On:   Friday, November 23, 2001 01:59 PM

try searching the servlets FAQ. There's a question there on how to execute a servlet periodically. The code you posted looks like it should work OK, though. (Except you'll get a NullPointerException calling response.getWriter() -- compare it to null first).
About | Sitemap | Contact