Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Struts: instantiate model class on startup using context parameters?

Status
Not open for further replies.

pexin

Programmer
Jul 26, 2005
3
0
0
NL
Hi everyone,

I'm developing an app in Struts, that needs to connect to a Domino database. For this purpose I wrote a dominoRetriever class, which is naturally a part of my model. It's a singleton, which is used by everything that needs it.

The parameters it needs (like host ip, server name, etc) are [highlight]hard coded[/highlight], which is why I'm writing to you. I'd like to place these parameters somewhere else (like web.xml), so they can be edited without desturbing the code.

So my wish comes to this:
When I start the app, I want DominoRetriever object to be created with parameters fetched from "somewhere".

I don't know how to order the instantiation of this class, on startup.
In a normal HttpServlet I'd say:
Code:
String server = getServletContext().getInitParameter("serverName");
...and then I'd instantiate the class, like:
Code:
DominoRetriever = new DominoRetriever(server);

and then I'd leave this servlet and never come back.

But in Struts I can't touch the Controller, can I? (And I certanly have to come back.)

Many thanx,
p



 
Hi,

You need to write a custom class which extend ActionServlet in struts and initilize class in web.xml insted of ActionServlet.

Ex: YourActionServlet.java
Code:
public class YourActionServlet extends ActionServlet {
    
    protected void initOther() throws ServletException {
        super.initOther();
        initDominoServer();
    }
  
   public void initDominoServer(){
    // to access the init parameter from web.xml
    String dbIP = getServletContext().getInitParameter("dbIp");
    // write up your code to initilize the server
   }
}

And in web.xml you can Initilize the sturts application like this
Code:
 <context-param>
    <param-name>dbIp</param-name>
    <param-value>127.0.0.1</param-value>
  </context-param>
<servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>YourActionServlet</servlet-class>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

Hope this will help you.
Cheers
Venu
 
Thank you for your effort, Venu(r).

I solved it a little bit different. I created something like
well known "ENTER"-page. There you can choose whether you'd
like to log-on or continue as guest. Whatever you choose,
I've got me a click. :)

But, to come back to your answer, when I'm done with init(), who controles the rest of the application? My extended controler or the struts-thing?

thanx for help,

p
 
Hi,

To answer it, once the init() is executed the struts will take care of the rest of the initilizations and controller of your application.

Cheers
Venu
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top