You are not logged in.

#1 2011-03-25 19:58:01

spookykid
Member
From: Portugal
Registered: 2006-07-23
Posts: 141

Java best practices

Hi all,
I'm developing a web application using java, and I'm having problems deciding the best way to go. My application uses MySQL as it's DB.
1. I have a DB schema,
2. I have StoredProcedures for SELECT, UPDATE, etc...
3. All DB tables are mapped to java classes with all attributes maped as properties inside the Class,
4. I have a DBConnectionManager class responsible for creating and closing DB connections
5. Inside all mapped java classes I have methods to call the StoredProcedures.

I've created a DB Layer (java class to handle connections), a Business Layer (java classes mapped from DB tables) and a Presentation Layer (JSP pages). I think this is the right way to go.
Now, my problem is in order to get all values returned from a SELECT Stored Procedure into a Table on a JSP page I'm using this:

<%@page import="com.saladorock.business_layer.Users"%>
<%Users user = new Users();%>
<%user.selectUser(0);%>

<table border="1">
    <tr>
        <td>User login</td>
        <td>User password </td>
        <td>User name</td>
        <td>User contact</td>
        <td>User role</td>
    </tr>
    <tr>
        <td><%out.println(user.getUserLogin());%>&nbsp;</td>
        <td><%out.println(user.getUserPassword());%>&nbsp;</td>
        <td><%out.println(user.getUserName());%>&nbsp;</td>
        <td><%out.println(user.getUserContact());%>&nbsp;</td>
        <td><%out.println(user.getUserRole());%>&nbsp;</td>
    </tr>
</table>

Is this OK? I mean it works, but I'm trying to follow best practices for web development.

Thankx,
spookykid


There is no knowledge that is not power!

Offline

#2 2011-03-31 10:01:28

Yob
Member
From: Warsaw, Poland
Registered: 2011-03-30
Posts: 6
Website

Re: Java best practices

Do you use some ORM framework or map tables to classes on your own? If you do this on your own I highly recommend you using e.g. Hibernate or sth like that. What is more Spring provides great support to JDBC/Hibernate transactions. Maybe you'll be interested in this: http://static.springsource.org/spring/d … -tier.html

Offline

#3 2011-03-31 14:27:44

Inxsible
Forum Fellow
From: Chicago
Registered: 2008-06-09
Posts: 9,183

Re: Java best practices

What was your reasoning to create Stored Procedures? They are great for certain conditions when you want to have pre-compiled queries to run -- especially complex queries joining on multiple tables or even schema. But they might be an overkill for simple select or updates. Simply use JDBC for it. Usage of ORM as suggested by Yob is also a good option, but I think Hibernate is a bit too heavy. iBatis or MyBatis might be a good lightweight alternative. Using an ORM would allow you to change your database from MySQL to sqlite if you had to and your code wouldn't be affected at all (except for changing the driver in iBatis config from a MySQL driver to sqlite driver.

Try to use jsp tag libraries instead of putting a lot of scriptlets in your JSP. although easier, it becomes a maintenance nightmare after a while. I put all my java code in a java class. Debugging becomes easier that way.
Also make sure you use some kind of transaction management (if required) on your updates and inserts. If you are using spring, it has a good transaction manager. Simply define and aop pointcut and inject a transaction manager at that pointcut.

If you don't want to use Spring, you can also create a transaction manager programatically.


Forum Rules

There's no such thing as a stupid question, but there sure are a lot of inquisitive idiots !

Offline

#4 2011-05-04 21:18:39

spookykid
Member
From: Portugal
Registered: 2006-07-23
Posts: 141

Re: Java best practices

Thank you for your replies, after searching a little bit I decided to user Hibernate seems perfect for what I need. I was only using SP because I was using then in .NET but with Java I have a new world to explore, I've just recently found out about Spring, Struts2, Hibernate, SWING, Maven, etc... So I'll need some time to assimilate that all.

thankx,
spook


There is no knowledge that is not power!

Offline

#5 2011-05-09 19:37:20

Stolas
Member
Registered: 2011-05-09
Posts: 16

Re: Java best practices

You'll need to filter the results from the user class.
Since now the application is phone to XSS attacks.
What if I was called: <script>alert(NastyJS);</script>

-- Stolas

Offline

#6 2011-05-09 20:12:21

eDio
Member
From: Ukraine, Kyiv
Registered: 2008-12-02
Posts: 422

Re: Java best practices

The first thing youd better to do is to get rid of scriptlets and use beans to pass some info to page.

<jsp:useBean id="user" class="fully.qualified.UserBean" scope="request" />
<table>
    <tr>
        <td><jsp:getProperty name="user" property="name" /></td>
        <td><jsp:getProperty name="user" property="password" /></td>
        <td><jsp:getProperty name="user" property="whatever" /></td>
    </tr>
</table>

EDIT: to pass bean to page use plain servlet

request.setAttribute("user", userController.selectUser(0));
RequestDispatcher rd = request.getRequestDispatcher();
rd.forward("pathToJspPage");

(code has been written right in browser, so it may contain syntax errors, etc.)

Last edited by eDio (2011-05-09 20:19:44)

Offline

Board footer

Powered by FluxBB