SSC - Communication and Networking
SSC - Web development
Model-View-Controller for Java web application
development
Shan He
School for Computational Science University of Birmingham
SSC - Communication and Networking Outline
Outline of Topics
Java Server Pages (JSP)
Model-View-Controller (MVC)
SSC - Communication and Networking Java Server Pages (JSP)
What is Java Server Pages
I
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.”
IViewed as a high-level abstraction of Java servlets: translated
into servlets at runtime from the JSP source file
I
Requires a Servlet container, e.g., Tomcat
I
Life cycle of JSP: very similar to Java Servlet:
I Step 1: JSP Page Translation: Translated as Servlet source code
I Step 2: JSP Page Compilation: Compiled as Servlet class I Other steps are similar to Java Servlet
SSC - Communication and Networking Java Server Pages (JSP)
What is Java Server Pages
_jspInit() JSP Page (.jsp) _jspService() Servlet class (.class) _jspDestroy()
Finalisation and garbage collection Request Response Servlet container Servlet source (.java) Translate Compile
SSC - Communication and Networking Java Server Pages (JSP)
How to write JSP
I
Java Code inside HTML using special JSP tags, called JSP
Scriting elements
I
Three categories of JSP Scriting elements:
I Declaration Tags: define varaibales and methods in JSP, start with <%! and end with %> , e.g.,
<%!
String username = Shan; %>
I Expression Tags: produce output (a String object) that can be used to display result on JSP, start with <%= and end with
%> , e.g., <%=name %>
I Scriptlets: start with <% and end with %> , e.g., <%
java.util.Date date = new java.util.Date(); %>
SSC - Communication and Networking Java Server Pages (JSP)
JSP vs Java Servlet
I
Differences:
I Java Servlet is “HTML in Java”, while JSP is “Java in HTML”.
I Servlets run faster compared to JSP since JSP needs to be translated and compiled before execution.
I More convenient to write JSP to generate HTML
I
Q: When to use JSP?
I
A: 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
SSC - Communication and Networking Model-View-Controller (MVC)
Java web application development: problems
I
Many software systems are essentially to retrieve data from a
data store, e.g., a database and display it for the user:
I The user changes the data using an user interface I The system stores the updates in the data store
I
Most straightforward design: combines user interface and data
store to reduce the amount of coding and to improve
application performance
I
Problems:
I user interface tends to change much more frequently than the data storage system.
I mixed up the presentation (the user interface) and application logic (data store).
SSC - Communication and Networking Model-View-Controller (MVC)
What is Model-View-Controller (MVC)?
I
MVC: a design pattern for efficiently relating the user
interface to underlying data models.
I
Three main components:
I 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. I View: a collection of classes representing the elements in the
user interface for the user to see on the screen
I Controller: classes connecting the model and the view, and is used to communicate between classes in the model and view.
SSC - Communication and Networking Model-View-Controller (MVC)
What is Model-View-Controller (MVC)?
Model View Controller User Manipulates Request Updates Return resultsSSC - Communication and Networking Model-View-Controller (MVC)
Advantages of MVC
I
Better complexity management:
I Software separate presentation (view) from application logic (model)
I Consequently, code is cleaner and easier to understand I Enable large teams of developers for parallel development
I
Flexibility: Presentation or user interface can be completely
revamped without touching the model
I
Reusability: The same model can used for other application
SSC - Communication and Networking Model-View-Controller (MVC)
MVC for Java Web Application
I
Model: Plain Old Java Object (POJO) or Java Beans
I
View: Java Server Pages (JSP)
I
Controller: Java Sevlet, which decides:
I what application logic code to be applied in the model I which JSP page is appropriate to present those particular
results and forwards the request there
I
A Java Sevlet Controller:
I handles incoming requests
I instantiates of model and invokes the correct application logic in the model for the request
I forwards the results from the model and request from the user to the appropriate view (JSP file) −→ RequestDispatcher I You can use request.setAttribute or
SSC - Communication and Networking Model-View-Controller (MVC)
MVC for Java Web Application
Model (POJO/Bean) View (JSP pages) Controller (Servlet) User Instantiate/ invoke Request Forward Request/results Return results
SSC - Communication and Networking Java Enterprise applications
What is Enterprise applications?
I
Enterprise applications: “ large-scale, multi-tiered, scalable,
reliable, and secure network applications”
I
Three Tier architecture consists of:
I Presentation Tier: usually consists of clients and components that handle the interaction between clients and the application tier
I Application Tier (Business/Logic Tier): coordinates
application, e.g., its business logic, decisions, calculations and evaluations, moves data between the presentation and data tiers.
I Data Tier (Enterprise Information Systems Tier): retrieves and stores data.
SSC - Communication and Networking Java Enterprise applications
Enterprise applications 3-Tier architecture
Java Enterprise Application Three Tier Architecture
A p p lic ati o n ti er Pr es en ta ti on ti er D ata ti e r
Browser with dynamic
web pages Desktop applications
Servlet/JSP/JSF componets
EJB/POJO
SSC - Communication and Networking Java Enterprise applications
Tools for Java Enterprise Application Development
I
Java EE: the one your are learning but need learned more
I
Spring Framework: one of the most popular open source
application development framework for developing Java
enterprise applications.
I Consists of many modules to simply development
I 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.
I 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.
I The basic idea: to simplify development complexity by loosing couplings/dependencies between classes
SSC - Communication and Networking Java Enterprise applications
Links
I