You are on page 1of 6

MVC Design Pattern

Spring 2011 CECS 423 lecture by Dr. Alvaro Monge

Java EE: An Introduction


JPA, EJB, JSF

The Model-View-Controller (MVC) design pattern separates the core business model functionality from the presentation and control logic that uses this functionality. The separation allows multiple views to share the same enterprise data model, which makes supporting multiple clients easier to implement, test, and maintain.
Source: Java BluePrints - J2EE Patterns, MVC

http://java.sun.com/blueprints/patterns/MVC-detailed.html

Java EE Components
Applets: GUI apps executed in a web browser. They use the
Swing API to provide powerful user interfaces.
Spring 2011 CECS 423 lecture by Dr. Alvaro Monge

JEE app and the MVC architecture

Spring 2011 CECS 423 lecture by Dr. Alvaro Monge

In a JEE application:

Applications: programs executed on a client. Typically GUIs or


batch-processing programs that have access to all the facilities of the Java EE middle tier.

The model -- business layer functionality represented by JavaBeans or


EJBs

The view -- the presentation layer functionality represented by JSFs


(the view)

Web applications: apps executed in a web container and respond


to HTTP requests from web clients.

The controller -- Servlet mediating between model and view

Made of servlets, servlet lters, web event listeners, JSP pages,


and JSF. Servlets also support web service endpoints

Must accommodate input from various clients including HTTP requests from web clients, and

Enterprise Java Beans: container-managed components for


processing transactional business logic. They can be accessed locally and remotely through RMI (or HTTP for SOAP and RESTful web services).

WML from wireless clients XML documents from suppliers Etc.

Model layer in a Web App


Spring 2011 CECS 423 lecture by Dr. Alvaro Monge Spring 2011 CECS 423 lecture by Dr. Alvaro Monge

Controller in a Web App



Serves as the logical connection between the user's interaction and the business services on the back Responsible for making decisions among multiple presentations

Models the data and behavior behind the business process What its responsible for:

Performing DB queries Calculating the business process Processing orders

e.g. User's language, locale or access level dictates a dierent presentation.

Encapsulation of data and behavior which are independent of presentation

A request enters the application through the control layer, which will decide how the request should be handled and what information should be returned

View layer in a Web App


Spring 2011 CECS 423 lecture by Dr. Alvaro Monge Spring 2011 CECS 423 lecture by Dr. Alvaro Monge

Web Applications

A web application is a dynamic extension of a web or application server. Types of web applications:

Presentation-oriented

generates interactive web pages containing various types of markup language (HTML, XHTML, XML, and so on) and dynamic content in response to requests.

Display information according to client types Display result of business logic (Model) Not concerned with how the information was obtained, or from where (since that is the responsibility of Model)

Service-oriented

A service-oriented web application implements the endpoint of a web service.

In Java EE platform, web components provide the dynamic extension capabilities for a web server.

Web components are either Java servlets, web pages, web service
endpoints, or JSP pages

Java Web App Request Handling


Spring 2011 CECS 423 lecture by Dr. Alvaro Monge Spring 2011 CECS 423 lecture by Dr. Alvaro Monge

Servlets and JSF



Servlets are Java classes that dynamically process requests and construct responses. JavaServer Faces (JSF) and Facelets are used for building interactive web applications. Servlets are best suited for service-oriented applications (web service endpoints are implemented as servlets) and the control functions of a presentation-oriented application, such as dispatching requests and handling nontextual data. Java Server Faces and Facelets pages are more appropriate for generating text-based markup, such as XHTML, and are generally used for presentationoriented applications.

DB

DB

From chapter 3 of The Java EE 6 Tutorial, http://java.sun.com/javaee/6/docs/tutorial/doc/geysj.html

Java EE Architecture

Spring 2011 CECS 423 lecture by Dr. Alvaro Monge Spring 2011 CECS 423 lecture by Dr. Alvaro Monge

Java EE Services

JPA: Standard API for object-relational mapping (ORM). Includes JPQL to query objects stored in the underlying database. JMS: allows components to communicate asynchronously through messages. Java Naming and Directory Interface (JNDI): used to access naming and directory systems. JTA: a transaction demarcation API JavaMail, JavaBeans Activation Framework (JAF), XML processing, JCA, JAAS, RESTful web services, Management, Deployment.

Java EE is a set of specications implemented by dierent containers. Containers are Java EE runtime environments that provide certain services to the components they host such as lifecycle management, dependency injection, security, etc. Figure shows the logical relationships between containers. The arrows represent the protocols used.

JPA

Spring 2011 CECS 423 lecture by Dr. Alvaro Monge Spring 2011 CECS 423 lecture by Dr. Alvaro Monge

JPA mapping

Relational model (i.e. RDBMS) vs. Object Oriented model (i.e. Java) Object-Relational Mapping (ORM) Java Persistence API (JPA)

An API above JDBC Can access and manipulate relational data from Enterprise Java Beans (EJBs), web components, and Java SE applications Includes an entity manager API to perform DB-related operations like CRUD Includes JPQL, an object-oriented query language

JPA

Spring 2011 CECS 423 lecture by Dr. Alvaro Monge

Enterprise Java Beans


Spring 2011 CECS 423 lecture by Dr. Alvaro Monge

Objects vs. Entities

Objects are instances that just live in memory. Entities are objects that live shortly in memory and persistently in a database.

@Entity public class Book { @Id @GeneratedValue private Long id; @Column(nullable = false) private String title; private Float price; @Column(length = 2000) private String description; private String isbn; private Integer nbOfPage; private Boolean illustrations; // Constructors, getters, setters }

server-side components encapsulate business logic take care of transactions and security used in building business layers to sit on top of the persistence layer and as an entry point for presentation-tier technologies like JavaServer Faces (JSF). can be built by annotating a POJO that will be deployed into a container

JPA map objects to a database via metadata that can be supplied using annotations or in an XML descriptor Annotations: The code of the entity is directly annotated with all sorts of annotations described in the javax.persistence package.

Types of EJBs

Spring 2011 CECS 423 lecture by Dr. Alvaro Monge Spring 2011 CECS 423 lecture by Dr. Alvaro Monge

Web pages and web servers


Web servers

Session beans and Message-driven Beans (MDBs) Session Beans are used to encapsulate high-level business logic and can be...

Handles HTTP requests and sends a HTTP response -- typically HTML page Default HTTP port: 80 Typical servers: Apache (47%), MS (21%)

Stateless: contains no conversational state between methods, and any instance can be used for any client Stateful: contains conversational state, which must be retained across methods for a single user Singleton: A single session bean is shared between clients and supports concurrent access

Netcraft Web Server Survey (December 2009)

Web languages

HTML CSS JavaScript

EJB Example
Spring 2011 CECS 423 lecture by Dr. Alvaro Monge Spring 2011 CECS 423 lecture by Dr. Alvaro Monge

JSF

@Stateless public class BookEJB { @PersistenceContext(unitName = "chapter06PU") private EntityManager em; public Book findBookById(Long id) { return em.find(Book.class, id); } public Book createBook(Book book) { em.persist(book); return book; } }

JSF applications are standard web applications that intercept HTTP via the Faces servlet and produce HTML

Spring 2011 CECS 423 lecture by Dr. Alvaro Monge

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html"> <h:head> <title>Creates a new book</title> </h:head> <h:body> <h1>Create a new book</h1> <hr/> <h:form> <table border="0"> <tr> <td><h:outputLabel value="ISBN : "/></td> <td><h:inputText value="#{bookController.book.isbn}"/></td> </tr> <tr> <td><h:outputLabel value="Title :"/></td> <td><h:inputText value="#{bookController.book.title}"/></td> </tr> </table> <h:commandButton value="Create a book" action="#{bookController.doCreateBook}" styleClass="submit"/> </h:form> <hr/> <i>APress - Beginning Java EE 6</i> </h:body> </html>

Packaging Java EE Web Apps

Spring 2011 CECS 423 lecture by Dr. Alvaro Monge

A web application module contains:

servlets, JSPs, JSF pages, and web services, as well as HTML and XHTML pages, Cascading Style Sheets (CSS), JavaScripts, images, videos, and so on.

All these artifacts are packaged in a jar le with a .war extension -- i.e., a war le, or Web Archive.

WEB-INF/web.xml is the optional web deployment descriptor WEB-INF/ejb-jar.xml is the optional EJB Lite beans deployment descriptor. WEB-INF/classes contains all the Java .class les WEB-INF/lib contains any dependent jar les.

You might also like