DASARI_R
asked on
Load database look up tables during servlet start up
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. ActionServ let" 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
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.
Please advice on how to go about this problem.
Thanks
Dasari
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("Sear ch");
String keywords=searchStr.split(s earchStr);
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("se lect * from searchDB where keyWord in (?, ?)", keyWords);
if ((results== null)) {
return(mapping.findForward ("no-Resul ts-Found") );
} else {
request.setAttribute("sear chResults" , results);
return(mapping.findForward ("success" ));
}
}
}
public class SearchAction extends Action
{
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception
{
String searchStr = request.getParameter("Sear
String keywords=searchStr.split(s
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_
Map[] results=tMgr.getSelect("se
if ((results== null)) {
return(mapping.findForward
} else {
request.setAttribute("sear
return(mapping.findForward
}
}
}
ASKER
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ok
Thank you.
Thank you.
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
}
}