• Post Reply Bookmark Topic Watch Topic
  • New Topic

experts - please comment on a design issue  RSS feed

Sathvathsan Sampath
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear experts,
I am doing a small mini-proj on servlet develpoment. The proj outline is below.
The 2 major components in my mini proj are:
1) User Iterface: A simple html form is displayed when the user types http://localhost:7001/wip. This is implemented in doGet method of the servlet.
a) On the form-level I perform simple validation (viz. ensure there are no empty fields). I am using javascript for doing this.

2)Server Side service: The servlet uses a cached database connection (using WLS) to fetch data from the database. It then formats the data into a html & sends it back to the client.
Now, at this point I have a question:
-{/b] When I write the code for doPost() I find myself doing the database activity i.e. generating sql & fetching data from database as well as formatting the data returned into HTML. Doing both in the same method gives a very shabby feel of doing things coz I am doing both DB activity as well the presentation at the same place (esp all those HTML code makes the code looks worse). In fact, they seem to be totally 2 different entites that could be dealt serperately. I feel that would be better.
Also, if I could break my proj as sep entities (viz. one entity could take care of doing a DB work, other the HTML part & etc etc) & be able to call them from doPost then I guess that be easier to develop.
For doing this should I start learning EJB components or is there some other elegant to approach the whole thing.
Please note that I have started devloping using servlets recently. I don't have much idea about EJB. Experts please guide me. Also, I was thinking if I should be using JSP (which I've just read the into chapter). What I am basically asking is for the correct approach & design. PLease help.
Thanks in advance,
William Brogden
Author and all-around good cowpoke
Posts: 13078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mixing a lot of HTML print statements with program logic bothers a lot of people about servlets. However, you don't have to do everything in one gigantic doPost method.
You might find it easier to debug if you put the SQL query construction and data fetch in a separate class. One advantage of this is you can test the logic of this class outside the servlet environment. You can use the JavaBean convention for that class and make life easier if you ever want to do JSP.

author of:
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!