Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is this coding practice correct?

 
thejwal pavithran
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34095
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are correct that this is bad code. Servlets are not thread safe. You could have request information leak across users.
 
thejwal pavithran
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34095
337
Eclipse IDE Java VI Editor
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. Or local variables if they are only used in one method.
 
Matthew Brown
Bartender
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 157
1
Android Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
look out for struts form bean design. Good example for you.
 
thejwal pavithran
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64631
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic