This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes Running servlet from bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Running servlet from bean" Watch "Running servlet from bean" New topic
Author

Running servlet from bean

Dilip kumar
Ranch Hand

Joined: Oct 16, 2000
Posts: 360
Hi,
We have
01. Data access class ( bean ) to connect to database, execute query and return recordset.
02. Class ( bean ) which prepares the dynamic SQL statement based on business logic. This class calls Data access class methods to connect to database, prepare statement and execute.
To improve the performance we would like to add database connection pool in Data access class (bean). Since Servlet�s INIT menthod is the best palce to create connection pool we are planning to make Data access class servlet instead of bean.
My question is can we run the servlet from class ( bean ) mentioned in #2. For testing purpose I tried the following code but it�s not working.
import javax.servlet.*;
import javax.servlet.http.*;
class Servlet1 extends HttpServlet {
String message;
public void init(ServletConfig config) throws ServletException {
message = "Done";
}
public void displayMessage () {
System.out.println(message);
}
}
public class ServletTest {
public static void main(String args[]) {
Servlet1 S = new Servlet1();
S.displayMessage();
}
}
Thanks
Dilip
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Dilip,
I think it is the other way round. Write your connectionPool as a Java class and test independently. Now in all your servlets which want to share a connectionPool object, do this logic.
In all servlet's init() check if there is a ConnectionPool obj in servletContext. If it is not there, then create a ConnecitonPool and sponsor to other servlets which are in same web application also by putting in ServletContext.
The same procedure is done in all other servlet's init(). So whichever servlet sees first time , there is no common ConnectionPool in sevlet context, creates a fresh one and delegates to others. From then onwards, others will just use the connection pool. This procedure I used and found it useful.
 
Consider Paul's rocket mass heater.
 
subject: Running servlet from bean
 
Similar Threads
using data base Access
How must i "synchronize" my servlets?
Really in a hurry!
Datasource Connection
Access-Servlet JDBC-ODBC