• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

prepoputlate form

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have managed to get a struts application working on Struts 1.1 / Tomcat 4.1. All is well and good, but trying to get some consistent data loaded into the Application context is driving me to distraction.

I use a JNDI datasource, and have implemented the Plug-In interface (saw this somewhere). On start-up, the servlet doesn't find the JNDI connection (no driver, no URL). If I hit a .do (action mapping) it does populate the application data.

There are problems with this -

1) the entry point to the application is Default.jsp - and this errors unless you have previously been to a .do

2) I think the plug-in methiod is wrong?

Can anyone suggest what I should be doing - I am quiet happy with this stuff usually, but where should I populate the first form in the applicaiton from - should I simply extend HttpServlet, or is there something better?


Many thanks

Andrew
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd suggest you make the entry to your application something like Default.do. Then have this action check to see if the resources you need are already in the application context and if If they aren't, put them there. It can then forward to default.jsp, and all your resources should be properly initialized.
 
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Andrew",

Welcome to JavaRanch. We don't have many rules here, but we do have a naming policy which we try to strictly enforce. Please re-read this document and edit your display name in order to comply. Thanks in advance, and we look forward to seeing you around the Ranch.
 
andrew low
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not sure how this would work over several entry points - usually a JSP application would allow entry to any JSP page (ideally)? Or am I being unreasonable with my expectations. Could I take my usual approach of a standard servlet that is called in the start up process form web.xml?

Also is there a struts way of testing if the page has all the data in beans that it needs to render itself?

Many thanks

Andrew


Originally posted by Merrill Higginson:
I'd suggest you make the entry to your application something like Default.do. Then have this action check to see if the resources you need are already in the application context and if If they aren't, put them there. It can then forward to default.jsp, and all your resources should be properly initialized.


[ March 23, 2005: Message edited by: andrew low ]
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Andrew,

I'm a little unclear about what your question is at this point. In your original question, you asked someone to point out "what I should be doing". In answer to that, I gave you the recognized Struts best practice, which is to always (or nearly always) call an action, rather than calling a jsp directly, and have the action redirect to the jsp. This way, the action can set up the resources the jsp needs.

While you certainly can call one jsp from another, either in the form of a link, or even specifying it in the action attribute of the form, this is not the best practice in Struts.

As to why your original plugin didn't work, I don't know. If you put initialization logic in a class that implements the org.apache.struts.action.PlugIn interface and reference it with a <plug-in> tag in the struts-config.xml, it's init method will get called by the Struts ActionServlet when it initializes. If you are trying to access a datasource that you have defined in struts, it may not work putting the logic here, because the struts ActionServlet may not have initialized the dataSource yet. However, if you just do a JNDI lookup and find the datasource yourself, it should work.

If I'm still not answering your question, please rephrase it or elaborate to give me a better opportunity to understand it.
 
andrew low
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your help Merrill,

The crux of the matter is that I am trying to place some data into the application scope once upon start-up. This is typically to avoid querying the same data over and over. I could refactor so that I always query the data in the action, and find / implement a caching layer to reduce the amount of database traffic, but this is beyond the scope of the project.

(The main reason not to refactor is that there are some large data sets used to create all the options in my forms - these are both multilingual and hierarchical and are cached in the application layer that I am currently attempting to port - these lists are used over and over and over, with little added elsewhere)

Without refactoring, my problem is that I can't actually get to the JNDI datasource before Tomcat has started up - that is, the context isn't available to the startup servlet when it is called as part of the startup process, but is available if it is accessed as a standard servlet when the application is running.

My Plug-In did work, but only at the point I entered an Action (i.e. the first time a servlet is compiled). Therefore the first user would suffer the overhead - ideally I want the data to be cached before the application presents itself to the world.

Finally, is there a cool struts way to check if I have the pre-requisties for a particular page - I guess I can check if the bean is null and redirect, but is there a best practice for this?

I appreciate I am veering around topics here ... but I hope I have been clearer this time.

Many thanks

Andrew

Originally posted by Merrill Higginson:
Andrew,

I'm a little unclear about what your question is at this point. In your original question, you asked someone to point out "what I should be doing". In answer to that, I gave you the recognized Struts best practice, which is to always (or nearly always) call an action, rather than calling a jsp directly, and have the action redirect to the jsp. This way, the action can set up the resources the jsp needs.

While you certainly can call one jsp from another, either in the form of a link, or even specifying it in the action attribute of the form, this is not the best practice in Struts.

As to why your original plugin didn't work, I don't know. If you put initialization logic in a class that implements the org.apache.struts.action.PlugIn interface and reference it with a <plug-in> tag in the struts-config.xml, it's init method will get called by the Struts ActionServlet when it initializes. If you are trying to access a datasource that you have defined in struts, it may not work putting the logic here, because the struts ActionServlet may not have initialized the dataSource yet. However, if you just do a JNDI lookup and find the datasource yourself, it should work.

If I'm still not answering your question, please rephrase it or elaborate to give me a better opportunity to understand it.

 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Andrew,

Thanks for the clarification. I think I understand what is happening now. I'm a bit surprised that the plugin didn't do it's work until the first action was called. It was my understanding that the struts ActionServlet calls your plugin when it performs it's init() method. Normally, the ActionServlet is configured to load on startup. This is something you might check. See if you can find:
<load-on-startup>1</load-on-startup> as a child tag of the <servlet> tag that defines the action servlet in your web.xml file.

If this still doesn't work, you have the option of creating a new servlet with your logic placed in the init method. Then register this servlet with the load-on-startup option set on.

Regarding your question about ways within struts to tell whether you have needed resources or not, I'd look a the <logic:xxx> tags, particularly logic resent/notPresent and logic:empty/notEmpty.

Good luck.
 
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Having the separate servlet is a very good idea.
 
andrew low
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi - the plug-in does get called at start up, but can't get the JNDI context, not until it is called a second time, from the first servlet.

Its very odd.

Cheers

Andrew

Originally posted by Merrill Higginson:
Andrew,

Thanks for the clarification. I think I understand what is happening now. I'm a bit surprised that the plugin didn't do it's work until the first action was called. It was my understanding that the struts ActionServlet calls your plugin when it performs it's init() method. Normally, the ActionServlet is configured to load on startup. This is something you might check. See if you can find:
<load-on-startup>1</load-on-startup> as a child tag of the <servlet> tag that defines the action servlet in your web.xml file.

If this still doesn't work, you have the option of creating a new servlet with your logic placed in the init method. Then register this servlet with the load-on-startup option set on.

Regarding your question about ways within struts to tell whether you have needed resources or not, I'd look a the <logic:xxx> tags, particularly logic resent/notPresent and logic:empty/notEmpty.

Good luck.

 
reply
    Bookmark Topic Watch Topic
  • New Topic