This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes Is this coding practice correct? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Is this coding practice correct?" Watch "Is this coding practice correct?" New topic
Author

Is this coding practice correct?

thejwal pavithran
Ranch Hand

Joined: Feb 11, 2012
Posts: 117
Hi,

I came across some code that uses a servlet to process data input through an html form..the html form had fields named temperature,symptoms,gender etc..(all text boxes)..

The servlet class that processes the data has actually made the values obtained from the form as instance variables like below:


after the user input values are obtained, they are stored into the database...

is this bad code as i suspect? I mean making the input values instance variables of the servlet class? or is this a commonly used method? please tell


on job hunt
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30136
    
150

You are correct that this is bad code. Servlets are not thread safe. You could have request information leak across users.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
thejwal pavithran
Ranch Hand

Joined: Feb 11, 2012
Posts: 117
So ideally there should be a class called Patient with these instance variables right? then this class should be used for creating Patient objects..?
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30136
    
150

Yes. Or local variables if they are only used in one method.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

Agreed. It's never a good idea to have member variables in a servlet (or almost never a good idea - there may be exceptions I can't think of right now). A servlet is intended to be able to service multiple requests at the same time.

To be honest, that doesn't look great code even if it wasn't a servlet. It's generally a good idea to make the scope of variables no greater than necessary, and there's no apparent reason why that class couldn't use local variables.
thejwal pavithran
Ranch Hand

Joined: Feb 11, 2012
Posts: 117
I am actually trying to find out some good piece of code that shows an example of how to use a java bean, along with servlets and storing the bean attributes into a database..anybody knows good resources i can use?
harshvardhan ojha
Ranch Hand

Joined: Jul 26, 2007
Posts: 157
    
    1

look out for struts form bean design. Good example for you.
thejwal pavithran
Ranch Hand

Joined: Feb 11, 2012
Posts: 117
on observing the code more closely.. this was done just to access the input values throughout the servlet class methods without having to do request.getParameter() each time!!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

Still not acceptable. If the data needs to be passed around to various parts of the code, the data should be collected into a bean for passing.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is this coding practice correct?
 
Similar Threads
How to call a servlet from a html
How to insert form-data(.jsp) into MySql through Servlets
jsp:useBean problem
jsp bean doubt
[Struts] How do you handle non-text data entry in your Struts forms?