my dog learned polymorphism*
The moose likes Servlets and the fly likes send form as email, procedural vs. OO Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "send form as email, procedural vs. OO" Watch "send form as email, procedural vs. OO" New topic
Author

send form as email, procedural vs. OO

Tim George
Greenhorn

Joined: Jan 09, 2009
Posts: 6
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?

    Thanks
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 37958
        
      22
    Welcome to JavaRanch

    I am afraid that question is too difficult for the beginners' forum . . . moving.
    William Brogden
    Author and all-around good cowpoke
    Rancher

    Joined: Mar 22, 2000
    Posts: 12760
        
        5
    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.

    Bill
    Tim George
    Greenhorn

    Joined: Jan 09, 2009
    Posts: 6
    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.)

    Tim
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: send form as email, procedural vs. OO
     
    Similar Threads
    Chapter 2 (HFSJ) notes , may be useful for anyone
    Service method
    Can we use doGet and doPost methods in a single Servlte?
    How to determine when a form field is submitted if it's changed from the original value?
    Object Persistence (Materialization & Dematerialization)