Solved

Load database look up tables during servlet start up

Posted on 2004-09-25
5
287 Views
Last Modified: 2012-05-05
Hi,

I am new to JSP and Struts. I am building a web application where I have Simple "Search" button which always exists. The user will be able to search for an "application" either by its name or type ect. The "type" should be a combo box. The values in the combo box should be read from the oracle database. When and Where should I load the database lookup tables. There are atleast 5 look up tables that needs to be read at start up.

I was planning on extending "org.apache.struts.action.ActionServlet"  class and override init() method to connect to the database and load these tables and put then in application scope for use in both jsps or actions.  I am using Weblogic 8.1 app server and using its JDBC datasources and connection pool.

Please advice on how to go about this problem.

Thanks
Dasari
0
Comment
Question by:DASARI_R
  • 3
  • 2
5 Comments
 
LVL 21

Expert Comment

by:MogalManic
ID: 12150290
First, putting database access classes/methods in the Struts classes is not a good idea.  It makes your action class hard to test and you will repeat the same DB code in other pages that use the database.  One solution is to create a TransactionGateway:

class TransactionGateway
{
        Connection dbConn;
        public TransactionGateway(String dataSource)
        {
            //Load the connection by looking up the connection URL, username, password, ETC in Map
           //...
        }


       /**
        * Run select statement on current connection and copy the results set in
        * array of Map, where each row is a Map and each column is accessed by
        * field name as key to map
        *
        * @param selectStatement - SQL statement to execute
        * @param statementParams - List of parameters to send to statement
        */        
       public Map[] getSelect(String selectStatement, List statementParams)
       {
           //Code to build statement and copy results to map array
       }
}
0
 
LVL 21

Expert Comment

by:MogalManic
ID: 12150319
To use the class override the ? class like this:
public class SearchAction extends Action
{
  public ActionForward execute(ActionMapping mapping,
                               ActionForm form,
                               HttpServletRequest request,
                               HttpServletResponse response)
      throws Exception
  {
    String searchStr = request.getParameter("Search");
    String keywords=searchStr.split(searchStr);
    if (keywords.length<2)
       throw new Exception("I only support exactly to word searches right now");
    ArrayList keywordList=new ArrayList();
    keyWords.add(keywords[0]);
    keyWords.add(keywords[1]);

    TransactionGateway tMgr=new TransactionGateway(SEARCH_DB);
    Map[] results=tMgr.getSelect("select * from searchDB where keyWord in (?, ?)", keyWords);
    if ((results== null)) {
      return(mapping.findForward("no-Results-Found"));
    } else {
      request.setAttribute("searchResults", results);
      return(mapping.findForward("success"));
    }
  }
}
0
 

Author Comment

by:DASARI_R
ID: 12150570
Thanks for your quick response.

I want to load the tables When the web application is loaded by the server. That was the reason I was planning to extend
"ActionServlet" not the action. I could pass in the database connection parameters as init parameters servlet in the web.xml or read them from a properties file as you suggested. My concern is that Is this the right way to load tables when no actions are called yet.

Thanks
Dasari
0
 
LVL 21

Accepted Solution

by:
MogalManic earned 500 total points
ID: 12150820
That should work, just overload the class and be sure to call super.init() in your servlet.  Something like this

class SearchServlet extends ActionServlet
{

    public void init()
          throws javax.servlet.ServletException
    {
         super.init();
        //...My initialization stuff..
        SearchDB.getInstance().initialize();
     }
}

Then in your web.xml refer to SearchServlet instead of ActionServlet.
0
 

Author Comment

by:DASARI_R
ID: 12150840
Ok
Thank you.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Facing problems with you memory card? Cannot access your memory card? All stored data, images, videos are lost? If these are your questions...than this small article might help you out in retrieving your lost or inaccessible data.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

911 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now