views:

262

answers:

3

I am interested in creating a web app that uses JSP, Servlets and XML.

At the moment I have the following:

JSP - Form input.

Servlet - Retrieving Form data and sending that data to a java object.

Java object (1) - Converts data into XML file....instantiates java object (2).

Java object (2) - Sends that file to a database.

On the returning side the database will send back another XML file that I will then process using XSLT to display back to the user.

Can I place that XSLT code in the orignial Servlets doPost() method? So my doPost()` method would:

  1. Retrieve user inputted data from the form on my JSP page.

  2. Instantiate a java object to convert that data to XML, in-turn that object will instantiates another object to send the XML file to a database.

  3. Converts the resulting XML file sent from the database and displays it for the user.

Can one servlet doPost() method handle all of this? If not, how would I set up my application and classes to handle this work flow?

Thank you in advance

+1  A: 

All your doPost() method has to do is generate a suitable servlet response (some form of content, and a suitable HTTP response structure). So it can do anything you want (including the above).

However it sounds like your rendering requirement is distinct from your form submission and storage requirement. So I would make your doPost() method delegate to a suitable method for rendering the output. That way you can generate output from stored data separately from submitting data to the database.

Brian Agnew
So, create a separate method in my servlet class to handle the XSLT and pass the results to my doPost() method?
Mark R
That would sound like a reasonable approach
Brian Agnew
+2  A: 

I wouldn't load the XSLT in POST, because every method has to do it.

Read that XSTL in the init method, precompile and cache it. Just make sure that you keep it thread safe.

Once you have the XSLT, you've got to apply it to every XML response, so those steps do belong in POST.

duffymo
+1  A: 

Well, this is not really specific to servlets, but more to Java/OOP (object oriented programming) in general. You can in fact do everything in a single method, even in a main() method. But hundreds or more of lines in a single method isn't really readable, maintainable, reuseable nor testable in long terms. Right now, you're probably just starting with Java and you probably don't need to do anything else than this, but if you ever need to duplicate (almost) the same lines of code, then it's time to refactor. Extract the variables from the duplicate code lines and wrap those lines in a new method which takes those variables as arguments and does a simple one-step task.

In general, you'd like to already split the big task in separate subtasks beforehand, using separate and reuseable classes and methods. In your case, you can for example have a single DAO class for all the DB interaction task, a generic XML helper class to convert Javabeans to XML and vice versa with help of XSL and (maybe) a domain object to manage the input/output processing (conversion/validation/errorhandling/response) and executing actions. Write down in paper how the big picture is to be accomplished in small single tasks. Each task can be often as good done by a single method. Group the methods with the same responsibilities and/or the same shared data in the same class.

To go a step further, for several tasks there may be 3rd party tools available which eases the task. I can think of for example XMLBeans and/or XStream to do the Javabean <--> XML conversion. That would already save a lot of boilerplate code and likely also the XSL step.

That said, duffymo's suggestion to load the XSL only once is a very good one. You don't need to re-execute exactly the same task which isn't dependent on request parameters at all again and again on every request, that's only inefficient.

BalusC