File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Dynamically Loading a XML file when the application starts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Dynamically Loading a XML file when the application starts" Watch "Dynamically Loading a XML file when the application starts" New topic
Author

Dynamically Loading a XML file when the application starts

Matt Thomassan
Ranch Hand

Joined: Dec 23, 2008
Posts: 44
Hello Ranchers,

I am trying to dynamically generate an XML file and loading it when the application starts using PL/SQL . The application uses Tomcat server.

The application uses an abc.xml file. This xml file, currently, is a static file.

I want to:
1) Dynamically generate the above XML file using some information in database.
2)Load the dynamically generated xml file when the application starts.

Point 1 is not a problem. I have problems in implementing point 2.

Can any one tell me how can I load this dynamically generated XML file when the application starts?


Cheers,
Matt
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61653
    
  67

Nothing Tomcat-specific here; moved to the Servlets forum.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61653
    
  67

Typically, startup actions are initiated using a context listener.
Matt Thomassan
Ranch Hand

Joined: Dec 23, 2008
Posts: 44
Hi Bear,

Thanks for replying.

Can you please elaborate on dynamically loading the xml file with the help of context listener?

Any links or pointers will be appreciated.

Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61653
    
  67

A context listener has methods that trigger when an app is started and when it is stopped.

You can perform whatever action you like within these methods.
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
... and you can use any XML parsing API (JDOM, Castor, JAXB, DOM, SAX, XMLBeans etc) to "load" an XML file (unmarshalling (XML -> Java object)).

Matt Thomassan
Ranch Hand

Joined: Dec 23, 2008
Posts: 44
@ Bear

I need some guidance in dynamically loading a file.

I created a small test web application that only has index.jsp. I have not created index.jsp directly. Instead, I have written a class that implements ServletContextListener interface. The contextInitialized method creates (using BufferedWriter) the index.jsp file.

When I start tomcat, it creates the index.jsp file in my C (C:\Eclipse WorkSpace\Test1\WebContent) drive (I cannot see the addition of index.jsp in eclipse yet). But when I hit index.jsp from the browser it says file not found. If I go back and refresh the eclipse project (now I can see the index.jsp file in eclipse ide), and then hit index.jsp from the browser; it displays the jsp page.

I think the problem is index.jsp page is created after the application is deployed. That may be the reason, tom cat cannot display the index.jsp page at first time. Then if I go and refresh the project in eclipse, then tom cat can display the index page.

I have made context reloadable to be true.

How can we ensure that after generating the file through contextInitialized method, Tomcat can immediately render the generated file and display it on the browser?

Please let me know how can we solve this problem?

Thanks
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18907
    
    8

Matt Thomassan wrote:I created a small test web application that only has index.jsp. I have not created index.jsp directly. Instead, I have written a class that implements ServletContextListener interface. The contextInitialized method creates (using BufferedWriter) the index.jsp file.


This doesn't sound like a very useful idea to me. What was your reason for doing that? Instead of something more normal, I mean. Like having the contextInitialized method just set up the data in some kind of beans and having the index.jsp file display that data using JSTL?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dynamically Loading a XML file when the application starts