my dog learned polymorphism
The moose likes JSP and the fly likes Write sophisticated code? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Write sophisticated code?" Watch "Write sophisticated code?" New topic

Write sophisticated code?

Haritha Gorijavolu

Joined: Feb 18, 2011
Posts: 21

Hi, everyone. There is this code I have written and it's very long(3 pages). It works fine, I have not detected any bugs. But the problem is I feel there is a way to write more sophisticated code. I am posting the code and anyone who has some free time to contribute, please examine the code and tell me where I can improve.
Thanks verymuch for your time. It will be a great help to me.

Background: This code is to be embedded in a JSP.
The code operates on 2 tables in the database. bookcopies and waitinglist.
The code is a module for online library management system(my semester project). We have book reservation facility in our system and the reservations are stored in waitinglist table. We have a bookcopies table which has the attributes: bookid,copynumber,borrower,duedate. I case the copy is not presently borrowed, the borrower and duedate will be null. Waitinglist has attributes like bookid,userid(user who reserved the book),listposition,activepassive,daycount.
activepassive is 1 when the waiter is active. activepassive is 0 when the waiter is passive.

If some thing is not understandable in my code, please ask and I shall reply back.
Here is the code

Thanks again for your valuble suggestions.
Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

please examine the code and tell me where I can improve

Remove scriptlets from your JSP.

[My Blog]
All roads lead to JavaRanch
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 62577

No code in a JSP can be "sophisticated" -- it is no longer acceptable, indeed it is irresponsible, to put code in a JSP at this point. The first step in sophisticating your code is to refactor the code out of the JSP and into a Java class or classes.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
sudhir nim
Ranch Hand

Joined: Aug 29, 2007
Posts: 212

Here's some thoughts on refactoring your code,

- Remove scriptlets from JSP, there should be no java code in JSP, use JSP absolutely for presentation purpose. it just displays data provided by controller
- Move data access code to data access objects - DAO
- Move business logic related code to services
- No data access/business logic code in your servlets/controller
- You controller (or servlet if you are not using any framework) uses services and DAOs and provided data to view (JSP) for display
- Have your domain model and entities instead of writing database table oriented code.

[Servlet tutorial] [Servlet 3.0 Cook Book]
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 42183
Too difficult a question for "beginning". Moving thread.
Stefan Evans

Joined: Jul 06, 2005
Posts: 1350
I'll echo the other comments about scriptlet code.
Java code belongs in a java class - and this is 99% java code.

Feedback on the code itself (ignoring the fact that it is in a JSP)
- This code is potentially open to SQL Injection attack. Instead of building up SQL using string concatenation, you should use a Prepared Statement.


- Instead of opening a database connection directly in your code, you should use a JNDI Datasource to get a connection from a connection pool

- I would not recommend using the JDBC-ODBC bridge driver. Use a proper database (eg Oracle, MSSql, MySQL) with a Type 4 JDBC connector.

- This code appears to always execute every single query in line. I would suggest breaking this up into methods, one action per method. You have already broken them up by comments, so extracting them into methods should be trivial.

- Often you are performing 2-3 seperate queries on seperate tables, where a better approach would be to use a single query and join the tables in the query.

For instance:

Could be written as one query:

- You don't seem to be releasing your database connection, though you seem pretty good about closing resultset/statement. My recommendation would be that you always release these resources in a finally block after a try/catch, so that they do get closed in the event of an exception.

Haritha Gorijavolu

Joined: Feb 18, 2011
Posts: 21

Thanks to you all for those useful responses. And, any more suggestions are welcome. Coderanch rocks!!!

Haritha Gorijavolu

Joined: Feb 18, 2011
Posts: 21

Move business logic related code to services

So, I have to use EJB as services?
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 62577

No, not at all.
Haritha Gorijavolu

Joined: Feb 18, 2011
Posts: 21

I have to use "web services" as services?Can any one suggest some books that would help me build web services through J2EE? I am a beginner.
I agree. Here's the link:
subject: Write sophisticated code?