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 {

	@Override
	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>
<html>
  <head>
  <meta charset="utf-8" />
  <title>Test</title>
  </head>

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

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 =
          javax.servlet.jsp.JspFactory.getDefaultFactory();

  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 {
      response.setContentType("text/html;charset=UTF-8");
      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("\n");
      out.write("<!DOCTYPE html>\n");
      out.write("<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("\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.print(2+3);
      out.write(" !</p>\n");
      out.write("  </body>\n");
      out.write("</html>");
    } 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 {
      _jspxFactory.releasePageContext(_jspx_page_context);
    }
  }
}

Generated HTML Page from the Servlet

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

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

Web Archives (WAR files)

Standard format for deploying an application to an application server.

MyApp/
├── 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.