• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

questions on reading XML file from a Servlet

 
Harry Wordsworth
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hello all,

I need some advice. I am running eclipse and doing a simple learning project.
Basically I want to use a servlet to read in a small XML file and write out it's output in JSP but I have a few questions like...

____________________________
a) where should the xml file be read in inside the servlet?
should it be read inside the constuctor? or inside the doPost/doGet method?

what I want to have is the first time the servlet is invoked to have the servlet read the XML file and then save a small part of the information (perhaps a node) in the XML to an array, then redirect to a JSP page which will have access to this array and display the information on the JSP page.

Then, the user will click on a button on the jsp page which will go back to the servlet, which will read in the next node from the XML and once again fill the array and redirect to the same JSP page which will now display this new information.

This will go on for a few more times until each node in the small XML file has been read.

So with that as the goal where should I actually place the code to read the XML file initially?

____________________________
the second question...
b) where should I place the XML file in eclipse to actually read it in.

In eclipse I see the J2EE project structure as...

MyProject
---- /Java Resources
---- ---- /src
---- /build
---- /WebContent
---- ---- /WEB-INF

I tried dropping the "input.xml" file I am using directly into the MyProject folder but that does not seem to work

I did create a basic java application to read an xml file as a test and it seemed to work. This is a snippet of the code I used and was trying to use in my servlet...





 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harry Wordsworth wrote:
should it be read inside the constructor?

No, because no servlet should ever have a constructor. If you need to perform initialization, implement an init() method.

or inside the doPost/doGet method?

Depends. Is it likely to change? Or do you just need its information at init time and then it's static?

what I want to have is the first time the servlet is invoked to have the servlet read the XML file and then save a small part of the information (perhaps a node) in the XML to an array, then redirect to a JSP page which will have access to this array and display the information on the JSP page.

If the information doesn't change, perhaps placing it in application scope at app startup makes more sense? But we don't know much about the nature of the data. Will it change while the app is deployed?

Then, the user will click on a button on the jsp page which will go back to the servlet, which will read in the next node from the XML and once again fill the array and redirect to the same JSP page which will now display this new information.

That doesn't make much sense. Trying to read bits and pieces of the XML over multiple requests is a tar pit of pain and agony. And what about multiple people hitting the servlet at the same time?

You should read the entire XML into Java constructs at once and deal with it from there.


b) where should I place the XML file in eclipse to actually read it in.

The IDE is irrelevant -- stop thinking that it is. Eclipse is just a dev tool. You want to think in terms of the structure of the web app and deployment outside of eclipse.

If you don't want the XML file to be accessible directly, put it somewhere under WEB-INF.

And don't use File I/O. Research the servlet's getResourceAsStream() approach.
 
Harry Wordsworth
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The information in the XML file is static. It will not change.
So with that in mind, perhaps using an init method to read the entire XML file into a java construct is best and then just send parts of this information to the JSP page as requested.

Basically the flow should now look like...
-servlet is initialized and XML file is read in to construct
-first node of XML is sent to a JSP page
-JSP page invoked and displays the first node's content on the web page
-flow goes to Servlet again
-servlet gets the second node of the XML from the construct
-second node of XML is sent to the JSP page
-JSP page invoked and displays the second node's content on the web page
-flow goes to Servlet again

...this goes on for about 10 times

the xml file is very simple. A root node with about 10 children and each of those children has 6 children of also.
So for each of the 10 times through I just want to grab the 6 corresponding children, pass that information to the JSP and display.
The information in the xml file will not change.

>>>> "And don't use File I/O. Research the servlet's getResourceAsStream() approach."

ok thanks. I will need to look into getResourceAsStream()
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harry Wordsworth wrote:The information in the XML file is static. It will not change.
So with that in mind, perhaps using an init method

If the information is static, it'd likely be best to read it in with a context listener, and put it application scope, where the servlets and JSP can readily access it.

For each user, it sounds like you need to maintain a pointer into a list of values. That's something you'd likely want to track in session scope.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic