ssc - web development model-view-controller for java …szh/teaching/2013backup/ssc/lecture... ·...
Post on 15-Aug-2018
219 Views
Preview:
TRANSCRIPT
SSC - Communication and Networking
SSC - Web developmentModel-View-Controller for Java Servlet
Shan He
School for Computational ScienceUniversity of Birmingham
Module 06-19321: SSC
SSC - Communication and Networking
Outline
Outline of Topics
Java Server Pages (JSP)
Model-View-Controller (MVC)
Java Enterprise applications
SSC - Communication and Networking
Java Server Pages (JSP)
What is Java Server Pages
I JSPs: a Java technology that provides a simplified, fast wayto create easily maintain, information-rich, dynamic Webpages based on HTML, XML, or other document types.
I Viewed as a high-level abstraction of Java servlets: translatedinto servlets at runtime from the JSP source file
I Requires a Servlet container, e.g., TomcatI Life cycle of JSP: very similar to Java Servlet:
I Step 1: JSP Page Translation:I Step 2: JSP Page Compilation: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 JSPI Java Code inside HTML using special JSP tags, called JSP
Scriting elementsI Three categories of JSP Scriting elements:
I Declaration Tags: define varaibales and methods in JSP, startwith <%! 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.,
<%
Date date = new 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 When to use JSP: when there is not much data process andmanipulation
I Otherwise, use Model-View-Controller design pattern, whereJSP used as View and Servlet as controller
SSC - Communication and Networking
Model-View-Controller (MVC)
The problem
I Many software systems are essentially to retrieve data from adata store, e.g., a database and display it for the user:
I The user changes the data using an user interfaceI The system stores the updates in the data store
I Most straightforward design: combines user interface and datastore to reduce the amount of coding and to improveapplication 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 userinterface 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 updatethe data. Note: No information about the user interface.
I View: a collection of classes representing the elements in theuser interface for the user to see on the screen
I Controller: classes connecting the model and the view, and isused to communicate between classes in the model and view.
SSC - Communication and Networking
Model-View-Controller (MVC)
What is Model-View-Controller (MVC)?
ModelView
Controller
User
Manipulates
Request
Updates
Return results
SSC - 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 understandI Enable large teams of developers for parallel development
I Flexibility: Presentation or user interface can be completelyrevamped without touching the model
I Reusability: The same model can used for other applicationwith different user interfaces
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 modelI which JSP page is appropriate to present those particular
results and forwards the request there
I A Java Sevlet Controller:I handles incoming requestsI instantiates of model and invokes the correct application logic
in the model for the requestI 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
session.setAttribute to pass the results to view
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 businesstier
I Application Tier (Business/Logic Tier): coordinate 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): retrieve andstore data.
SSC - Communication and Networking
Java Enterprise applications
Enterprise applications 3-Tier architecture
Java Enterprise Application Three Tier Architecture
Ap
plic
atio
n t
ier
Pres
enta
tion
tie
rD
ata
tier
Browser with dynamic web pages
Desktop applications
Servlet/JSP/JSF componets
EJB/POJO
JDBC/Hibernate/JDO etc.
SSC - Communication and Networking
Java Enterprise applications
Tools for Java Enterprise Application Development
I Java EE: the one your are learning but need learned moreI Spring Framework: one of the most popular open source
application development framework for developing Javaenterprise applications.
I Consists of many modules to simply developmentI The key concept - Inversion of control: the control flow of a
program is inverted, e.g., instead the programmer controls theflow of a program, the external source, e.g., Spring Frameworktakes control of it.
I The fundamental functionality - dependency injection: objectsdo not create other objects on which they rely to do theirwork. Instead, they get the objects from by a externalframework component.
I The basic idea: to simplify development complexity by loosingcouplings/dependencies between classes
SSC - Communication and Networking
Java Enterprise applications
Links
I Java EE vs Spring
I Spring Tutorial - Eclipse
I Spring Tutorial - Netbean
I Spring guides
top related