Domain Driven Web Development With WebJinn Sergei Kojarski College of Computer & Information Science Northeastern University joint work with David H. Lorenz
Outline Background Problem Solution Conclusion Future work & Projects
Beginning: Static Pages In the beginning (1991), web was static. People could only browse html pages and no other interaction was allowed….. Resource ClientHTTPWeb-Server Browser Client-server interaction was specified by the HTTP 0.9 protocol, in terms of a simple request/response model…BACKGROUND
What about interaction? Web App Web App ClientHTTPWeb Server Browser 1. HTTP was extended to allow user data in the request string 2. Web Server redirects requests to a server-side application 3. Application reads user data, performs server-side actions and dynamically generates HTTP responseBACKGROUND
Web Application : closer look An HTTP gateway to a server–side resource Converts between HTTP and Resource terms Normally, doesn’t have complex logicBACKGROUND Web App Web App HTTP Server Resource
Web Development Two developer teams: Functionality (e.g., Java, C, Perl) Presentation (e.g., XML, HTML)BACKGROUND
Technology #1: CGI scripts public void service(HttpServletRequest request, HttpServletResponse response) { String message=request.getParameter(“message”); response.setContentType("text/html"); PrintStream rs = new PrintStream(response.getOutputStream()); rs.print(“ ”); if (message != null) rs.print(“ ” + message + “ ”) ; else rs.print(“ No message! ”); rs.print(“ ”); } Presentation (HTML) code is embedded into functionality codeBACKGROUND
Technology #2: Dynamic Pages No message! Cleaner look More friendly to web designers Easier to change Presentation and functionality are still tangled!BACKGROUND
Theory and Practice Well-understood Simple functionality Interactions patterns Difficult to create Complicated implementations No reusable components ConceptState of the artBACKGROUND
Background Problem Solution Conclusion Future work & Projects Outline
Schema HTTP Resource Schema Functionality server-side operations Presentation HTML response Control high-level control-flow Functionality Presentation Control <%try{ Class.forName("org.gjt.mm.mysql.Driver").newInstance(); java.sql.Connection conn; conn = DriverManager.getConnection( "jdbc:mysql://localhost/database?user=login&password=pass"); try { Statement st = conn.createStatement(); ResultSet rs = st.executeQuery("select name, ,message from guestbook");%> Visitor: '> Guest book is empty. <%} } finally {conn.close();} } catch (Exception e) {%> Server error: The Problem: Tangling and Scattering page code example: name message Visitor: PROBLEM PROBLEM
Server error: Server error: Separation of concerns <%try{ Class.forName("org.gjt.mm.mysql.Driver").newInstance(); java.sql.Connection conn; conn = DriverManager.getConnection( "jdbc:mysql://localhost/database?user=login&password=pass"); try { PreparedStatement pst = conn.prepareStatement( (“insert into guestbook (name, ,message) values (?,?,?)”); pst.setString(1,request.getParameter(“name”); pst.setString(2,request.getParameter(“ ”); pst.setString(3,request.getParameter(“message”); pst.execute(); %> Message was successfully added } finally {conn.close();} } catch (Exception e) {%> <%try{ Class.forName("org.gjt.mm.mysql.Driver").newInstance(); java.sql.Connection conn; conn = DriverManager.getConnection( "jdbc:mysql://localhost/database?user=login&password=pass"); try { Statement st = conn.createStatement(); ResultSet rs = st.executeQuery("select name, ,message from guestbook");%> Visitor: '> Guest book is empty. <%} } finally {conn.close();} } catch (Exception e) {%> New Message Your Name Message select.jspdoInsert.jsp insertForm.jsp name message name message pst.setString(1,request.getParameter(“name”); Your Name pst.setString(1,request.getParameter(“ ”); pst.setString(1,request.getParameter(“message”); Message ?,?,? Visitor: Main contribution: schema concern modularization PROBLEM PROBLEM
Example:Guestbook PROBLEM PROBLEM
Example: Guestbook Typical client-server interaction Typical components (select, insert) Typical problems intra-crosscutting inter-crosscutting PROBLEM PROBLEM
Guestbook Architecture Browser guestbook database table SQL queries select.jsp doInsert.jsp insertForm.jsp HTTP PROBLEM PROBLEM Retrieves messages A form for adding a message Posts a new message
Select records from guestbook table Print “Content” Load driver and connect to database If table is not empty then Else Print “No records” Close database connection Catch exception Print “Service not available” select.jsp <%try{ Class.forName("org.gjt.mm.mysql.Driver").newInstance(); java.sql.Connection conn; conn = DriverManager.getConnection( "jdbc:mysql://localhost/database?user=login&password=pass"); try { Statement st = conn.createStatement(); ResultSet rs = st.executeQuery("select name, ,message from guestbook");%> Visitor: '> Guest book is empty. <%} } finally {conn.close();} } catch (Exception e) {%> Server error: page codedescription name message Visitor: select name, , message from … name message href=“ > PROBLEM PROBLEM
Construct and execute SQL insert statement Print “Fail” Print “Message added” doInsert.jsp <%try{ Class.forName("org.gjt.mm.mysql.Driver").newInstance(); java.sql.Connection conn; conn = DriverManager.getConnection( "jdbc:mysql://localhost/database?user=login&password=pass"); try { PreparedStatement pst = conn.prepareStatement( (“insert into guestbook (name, , message) values (?,?,?)”); pst.setString(1,request.getParameter(“name”); pst.setString(2,request.getParameter(“ ”); pst.setString(3,request.getParameter(“message”); pst.execute(); %> Message was successfully added } finally {conn.close();} } catch (Exception e) {%> Server error: Load driver and connect to database Close connection Catch exception page codedescription name message pst.setString(1,request.getParameter(“name”); pst.setString(1,request.getParameter(“ ”); pst.setString(1,request.getParameter(“message”); ?,?,? PROBLEM PROBLEM
insertForm.jsp Return an HTML form that the visitor can fill in order to add a new message into the guestbook. New Message Your Name Message page codedescription Your Name Message PROBLEM PROBLEM
Intra- versus inter-crosscutting Intra Tangling Dynamic pages Page scoped OOP solution Inter Scattering Dynamic and static Crosscuts pages AOP solution All scheme expressions relate to the same instance insertForm.jsp select.jspdoInsert.jsp Illustration New Message Your Name Message <%try{ Class.forName("org.gjt.mm.mysql.Driver").newInstance(); java.sql.Connection conn; conn = DriverManager.getConnection( "jdbc:mysql://localhost/database?user=login&password=pass"); try { Statement st = conn.createStatement(); ResultSet rs = st.executeQuery("select name, ,message from guestbook");%> Visitor: '> Guest book is empty. <%} } finally {conn.close();} } catch (Exception e) {%> Server error: <%try{ Class.forName("org.gjt.mm.mysql.Driver").newInstance(); java.sql.Connection conn; conn = DriverManager.getConnection( "jdbc:mysql://localhost/database?user=login&password=pass"); try { PreparedStatement pst = conn.prepareStatement( (“insert into guestbook (name, ,message) values (?,?,?)”); pst.setString(1,request.getParameter(“name”); pst.setString(2,request.getParameter(“ ”); pst.setString(3,request.getParameter(“message”); pst.execute(); %> Message was successfully added } finally {conn.close();} } catch (Exception e) {%> Server error: Tangling Scattering Dynamic pages Dynamic and static Page scoped Crosscuts pages OOP solution AOP solution PROBLEM PROBLEM
High development cost High maintenance cost Loss of reuse opportunities Increases dependency between developer groups, error-prone, etc. Natural language Correct? Consequence of inter-crosscutting PROBLEM PROBLEM High development cost High maintenance cost Loss of reuse opportunities Changes to the schema trigger complex code updates Cost of schema changes out- weights reuse benefits
Outline Background Problem Solution Conclusion Future work
<%try{ Class.forName("org.gjt.mm.mysql.Driver").newInstance(); java.sql.Connection conn; conn = DriverManager.getConnection( "jdbc:mysql://localhost/database?user=login&password=pass"); try { PreparedStatement pst = conn.prepareStatement( (“insert into guestbook (name, ,message) values (?,?,?)”); pst.setString(1,request.getParameter(“name”); pst.setString(2,request.getParameter(“ ”); pst.setString(3,request.getParameter(“message”); pst.execute(); %> Message was successfully added } finally {conn.close();} } catch (Exception e) {%> Server error: Unweaving inter-crosscutting <%try{ Class.forName("org.gjt.mm.mysql.Driver").newInstance(); java.sql.Connection conn; conn = DriverManager.getConnection( "jdbc:mysql://localhost/database?user=login&password=pass"); try { Statement st = conn.createStatement(); ResultSet rs = st.executeQuery("select name, ,message from guestbook");%> Visitor: '> Guest book is empty. <%} } finally {conn.close();} } catch (Exception e) {%> Server error: New Message Your Name Message name message name message pst.setString(1,request.getParameter(“name”); Your Name pst.setString(1,request.getParameter(“ ”); pst.setString(1,request.getParameter(“message”); Message ?,?,? Visitor: Schema-related expressions are clustered by operation select.jspdoInsert.jsp insertForm.jsp Constructing SQL select statement Constructing SQL insert statement Rendering guest book messages as HTML table Rendering HTML form for inserting a message XP1 XP2 XP4 1. Substitute clusters with schema-shy extension points To unweave inter-crosscutting we… 2. Specify schema-related code in a separate module <%try{ Class.forName("org.gjt.mm.mysql.Driver").newInstance(); java.sql.Connection conn; conn = DriverManager.getConnection( "jdbc:mysql://localhost/database?user=login&password=pass"); try { PreparedStatement pst = conn.prepareStatement( (“insert into guestbook (name, ,message) values (?,?,?)”); pst.setString(1,request.getParameter(“name”); pst.setString(2,request.getParameter(“ ”); pst.setString(3,request.getParameter(“message”); pst.execute(); %> Message was successfully added } finally {conn.close();} } catch (Exception e) {%> Server error: <%try{ Class.forName("org.gjt.mm.mysql.Driver").newInstance(); java.sql.Connection conn; conn = DriverManager.getConnection( "jdbc:mysql://localhost/database?user=login&password=pass"); try { Statement st = conn.createStatement(); ResultSet rs = st.executeQuery("select name, ,message from guestbook");%> Visitor: '> Guest book is empty. <%} } finally {conn.close();} } catch (Exception e) {%> Server error: New Message Your Name Message XP1 XP2 XP3 XP4 Abstract application Schema Weaver XP3 3. Employ weaver to produce executable code XP Model SOLUTION SOLUTION
Unweaving intra-crosscutting Resource Model Controller View HTTP request response Action classes JSP pages Controller servlet Web-application Selects action class to handle request received Performs server-side operations. Deposit results in web-tier containers. Selects appropriate JSP view Obtains data from web-tier containers and renders the HTTP response Structure concern cuts across action classes and JSP views
DDD model SOLUTION SOLUTION DDD = MVC + XP What about having one model that solves both problems?
Solution space SOLUTION SOLUTION ProblemModelFramework Intra- crosscutting MVCApache Struts Inter- crosscutting XPWebJinn/XP Intra- and Inter- crosscutting DDDWebJinn
WebJinn Architecture Resource Model Controller View request response Java classes JSP pages Controller servlet Schema SOLUTION SOLUTION
Benefits of WebJinn Decreased dependency between developer groups Adaptability to schema changes High code reusability. Web components! SOLUTION SOLUTION
Dynamic page DDD evolution Conclusion CONCLUSION CONCLUSION Intra- crosscutting Inter- crosscutting Static page CGI scripts MVC XP
Future Work and Projects Web AOP Project AOP language design Implementation of language semantics Weaver implementation Documentation Web components project Webjinn Component builder (use BeanBox) Eclipse plugin development Related projects: Content Management System.
Related Models & Frameworks Apache Struts Java Server Facets The MVC Model JSP & Servlet technology ASP, PhP and other dynamic page technologies RELATED WORK