26
Core Web Application Development Using Servlet & JSP Bahaa Farouk Organized By

Core web application development

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Core web application development

Core Web Application Development Using Servlet & JSP

Bahaa FaroukOrganized

By

Page 2: Core web application development

• 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?

Agenda

Page 3: Core web application development

Web Application Development?

Page 4: Core web application development

• 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?

Agenda

Page 5: Core web application development

• 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.

What is Java Servlet?

Page 6: Core web application development

• 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

Servlet Life Cycle

Page 7: Core web application development

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.

Page 8: Core web application development

Request Header Example

Page 9: Core web application development

Request Parameters

Page 10: Core web application development

Cookies

Page 11: Core web application development

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.

Page 12: Core web application development

Sessions

Page 13: Core web application development

• 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?

Agenda

Page 14: Core web application development

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

Page 15: Core web application development

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.

Page 16: Core web application development

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

Page 17: Core web application development

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

Page 18: Core web application development

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” %>

Page 19: Core web application development

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

Page 20: Core web application development

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); } %>

Page 21: Core web application development

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

Page 22: Core web application development

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

Page 23: Core web application development

Number guess - Browser Output

Page 24: Core web application development

• 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?

Agenda

Page 25: Core web application development

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?

hp

Page 26: Core web application development

Questions?