Diese Präsentation wurde erfolgreich gemeldet.
Die SlideShare-Präsentation wird heruntergeladen. ×

Developing Java Web Applications

Nächste SlideShare
Advance Java Topics (J2EE)
Advance Java Topics (J2EE)
Wird geladen in …3

Hier ansehen

1 von 45 Anzeige

Weitere Verwandte Inhalte

Andere mochten auch (20)


Ähnlich wie Developing Java Web Applications (20)


Aktuellste (20)

Developing Java Web Applications

  1. 1. Developing Java Web Applications Dr. Harry Chen CMSC 491S/691S February 11, 2008
  2. 2. Agenda <ul><li>Web application architecture </li></ul><ul><li>Java Servlet technology </li></ul><ul><li>WebWork : building webapps made easy </li></ul><ul><li>Spring : Inversion of Control (IoC) </li></ul><ul><li>Maven : Java project management and build tool </li></ul>
  3. 3. A canonical Web architecture Do you see any technical issues in this architecture? Source: http://www.ibm.com/developerworks/ibm/library/it-booch_web/
  4. 4. Stateless communication <ul><li>Communications between your browser and the Web Server is usually stateless </li></ul><ul><li>“ Cookie” was invented to solve this problem. </li></ul>
  5. 5. Thin client vs. Thick (fat) client <ul><li>Where do you place your business logic? </li></ul><ul><li>Thin client : most of the business logic live on the client side </li></ul><ul><li>Thick client : application states are managed by the server </li></ul>Thin clients live here. Thick clients live here.
  6. 6. How do we create View? <ul><li>Raw data must be processed before it’s presented to the user. </li></ul><ul><li>Users see the View (HTML, PDF, RSS etc.) </li></ul><ul><li>Should you “hardwire” code in the business logic to generate Views? </li></ul>Raw data View (HTML, PDF, etc) How will be the Views be generated?
  7. 7. Overhead in access data <ul><li>A major part of the application implementation consists of accessing and updating the “raw data” – which can be costly. </li></ul><ul><li>Reduce this overhead can speed up your development (i.e., save cost) </li></ul>SQL DB HTML Do I have to call “SELECT”, “INSERT”, “UPDATE” all the time?
  8. 8. Framework <ul><li>Software engineering is like civil engineering: we need framework. </li></ul>Option 2: Use framework Your Goal Option1: Build everything from scratch…
  9. 9. Framework: It’s a juggle out there. <ul><li>http://en.wikipedia.org/wiki/List_of_web_application_frameworks </li></ul>JavaScript Backbase Clean AJAX Dojo Toolkit Echo Ext JQuery Microsoft AJAX Library Mochikit MooTools OpenLink AJAX Toolkit Prototype JavaScript Framework qooxdoo Rialto Toolkit Rico Script.aculo.us SmartClient Spry framework Yahoo! UI Library ASP.NET MonoRail DotNetNuke CSLA ASP.NET MVC Framework Java Apache Cocoon Apache Struts AppFuse Aranea framework Click [ fleXive ] Google Web Toolkit Grails Hamlets ICEfaces IT Mill Toolkit ItsNat JavaServer Faces Java JBoss Seam Makumba OpenLaszlo OpenXava Oracle ADF Reasonable Server Faces RIFE Shale Framework (software) SmartClient Spring Framework Stripes (framework) Tapestry ThinWire WebObjects WebWork Wicket framework ZK Framework ztemplates Perl Catalyst Interchange Maypole Mason PHP Akelos PHP Framework Antares Framework CakePHP Canvas Framework CodeIgniter DIY Framework Drupal epesi FUSE Horde Joomla ! KohanaPHP MODx PHP For Applications PHPOpenbiz PostNuke PRADO Qcodo QPHP Framework Seagull PHP Framework Simplicity PHP framework Symfony ColdFusion ColdBox ColdFusion on Wheels ColdSpring Fusebox Mach-II Model-Glue onTap
  10. 10. Java Servlet technology
  11. 11. Java Servlet technology <ul><li>Servlets are Java code that run in a server application (e.g., Tomcat) and answer client request. </li></ul>Source: http://www.informit.com/articles/article.aspx?p=26920
  12. 12. HelloWorld Servlet Set HTTP Header: Content-Type (HTML) Creates the HTML page content Implementation that handles HTTP GET request Outputs the HTML into the HTTP Response Do you see any technical issues with this implementation? Source: http://www.informit.com/articles/article.aspx?p=26920
  13. 13. JSP (Java Servlet Pages) <ul><li>Technology that allows Java code and certain pre-defined actions to be embedded into static content. </li></ul><ul><li>Java Servlet: </li></ul><ul><ul><li>Write code  compile  deploy </li></ul></ul><ul><li>JSP </li></ul><ul><ul><li>Write code  deploy </li></ul></ul>
  14. 14. HelloWorld JSP Source: http://mainline.brynmawr.edu/~dkumar/JSP/ Use Java as if it’s a scripting language
  15. 15. Open issues in Java Servlet technology <ul><li>No mention of any framework or design pattern for building web applications </li></ul><ul><li>How to handle stateless communication? </li></ul><ul><ul><li>Use Thin or Thick client? </li></ul></ul><ul><ul><li>How to access data? </li></ul></ul><ul><ul><li>How to create Views? </li></ul></ul>
  16. 16. MVC: Model-View-Control <ul><li>Java BluePrints developed a recommended design pattern for building Java Servlet applications </li></ul><ul><li>Goal: help you to design, implement and maintain your Java Web applications </li></ul>http://java.sun.com/blueprints/patterns/MVC-detailed.html
  17. 17. Example: J2EE webapp <ul><li>This is what you want to build. </li></ul>
  18. 18. Example: J2EE webapp (cont.) <ul><li>This is an MVC solution </li></ul>All DB access and update operations are implemented here. All business logic and applications states are stored here. HTML, PDF and whatever goes here. They read and parse data object from the Model
  19. 19. Problems with MVC <ul><li>MVC is only a design pattern – i.e., you still have to write code to follow this specification </li></ul><ul><li>An obvious problem is that everyone has to learn and write their own MVC framework </li></ul>
  20. 20. WebWork
  21. 21. WebWork <ul><li>A Java MVC framework that help developers to minimize code and to be more concentrated on business logic and modeling. </li></ul><ul><li>Less “plumbing” code, more “useful” code. </li></ul>http://www.opensymphony.com/webwork/
  22. 22. WebWork terminology <ul><li>Action : a piece of code that is executed when a URL is visited </li></ul><ul><li>Bean : a data object that holds useful application information </li></ul><ul><li>Views : templates pages for generating outputs for the clients to consume. </li></ul><ul><li>Result : the output created by a template page </li></ul>
  23. 23. WebWork workflow Read: http://www.javaworld.com/javaworld/jw-03-2004/jw-0329-webwork.html
  24. 24. WebWork HelloWorld <ul><li>Goal: Go a URL, the webapp outputs “Hello World!” </li></ul><ul><li>3 Steps </li></ul><ul><ul><li>Create an Action class to handle the request </li></ul></ul><ul><ul><li>Create a View (template) to produce HTML </li></ul></ul><ul><ul><li>Configure Action and View </li></ul></ul>
  25. 25. Action: HelloWorld.java Extends a standard Action superclass. Implement the business logic How to access the Bean (the message)
  26. 26. View: helloworld.jsp Use JSP Tag lib to access our Bean (the message) If you don’t like to use JSP, you have other options: Freemarker and Velocity
  27. 27. WebWork Configuration: HelloWorld <ul><li>Edit xwork.xml to “glue” the Action with the Template view. </li></ul>Define our Action and name it “helloworld” If the action returns “ success ”, then apply the “ hello.jsp ” template
  28. 28. WebWork: HelloWorld Output Read: http://www.javaworld.com/javaworld/jw-10-2005/jw-1010-webwork.html
  29. 29. Spring IoC
  30. 30. Dynamic object instantiation <ul><li>Often your business logic implementation requires runtime configuration </li></ul><ul><ul><li>Customer Relationship App may use a different JDBC connection to access a new customer DB </li></ul></ul><ul><ul><li>Dynamically fine tune how many threads the robot should instantiate for building search index DB </li></ul></ul><ul><ul><li>Configure the username, password and DB url for your JDBC connection. </li></ul></ul>
  31. 31. Inversion of Control (IoC) <ul><li>IoC is a principle that encourage the decoupling of code. </li></ul><ul><ul><li>A.K.A. Dependency Injection. </li></ul></ul>Problem : The use of “MovieFinderImpl” is hardwired into “MovieLister”. Change this logic will require code rewrite and re-compile “MovieFinderImpl” Read: http://martinfowler.com/articles/injection.html
  32. 32. IoC Solution <ul><li>Introduce an intermediate component to handle the assembly of class objects. </li></ul>Solution : If you want to use a different “MovieFinder”, just change how “Assembler” calls <creates>. <creates>
  33. 33. Spring IoC <ul><li>A framework that can be coupled with WebWork to provide Dependency Injection. </li></ul>Spring IoC implements a flexible “Assembler”. Developers can tell this “Assembler” how to perform “<create>” via XML configuration. http://static.springframework.org/spring/docs/2.0.x/reference/beans.html
  34. 34. Spring IoC: HelloWorld in gnizr <ul><li>How HelloWorld Action is instantiated in gnizr’s HelloWorld demo </li></ul>http://code.google.com/p/gnizr/wiki/HelloWorldDemo
  35. 35. Spring IoC: a more complex example (1) The Class object “folderTagListener” is dynamically associated with “bookmarkManager” via a configuration file, not hardwired in the source of “bookmarkManager”. (2) Developers also fine tune the number of WorkerThread to be instantiated by “bookmarkManager” in the same configuration file. (1) (2)
  36. 36. Apache Maven
  37. 37. Jave project management <ul><li>It concerns the management of </li></ul><ul><ul><li>library dependency, </li></ul></ul><ul><ul><li>code building, </li></ul></ul><ul><ul><li>unit testing, </li></ul></ul><ul><ul><li>packaging and </li></ul></ul><ul><ul><li>documentation. </li></ul></ul><ul><li>Why do you think it’s important to consider those issues when building software? </li></ul><ul><li>- Can you relate to what you have experienced in the past (in school or at work)? </li></ul>
  38. 38. Problems I have faced <ul><li>When some JAR library changed, I have to manually download the JAR from the project web site. </li></ul><ul><li>Make sure dependency library are included in my CLASSPATH for building and testing </li></ul><ul><li>Decide the directory layout of my project (where is “src”, “classes”, “resources”). </li></ul><ul><li>Figure out how to write documentation and call “javadoc” in my project. </li></ul>
  39. 39. Apache Maven <ul><li>Maven is an open source Java project management tool that simplifies many of the tedious tasks associated with Java programming. </li></ul><ul><li>Features I love very much: </li></ul><ul><ul><li>Library dependency management </li></ul></ul><ul><ul><li>Build, test, and package </li></ul></ul><ul><ul><li>Pre-defined directory layout for webapp dev. </li></ul></ul>http://maven.apache.org/
  40. 40. POM file <ul><li>In “Make”, we have “Makefile” </li></ul><ul><li>In “Maven”, we have “POM file” (pom.xml) </li></ul><ul><li>In this POM file, you can define </li></ul><ul><ul><li>JAR (version and package names) that should to be included in your build CLASSPATH (dependencies). </li></ul></ul><ul><ul><li>The layout of your project (where do you keep source, .class, other resources and configuration) </li></ul></ul><ul><ul><li>Information for generating documentation </li></ul></ul><ul><ul><li>And many other stuff… </li></ul></ul>
  41. 41. Maven: Create a project
  42. 42. Maven: POM file
  43. 43. Maven: build package
  44. 44. Try Maven in 5 minutes http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html
  45. 45. Summary <ul><li>Building a successful and maintainable Java Web application requires the effective use of software frameworks and development tools. </li></ul><ul><li>WebWork and Spring are excellent frameworks for building scalable and high-quality Java Web applications. </li></ul><ul><li>Apache Maven solves many project management issues that programmers have been struggling for a long time. </li></ul>