I'm modifying a servlet (written by someone else) which converts HTML forms to email. I'm struggling to understand some of the design.
There are 4 methods called in sequence from doPost():
parseData(AllData allData) pushes the HTML form data into the allData object
checkData(AllData allData) looks for invalid strings
sendMail(AllData allData) uses the Java mail library to send the email
returnPage(AllData allData) returnPage returns an HTML 'thankyou' page
The allData object is created during doPost() and stores the HTML form data and configuration attributes from the deployment descriptor.
The questions: The HttpServletResponse, Request and HttpSession are referenced in allData so that they can be accessed by the 4 methods above, this doesn't seem right? Is there a better way to access these objects within the methods?
The way the steps have been split out doesn't appear to provide any advantage except readability, any other justification?
Is there an object oriented approach to sending form data as an email? I can envisage a Email and Form objects, but where would the other parts reside (data validation, return page, etc.)? How would you approach this problem from scratch?
The HttpServletResponse, Request and HttpSession are referenced in allData so that they can be accessed by the 4 methods above, this doesn't seem right? Is there a better way to access these objects within the methods?
In my opinion, you are exactly correct.
These objects are managed by the servlet container and references to them should NEVER be stored by objects that may live longer than a single request/response cycle. Furthermore, objects which are dependent on the servlet container cant be tested outside the servlet environment.
The servlet API has methods to extract the form data as standard collection objects, for example getParameterMap().
There is also a hidden problem here - suppose the mail server is temporarily unavailable!
When I have done this I have created an object to represent the mail to be sent and handed it to a queue used by a separate Thread which occasionally trys to send mail from the queue.
Thanks for moving the post, I really am a Java beginner, but I've been working with servlets for a while on a fairly large application and I'm now going back to learn Java from scratch. I know a little knowledge is a dangerous thing, but I had a good mentor, just not enough time to study.
Removing the data object will really clean things up, I'll check out the Servlet API, thanks!
Good point about the mail server. I haven't a clue how to run a queue in a separate thread; I'll do some research on that myself.
Creating a mail object seemed like an obvious OO thing to do, but where would you do validation of attributes such as email addresses? (By validation I mean making sure they contain an @ etc. Currently in the checkData function.)