java - How to achieve separation of concerns without using any framework but Tomcat + Servlets? -
i have code works fine. important parts follows:
my model class:
package biz.tugay.sakila.model; /* user: koray@tugay.biz date: 25/06/15 time: 12:48 */ public class actor { private long id; private string firstname; private string lastname; // getters, setters... }
my dao class:
package biz.tugay.sakila.dao; /* user: koray@tugay.biz date: 25/06/15 time: 12:12 */ import biz.tugay.sakila.model.actor; import java.sql.*; import java.util.arraylist; import java.util.list; public class actordao { protected static final connection connection = dbconnector.getconnection(); public list<actor> getallactors() throws sqlexception { list<actor> allactors = new arraylist<actor>(); statement stmt = connection.createstatement(); string sql = "select * actor"; resultset rs = stmt.executequery(sql); while (rs.next()) { actor actor = new actor(); actor.setfirstname(rs.getstring("first_name")); // idea... setters again.. allactors.add(actor); } rs.close(); stmt.close(); return allactors; } }
and dbconnector
package biz.tugay.sakila.dao; /* user: koray@tugay.biz date: 25/06/15 time: 12:35 */ import java.sql.connection; import java.sql.drivermanager; import java.sql.sqlexception; public class dbconnector { static final string jdbc_driver = "com.mysql.jdbc.driver"; static final string db_url = "jdbc:mysql://localhost/sakila"; static final string user = "root"; static final string pass = ""; private static connection connection = null; public static final connection getconnection() { if (connection != null) { return connection; } else { try { class.forname(jdbc_driver); connection = drivermanager.getconnection(db_url, user, pass); return connection; } catch (classnotfoundexception e) { } catch (sqlexception e) { } throw new unsupportedoperationexception(); } } }
my servlet class:
package biz.tugay.sakila.servlet; /* user: koray@tugay.biz date: 26/06/15 time: 14:31 */ import biz.tugay.sakila.dao.actordao; import biz.tugay.sakila.model.actor; import javax.servlet.servletexception; import javax.servlet.annotation.webservlet; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import java.io.ioexception; import java.sql.sqlexception; import java.util.list; @webservlet(urlpatterns = "/actors") public class actorservlet extends httpservlet { @override protected void doget(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception { actordao actordao = new actordao(); list<actor> allactors = null; try { allactors = actordao.getallactors(); req.setattribute("allactors",allactors); req.getrequestdispatcher("/actors.jsp").forward(req, resp); } catch (sqlexception e) { } } }
and /actors.jsp show html table user.
i have made exercise myself sakila sample database mysql provides.
my question is, without using framework such spring or struts, how can achieve better separation? example, actorservlet depends on actordao concretely, can fix this, if how? actordao depends heavily on dbconnector. example, want able create nosql connector , use it, can not guess?
first step abstract out interfaces. example, make actordao
interface, move implementation actordaoimpl
or whatever. create actordaofactory
hands actordao
is, under covers, actordaoimpl
, servlet doesn't need know that.
second step more complex... if want only use tomcat, injection , out, can configure tomcat create these new interfaces , put them in jndi. process complex put in answer here, tomcat documentation on jndi nice. process involves creating factory, advocated above, , having tomcat invoke factory through configuration.
once this, looking them jndi simple as
// obtain our environment naming context context initctx = new initialcontext(); context envctx = (context) initctx.lookup("java:comp/env"); // our dao actordao ad = (actordao)envctx.lookup("dao/actor");
good luck!
Comments
Post a Comment