Web App Classical Patterns

The Model View Controller is the default and acknowledged design pattern for Web Applications.

mvc sequence

MVC pattern

Sequence Diagram of a request within the MVC pattern.

mvc sequence

Java EE (Java Enterprise Edition)

  • A set of specifications for tools that extend the standard Java platform (Java SE).
  • Java EE is not a Platform, it is a platform specification : a set of standards like Servlets, JSP, EJB, Java Persistence API, JSON Processing, Web Socket API, ...
  • Many compatible implementations of the standard:
    • Oracle GlassFish
    • IBM WebSphere
    • Apache TomEE
    • Hitachi uCosminexus
    • Red Hat WildFly (Formally JBoss)
  • 2 main features :
    • A web server : serves low-level connexions, requests, cookies, security, ...
    • An Application Server : serves high-level interaction between users and applications.

Which Java EE technology should be used for Web Apps?

  • 2 main technologies can do WebApps, depending on the needs of the app
  • Servlets are best for
    • service-oriented applications (API)
    • dispatching requests (routing)
    • dealing with non-textual data
  • JavaServer Faces and Facelets pages are best for generating text-based markup (XHTML)

Java EE Web App MVC Pattern

Java EE entities in a typical Web App (client/server) request.

mvc sequence

Java EE Servlets

  • Java classes meant to extend the capabilities of an application server.
  • Could actually serve any king of network connection.
  • Mostly used with HTTP.
  • All HTTP Servlets extend the javax.servlet.http.HttpServlet abstract class.
  • Main role:
    • Manage requests and response objects (HttpServletRequest and HttpServletResponse)
    • Serve HTTP verbs (GET, POST, PUT, DELETE, ... )
    • Dispatch requests and call appropriate actions (dealing with Views and Models)

Java EE Servlets Methods

  • doGet, if the servlet supports HTTP GET requests
  • doPost, for HTTP POST requests
  • doPut, for HTTP PUT requests
  • doDelete, for HTTP DELETE requests
  • init and destroy, to manage resources that are held for the life of the servlet
  • getServletInfo, which the servlet uses to provide information about itself
  • service : receives HTTP requests and dispatches them to the doXXX methods. Should not be overridden.

Exemple Servlet

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestServlet extends HttpServlet {

	public void doGet( HttpServletRequest request, HttpServletResponse response )
		throws ServletException, IOException {
		// Do something with 'request' and 'response'

JavaServer Pages (JSP)

  • Fast creation of dynamic web pages
  • It’s a templating language based on block/tags that can create any output language (HTML, CSS, JS,…)
  • Main characteristic : new tags with new functionality can be defined with JavaServer Pages Standard Tag Library (JSTL) but for a better presentation-oriented application, JavaServer Faces and Facelets pages are preferred.
  • From a JSP a Java Servlet is dynamically created. Then the Servlet is executed to produce the output file.
<%@ page pageEncoding="UTF-8" %>
<!DOCTYPE html>
  <meta charset="utf-8" />

      <p>Je suis une page générée dynamiquement avec JSP, et je sais compter jusqu'à <%=2+3%> !</p>

Generated Servlet Class from the JSP

 * Generated by the Jasper component of Apache Tomcat
 * Version: Apache Tomcat/7.0.47
 * Generated at: 2013-12-09 21:02:40 UTC
 * Note: The last modified time of this file was set to
 *       the last modified time of the source file after
 *       generation to assist with modification tracking.
package org.apache.jsp;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;

public final class test_jsp extends org.apache.jasper.runtime.HttpJspBase
    implements org.apache.jasper.runtime.JspSourceDependent {

  private static final javax.servlet.jsp.JspFactory _jspxFactory =

  private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants;

  private javax.el.ExpressionFactory _el_expressionfactory;
  private org.apache.tomcat.InstanceManager _jsp_instancemanager;

  public java.util.Map<java.lang.String,java.lang.Long> getDependants() {
    return _jspx_dependants;

  public void _jspInit() {
    _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
    _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());

  public void _jspDestroy() {

  public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
        throws java.io.IOException, javax.servlet.ServletException {

    final javax.servlet.jsp.PageContext pageContext;
    javax.servlet.http.HttpSession session = null;
    final javax.servlet.ServletContext application;
    final javax.servlet.ServletConfig config;
    javax.servlet.jsp.JspWriter out = null;
    final java.lang.Object page = this;
    javax.servlet.jsp.JspWriter _jspx_out = null;
    javax.servlet.jsp.PageContext _jspx_page_context = null;

    try {
      pageContext = _jspxFactory.getPageContext(this, request, response,
            null, true, 8192, true);
      _jspx_page_context = pageContext;
      application = pageContext.getServletContext();
      config = pageContext.getServletConfig();
      session = pageContext.getSession();
      out = pageContext.getOut();
      _jspx_out = out;

      out.write("<!DOCTYPE html>\n");
      out.write("  <head>\n");
      out.write("  <meta charset=\"utf-8\" />\n");
      out.write("  <title>Test</title>\n");
      out.write("  </head>\n");
      out.write("  <body>\n");
      out.write("      <p>Je suis une page générée dynamiquement avec JSP, et je sais compter jusqu'à ");
      out.write(" !</p>\n");
      out.write("  </body>\n");
    } catch (java.lang.Throwable t) {
      if (!(t instanceof javax.servlet.jsp.SkipPageException)){
        out = _jspx_out;
        if (out != null && out.getBufferSize() != 0)
          try { out.clearBuffer(); } catch (java.io.IOException e) {}
        if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
        else throw new ServletException(t);
    } finally {

Generated HTML Page from the Servlet

<!DOCTYPE html>
  <meta charset="utf-8" />

      <p>Je suis une page générée dynamiquement avec JSP, et je sais compter jusqu'à 5 !</p>

Web Archives (WAR files)

Standard format for deploying an application to an application server.

├── WEB-INF/
│   ├── *.jsp
│   ├── classes/
│   │   └── *.class
│   ├── lib/
│   │   └── *.jar
│   └── web.xml
├── *.html
├── *.css
├── *.png
└── *.js

  • Resources outside The WEB-INF/ folder are public resources (html, css, js). Inside WEB-INF, files are not publicly accessible
  • web.xml is the entry point of the application. Contains routing information.
  • The classes/ folder contains compiled Java objects of the application
  • The lib/ folder contains external libraries needed by the application.