SSC - Web development Model-View-Controller for Java Servlet Shan He School for Computational Science University of Birmingham Module 06-19321: SSC
Outline Outline of Topics Java Server Pages (JSP) Model-View-Controller (MVC) Java Enterprise applications
Java Server Pages (JSP) What is Java Server Pages JSPs: a Java technology that provides a simplified, fast way to create easily maintain, information-rich, dynamic Web pages based on HTML, XML, or other document types. Viewed as a high-level abstraction of Java servlets: translated into servlets at runtime from the JSP source file Requires a Servlet container, e.g., Tomcat Life cycle of JSP: very similar to Java Servlet: Step 1: JSP Page Translation: Step 2: JSP Page Compilation: Other steps are similar to Java Servlet
Java Server Pages (JSP) What is Java Server Pages JSP Page (.jsp) Translate Servlet source (.java) Compile Servlet class (.class) _jspinit() Response _jspservice() Request _jspdestroy() Servlet container Finalisation and garbage collection
Java Server Pages (JSP) How to write JSP Java Code inside HTML using special JSP tags, called JSP Scriting elements Three categories of JSP Scriting elements: Declaration Tags: define varaibales and methods in JSP, start with <%! and end with %>, e.g., <%! String username = Shan; %> Expression Tags: produce output (a String object) that can be used to display result on JSP, start with <%= and end with %>, e.g., <%=name %> Scriptlets: start with <% and end with %>, e.g., <% Date date = new Date(); %>
Java Server Pages (JSP) JSP vs Java Servlet Differences: Java Servlet is HTML in Java, while JSP is Java in HTML. Servlets run faster compared to JSP since JSP needs to be translated and compiled before execution. More convenient to write JSP to generate HTML When to use JSP: when there is not much data process and manipulation Otherwise, use Model-View-Controller design pattern, where JSP used as View and Servlet as controller
Model-View-Controller (MVC) The problem Many software systems are essentially to retrieve data from a data store, e.g., a database and display it for the user: The user changes the data using an user interface The system stores the updates in the data store Most straightforward design: combines user interface and data store to reduce the amount of coding and to improve application performance Problems: user interface tends to change much more frequently than the data storage system. mixed up the presentation (the user interface) and application logic (data store).
Model-View-Controller (MVC) What is Model-View-Controller (MVC)? MVC: a design pattern for efficiently relating the user interface to underlying data models. Three main components: Model: represents the underlying data structures in a software application and the functions to retrieve, insert, and update the data. Note: No information about the user interface. View: a collection of classes representing the elements in the user interface for the user to see on the screen Controller: classes connecting the model and the view, and is used to communicate between classes in the model and view.
Model-View-Controller (MVC) What is Model-View-Controller (MVC)? User Request Controller Return results Updates Manipulates View Model
Model-View-Controller (MVC) Advantages of MVC Better complexity management: Software separate presentation (view) from application logic (model) Consequently, code is cleaner and easier to understand Enable large teams of developers for parallel development Flexibility: Presentation or user interface can be completely revamped without touching the model Reusability: The same model can used for other application with different user interfaces
Model-View-Controller (MVC) MVC for Java Web Application Model: Plain Old Java Object (POJO) or Java Beans View: Java Server Pages (JSP) Controller: Java Sevlet, which decides: what application logic code to be applied in the model which JSP page is appropriate to present those particular results and forwards the request there A Java Sevlet Controller: handles incoming requests instantiates of model and invokes the correct application logic in the model for the request forwards the results from the model and request from the user to the appropriate view (JSP file) RequestDispatcher You can use request.setattribute or session.setattribute to pass the results to view
Model-View-Controller (MVC) MVC for Java Web Application User Request Controller (Servlet) Return results View (JSP pages) Forward Request/results Instantiate/ invoke Model (POJO/Bean)
Java Enterprise applications What is Enterprise applications? Enterprise applications: large-scale, multi-tiered, scalable, reliable, and secure network applications Three Tier architecture consists of: Presentation Tier: usually consists of clients and components that handle the interaction between clients and the business tier Application Tier (Business/Logic Tier): coordinate application, e.g., its business logic, decisions, calculations and evaluations, moves data between the presentation and data tiers. Data Tier (Enterprise Information Systems (Tier): retrieve and store data.
Data tier Application tier Presentation tier SSC - Communication and Networking Java Enterprise applications Enterprise applications 3-Tier architecture Java Enterprise Application Three Tier Architecture Browser with dynamic web pages Desktop applications Servlet/JSP/JSF componets EJB/POJO JDBC/Hibernate/JDO etc.
Java Enterprise applications Tools for Java Enterprise Application Development Java EE: the one your are learning but need learned more Spring Framework: one of the most popular open source application development framework for developing Java enterprise applications. Consists of many modules to simply development The key concept - Inversion of control: the control flow of a program is inverted, e.g., instead the programmer controls the flow of a program, the external source, e.g., Spring Framework takes control of it. The fundamental functionality - dependency injection: objects do not create other objects on which they rely to do their work. Instead, they get the objects from by a external framework component. The basic idea: to simplify development complexity by loosing couplings/dependencies between classes
Java Enterprise applications Links Java EE vs Spring Spring Tutorial - Eclipse Spring Tutorial - Netbean Spring guides