Core Web Application
Development Using Servlet &
JSP
Bahaa Farouk
                       Organized
                          By
Agenda
• Servlet
   – What is Java Servlet?
   – Servlet Life Cycle
   – Client Interaction & Session
• JSP
   –   What’s JSP?
   –   JSP Life Cycle
   –   What’s JSP Contains?
   –   JSP Scope & Implicit Objects
• Why Servlet & JSP?
Web Application Development?
Agenda
• Servlet
   – What is Java Servlet?
   – Servlet Life Cycle
   – Client Interaction & Session
• JSP
   –   What’s JSP?
   –   JSP Life Cycle
   –   What’s JSP Contains?
   –   JSP Scope & Implicit Objects
• Why Servlet & JSP?
What is Java Servlet?
• An alternate form of server-side computation that
  uses Java
• The Web server is extended to support an API, and
  then Java programs use the API to create dynamic
  web pages
• Using Java servlets provides a platform-independent
  replacement for CGI scripts.
• Servlets can be embedded in many different servers
  because the servlet API, which you use to write
  servlets, assumes nothing about the server's
  environment or protocol.
Servlet Life Cycle
• Initialization
   – the servlet engine loads the servlet’s *.class file in the JVM
     memory space and initializes any objects
• Execution
   – when a servlet request is made,
      • a ServletRequest object is sent with all information about the
        request
      • a ServletResponse object is used to return the response
• Destruction
   – the servlet cleans up allocated resources and shuts down
Client Interaction
• When a servlet accepts a call from a client,
  it receives two objects:
  – A ServletRequest, which encapsulates the
    communication from the client to the server.
  – A ServletResponse, which encapsulates the
    communication from the servlet back to the
    client.
• ServletRequest and ServletResponse are
  interfaces defined by the javax.servlet
  package.
Request Header Example
Request Parameters
Cookies
Session Capabilities
• Session tracking is a mechanism that servlets use to
  maintain state about a series of requests from the
  same user(that is, requests originating from the
  same browser) across some period of time.
• Session tracking capabilities. The servlet writer can
  use these APIs to maintain state between the servlet
  and the client that persists across multiple
  connections during some time period.
Sessions
Agenda
• Servlet
   – What is Java Servlet?
   – Servlet Life Cycle
   – Client Interaction & Session
• JSP
   –   What’s JSP?
   –   JSP Life Cycle
   –   What’s JSP Contains?
   –   JSP Scope & Implicit Objects
• Why Servlet & JSP?
What is JSP?
• A Java Servlet is a Java program that is run on the
  server
   – There are Java classes for retrieving HTTP requests and
     returning HTTP responses
   – Must return an entire HTML page, so all tuning of the page
     must be done in a Java program that needs to be re-
     compiled
• Java Server Pages (JSP)
   – use HTML and XML tags to design the page and JSP scriplet
     tags to generate dynamic content (Easier for separation
     between designer & developer)
   – use Java Beans and useful built-in objects for more
     convenience
JSP Life Cycle
• JSP page (MyFirstJSP.jsp)
   –   Translated to Servle (MyFirstJSP.servlet)
   –   Compiled to class (MyFirstJSP.class)
   –   Loaded into memory (Initialization)
   –   Execution (repeats)
   –   Destruction


• Any change in JSP page automatically repeats the
  whole life cycle.
Introduction
 • A Java Servlet is a Java program that is run on the
   server
    – There are Java classes for retrieving HTTP requests and
      returning HTTP responses
 • Java Server Pages (JSP)
    – use HTML and XML tags to design the page and JSP scriplet
      tags to generate dynamic content
    – use Java Beans, which are reusable components that are
      invoked by scriplets
What do JSPs contain?
• Template data
  – Everything other than elements (eg. Html tags)
• Elements
  – based on XML syntax
     • <somejsptag attribute name=“atrribute value”> BODY
       </somejsptag>
  – Directives
  – Scripting
     • Declarations
     • Scriptles
     • Expressions
  – Standard Actions
Directives
 • <%@ directivename attribute=“value”
   attribute=“value” %>
 • The page directive
    – <%@ page ATTRIBUTES %>
    – language, import, Buffer, errorPage,…
    – <%@ page languange=“java”
      import=“java.rmi.*,java.util.*” %>
 • The include directive
    – <%@ include file=“Filename” %>
    – the static file name to include (included at translation
      time)
 • The taglib directive
    – <% taglib uri=“taglibraryURI” prefix=“tagPrefix” %>
Scripting
(Declaration, Expressions, Scriptlets)
 • <%! . . %> declares variables or methods
    – define class-wide variables
    – <%! int i = 0; %>
    – <%! int a, b; double c: %>
    – <%! Circle a = new Circle(2.0); %>
    – You must declare a variable or method in a jsp page before
      you use it
    – The scope of a declaration is the jsp file, extending to all
      includes

 • <%= . . %> defines an expression and casts the result
   as a string
Scripting II
  • <%= . . %> can contain any language expression, but
    without a semicolon, e.g.
  • <%= Math.sqrt(2) %>
  • <%= items[I] %>
  • <%= a + b + c %>
  • <%= new java.util.Date() %>
  • <% . . %> can handle declarations (page scope),
    expressions, or any other type of code fragment
  • <% for(int I = 0; I < 10; I++) {
        out.println(“<B> Hello World: “ + I);       } %>
JSP and Scope
• Page - objects with page scope are accessible only within the
  page where they are created
• Request - objects with request scope are accessible from
  pages processing the same request where they were created
• Session - ojbects with session scope are accessible from pages
  processing requests that are in the same session as the one in
  which they were created
• Application - objects with application scope are accessible
  from pages processing requests that are in the same
  application as the one in which they were created
• All the different scopes behave as a single name space
Implicit Objects
• These objects do not need to be declared or instantiated by
  the JSP author, but are provided by the container (jsp engine)
  in the implementation class
• request Object (javax.servlet.ServletRequest)
• response Object (javax.servlet.ServletResponse)
• session Object (javax.servlet.http.HttpSession)
• application Object
• out Object
• config Object
• page Object
• pageContext Object (javax.servlet.jsp.PageContext)
• exception
Number guess - Browser Output
Agenda
• Servlet
   – What is Java Servlet?
   – Servlet Life Cycle
   – Client Interaction & Session
• JSP
   –   What’s JSP?
   –   JSP Life Cycle
   –   What’s JSP Contains?
   –   JSP Scope & Implicit Objects
• Why Servlet & JSP?
Why Servlet/JSP?
What is an Enterprise Application?
 •   Reliable
 •   Scalable
 •   Maintainable
 •   Manageable

     – If you are developing an Enterprise Application for
     whose daily transactions are millions?
        • Performance? Scalability? Reliability?
Questions ?

Core web application development

  • 1.
    Core Web Application DevelopmentUsing Servlet & JSP Bahaa Farouk Organized By
  • 2.
    Agenda • Servlet – What is Java Servlet? – Servlet Life Cycle – Client Interaction & Session • JSP – What’s JSP? – JSP Life Cycle – What’s JSP Contains? – JSP Scope & Implicit Objects • Why Servlet & JSP?
  • 3.
  • 4.
    Agenda • Servlet – What is Java Servlet? – Servlet Life Cycle – Client Interaction & Session • JSP – What’s JSP? – JSP Life Cycle – What’s JSP Contains? – JSP Scope & Implicit Objects • Why Servlet & JSP?
  • 5.
    What is JavaServlet? • An alternate form of server-side computation that uses Java • The Web server is extended to support an API, and then Java programs use the API to create dynamic web pages • Using Java servlets provides a platform-independent replacement for CGI scripts. • Servlets can be embedded in many different servers because the servlet API, which you use to write servlets, assumes nothing about the server's environment or protocol.
  • 6.
    Servlet Life Cycle •Initialization – the servlet engine loads the servlet’s *.class file in the JVM memory space and initializes any objects • Execution – when a servlet request is made, • a ServletRequest object is sent with all information about the request • a ServletResponse object is used to return the response • Destruction – the servlet cleans up allocated resources and shuts down
  • 7.
    Client Interaction • Whena servlet accepts a call from a client, it receives two objects: – A ServletRequest, which encapsulates the communication from the client to the server. – A ServletResponse, which encapsulates the communication from the servlet back to the client. • ServletRequest and ServletResponse are interfaces defined by the javax.servlet package.
  • 8.
  • 9.
  • 10.
  • 11.
    Session Capabilities • Sessiontracking is a mechanism that servlets use to maintain state about a series of requests from the same user(that is, requests originating from the same browser) across some period of time. • Session tracking capabilities. The servlet writer can use these APIs to maintain state between the servlet and the client that persists across multiple connections during some time period.
  • 12.
  • 13.
    Agenda • Servlet – What is Java Servlet? – Servlet Life Cycle – Client Interaction & Session • JSP – What’s JSP? – JSP Life Cycle – What’s JSP Contains? – JSP Scope & Implicit Objects • Why Servlet & JSP?
  • 14.
    What is JSP? •A Java Servlet is a Java program that is run on the server – There are Java classes for retrieving HTTP requests and returning HTTP responses – Must return an entire HTML page, so all tuning of the page must be done in a Java program that needs to be re- compiled • Java Server Pages (JSP) – use HTML and XML tags to design the page and JSP scriplet tags to generate dynamic content (Easier for separation between designer & developer) – use Java Beans and useful built-in objects for more convenience
  • 15.
    JSP Life Cycle •JSP page (MyFirstJSP.jsp) – Translated to Servle (MyFirstJSP.servlet) – Compiled to class (MyFirstJSP.class) – Loaded into memory (Initialization) – Execution (repeats) – Destruction • Any change in JSP page automatically repeats the whole life cycle.
  • 16.
    Introduction • AJava Servlet is a Java program that is run on the server – There are Java classes for retrieving HTTP requests and returning HTTP responses • Java Server Pages (JSP) – use HTML and XML tags to design the page and JSP scriplet tags to generate dynamic content – use Java Beans, which are reusable components that are invoked by scriplets
  • 17.
    What do JSPscontain? • Template data – Everything other than elements (eg. Html tags) • Elements – based on XML syntax • <somejsptag attribute name=“atrribute value”> BODY </somejsptag> – Directives – Scripting • Declarations • Scriptles • Expressions – Standard Actions
  • 18.
    Directives • <%@directivename attribute=“value” attribute=“value” %> • The page directive – <%@ page ATTRIBUTES %> – language, import, Buffer, errorPage,… – <%@ page languange=“java” import=“java.rmi.*,java.util.*” %> • The include directive – <%@ include file=“Filename” %> – the static file name to include (included at translation time) • The taglib directive – <% taglib uri=“taglibraryURI” prefix=“tagPrefix” %>
  • 19.
    Scripting (Declaration, Expressions, Scriptlets) • <%! . . %> declares variables or methods – define class-wide variables – <%! int i = 0; %> – <%! int a, b; double c: %> – <%! Circle a = new Circle(2.0); %> – You must declare a variable or method in a jsp page before you use it – The scope of a declaration is the jsp file, extending to all includes • <%= . . %> defines an expression and casts the result as a string
  • 20.
    Scripting II • <%= . . %> can contain any language expression, but without a semicolon, e.g. • <%= Math.sqrt(2) %> • <%= items[I] %> • <%= a + b + c %> • <%= new java.util.Date() %> • <% . . %> can handle declarations (page scope), expressions, or any other type of code fragment • <% for(int I = 0; I < 10; I++) { out.println(“<B> Hello World: “ + I); } %>
  • 21.
    JSP and Scope •Page - objects with page scope are accessible only within the page where they are created • Request - objects with request scope are accessible from pages processing the same request where they were created • Session - ojbects with session scope are accessible from pages processing requests that are in the same session as the one in which they were created • Application - objects with application scope are accessible from pages processing requests that are in the same application as the one in which they were created • All the different scopes behave as a single name space
  • 22.
    Implicit Objects • Theseobjects do not need to be declared or instantiated by the JSP author, but are provided by the container (jsp engine) in the implementation class • request Object (javax.servlet.ServletRequest) • response Object (javax.servlet.ServletResponse) • session Object (javax.servlet.http.HttpSession) • application Object • out Object • config Object • page Object • pageContext Object (javax.servlet.jsp.PageContext) • exception
  • 23.
    Number guess -Browser Output
  • 24.
    Agenda • Servlet – What is Java Servlet? – Servlet Life Cycle – Client Interaction & Session • JSP – What’s JSP? – JSP Life Cycle – What’s JSP Contains? – JSP Scope & Implicit Objects • Why Servlet & JSP?
  • 25.
    Why Servlet/JSP? What isan Enterprise Application? • Reliable • Scalable • Maintainable • Manageable – If you are developing an Enterprise Application for whose daily transactions are millions? • Performance? Scalability? Reliability?
  • 26.