Unit Four


El alumno será capaz de utilizar tecnologías de la plataforma industrial Java (Enterprise Edition) para implementar una aplicación Web de moderada complejidad. Al finalizar el curso, el alumno será capaz de aplicar los conocimientos adquiridos en la construcción de sus futuros proyectos, reduciendo su tiempo de desarrollo y consiguiendo resultados que demuestren eficiencia, robustez, flexibilidad y solidez.

Contenido Temático

  1. Introducción a JDBC (Java DataBase Connectivity)
  2. Java Servlets
  3. JavaServer Pages
  4. JSP Tag Libraries
  5. Model View Controller revisado
  6. Model View Controller en acción: Frameworks

Java Platforms

Java technology is both a programming language and a platform.

The Java programming language is a high-level object-oriented language that has a particular syntax and style. A Java platform is a particular environment in which Java programming language applications run.

The Java Programming Language Platforms

All Java platforms consist of a Java Virtual Machine (VM) and an application programming interface (API).

The Java Virtual Machine is a program, for a particular hardware and software platform, that runs Java technology applications.

An API is a collection of software components that you can use to create other software components or applications.

Each Java platform provides a virtual machine and an API, and this allows applications written for that platform to run on any compatible system with all the advantages of the Java programming language: platform-independence, power, stability, ease-of-development, and security.

Java Platform, Standard Edition (Java SE)

When most people think of the Java programming language, they think of the Java SE API. Java SE's API provides the core functionality of the Java programming language. It defines everything from the basic types and objects of the Java programming language to high-level classes that are used for networking, security, database access, graphical user interface (GUI) development, and XML parsing.

In addition to the core API, the Java SE platform consists of a virtual machine, development tools, deployment technologies, and other class libraries and toolkits commonly used in Java technology applications.


Java Platform, Enterprise Edition (Java EE)

The Java EE platform is built on top of the Java SE platform. The Java EE platform provides an API and runtime environment for developing and running large-scale, multi-tiered, scalable, reliable, and secure network applications.

Java EE is the standard in community-driven enterprise software. Java EE is developed using the Java Community Process, with contributions from industry experts, commercial and open source organizations, Java User Groups, and countless individuals. Each release integrates new features that align with industry needs, improves application portability, and increases developer productivity.


Java Platform, Micro Edition (Java ME)

The Java ME platform provides an API and a small-footprint virtual machine for running Java programming language applications on small devices, like mobile phones. The API is a subset of the Java SE API, along with special class libraries useful for small device application development. Java ME applications are often clients of Java EE platform services.



JavaFX is a platform for creating rich internet applications using a lightweight user-interface API. JavaFX applications use hardware-accelerated graphics and media engines to take advantage of higher-performance clients and a modern look-and-feel as well as high-level APIs for connecting to networked data sources. JavaFX applications may be clients of Java EE platform services.



Java EE

"Enterprise applications" is a shorthand name for large-scale, multi-tiered, scalable, reliable, and secure network applications. These applications are designed to solve the problems encountered by large enterprises. The benefits of an enterprise application are helpful, even essential, for individual developers and small organizations in an increasingly networked world.

Tiered Applications

In a multi-tiered application, the functionality of the application is separated into isolated functional areas, called tiers. Typically, multi-tiered applications have a client tier, a middle tier, and a data tier (often called the enterprise information systems tier).

The client tier consists of a client program that makes requests to the middle tier.

The middle tier is divided into a web tier and a business tier, which handle client requests and process application data, storing it in a permanent datastore in the data tier.

Java EE application development concentrates on the middle tier to make enterprise application management easier, more robust, and more secure.

The Client Tier

Consists of application clients that access a Java EE server. The clients make requests to the server. The server processes the requests and returns a response back to the client. Many different types of applications can be Java EE clients, and they are not always, or even often Java applications. Clients can be a web browser, a standalone application, or other servers.

The Web Tier

Consists of components that handle the interaction between clients and the business tier. Its primary tasks are the following:

  • Dynamically generate content in various formats for the client
  • Collect input from users of the client interface and return appropriate results from the components in the business tier
  • Control the flow of screens or pages on the client
  • Maintain the state of data for a user's session
  • Perform some basic logic and hold some data temporarily in managed beans
See Web Tier Java EE Technologies

The Business Tier

Consists of components that provide the business logic for an application. Business logic is code that provides functionality to a particular business domain, like the financial industry, or an e-commerce site. In a properly designed enterprise application, the core functionality exists in the business tier components.

The following Java EE technologies are among those that are used in the business tier in Java EE applications:

  • Enterprise JavaBeans (enterprise bean) components
  • JAX-RS RESTful web services
  • Java Persistence API entities

The Enterprise Information Systems Tier

The enterprise information systems (EIS) tier consists of database servers, enterprise resource planning systems, and other legacy data sources, like mainframes. These resources typically are located on a separate machine from the Java EE server, and are accessed by components on the business tier.

The following Java EE technologies are used to access the EIS tier in Java EE applications:

  • The Java Database Connectivity API (JDBC)
  • The Java Persistence API
  • The Java EE Connector Architecture
  • The Java Transaction API (JTA)



The JDBC API is the industry standard for database-independent connectivity between the Java programming language and a wide range of databases. The JDBC API provides a call-level API for SQL-based database access. JDBC technology allows you to use the Java programming language to exploit "Write Once, Run Anywhere" capabilities for applications that require access to enterprise data.



JavaServer Pages (JSP) technology allows you to easily create web content that has both static and dynamic components. JSP technology makes available all the dynamic capabilities of Java Servlet technology but provides a more natural approach to creating static content.

Key points:
  • It uses an expression language (EL) for accessing server-side objects
  • It allows to combine Java code within the marking up of a page
  • JSP file gets compiled to a servlet (this happens internally and is done by the server)
  • It's extensible and allows new tags to be added or created (JSTL)
  • EL has acces to the following implicit objects
  • JSP allows scriptlets, scriptlet expressions, and scriptlet declarations unless scripting-invalid is specified in the web.xml
More on JSP

JSTL (JSP Standard Tag Library)

JSTL is an example of the extensibility JSP provides and some of them are very useful since they allow to perform common tasks without the need of scriptlets (java code within JSP)

You can perform control flow operations using JSTL .
JSTL won't solve all the design problems, so still need to pay attention on how you are using tags.

More on JSTL.


As soon as the web began to be used for delivering services, service providers recognized the need for dynamic content. Applets, one of the earliest attempts toward this goal, focused on using the client platform to deliver dynamic user experiences. At the same time, developers also investigated using the server platform for this purpose. Initially, Common Gateway Interface (CGI) scripts were the main technology used to generate dynamic content. Although widely used, CGI scripting technology has a number of shortcomings, including platform dependence and lack of scalability. To address these limitations, Java Servlet technology was created as a portable way to provide dynamic, user-oriented content.

A servlet is a Java class that is used to extend the capabilities of servers that host applications accessed by means of a request-response programming model.

  • The javax.servlet and javax.servlet.http packages provide interfaces and classes for writing servlets. All servlets must implement the Servlet interface, which defines life-cycle methods. When implementing a generic service, you can use or extend the GenericServlet class provided with the Java Servlet API.
  • The HttpServlet class provides methods, such as doGet and doPost, for handling HTTP-specific services.
  • web.xml maps a servlet class to a url pattern by using tags:<servlet-mapping/>, <servlet/>
  • HttpServletRequest hold the information that was sent by the client/browser
  • HttpServletResponse hold the information that is going to be sent the client/browser

MVC with JSP & Servlets

Using JSP's to combine business logic seems evil, even the usage of Java code within a JSP is discouraged because this cause spaghetti code. If we want to use JSP & Servlets in an MVC way we need to consider:

  • JSP should be used only to display information (use tags / EL to pull information)
  • Servlet should be used as a controller (controllers should be kept light)
  • Business logic should be kept in a service layer which is part of the model (remember that more often than not UI is changed but business logic is preserved)
  • Use components wisely, remember SOLID applies to every bit of software


A filter is a Java class that is used to pre-process the request or post-process the response on a Java web application. The container sends the request fo the filter chain before sending it to a servlet.

  • Should implement javax.servlet.Filter
  • Filter must be mapped in the web.xml to be considered by the container.
  • Filter must invoke chain#doFilter() if the request is supposed to get to the servlet, alternatively the filter can write the output directly.
  • Some Filter usages are : statistics, compression, security, auditing..
More on filters...


Http servers are stateless, this means that they don't know if there has been a previous request or not, so they need help to know if a previous request has been sent by a client. A session is a conversational state between the client and the server.

  • JSESSIONID cookie is used to store the session id
  • URL rewriting is needed if cookies are disabled
  • We store the information in class javax.servlet.http.HttpSession
  • getSession() will create a new Session
  • getSession(boolean) may create a new Session
  • invalidate() will invalidate the session.
  • A timeout can be set in the web.xml
More on sessions


Listeners are Java classes that implement an interface (javax.servlet. or javax.servlet.http.) that allows them to "listen" specific events in a Java Web application.

  • There are 2 main categories of listeners: Session, Servlet (application-level)
  • Each category has 2 subcategories: attribute listener, life cycle listener
More on listeners

Spring MVC

The Spring Web model-view-controller (MVC) framework is designed around a DispatcherServlet that dispatches requests to handlers, with configurable handler mappings, view resolution, locale, time zone and theme resolution as well as support for uploading files. The default handler is based on the @Controller and @RequestMapping annotations, offering a wide range of flexible handling methods. With the introduction of Spring 3.0, the @Controller mechanism also allows you to create RESTful Web sites and applications, through the @PathVariable annotation and other features.

The request processing workflow in Spring Web MVC (high level)