Chapter 2 The Web Tier

indexadjustmentInternet και Εφαρμογές Web

13 Νοε 2013 (πριν από 3 χρόνια και 7 μήνες)

99 εμφανίσεις

1

Chapter 2 The Web Tier



Web Applications and Web Containers



䑹湡浩挠䍯C瑥湴n䍲敡瑩潮



Application Designs



Servlets Technology



JSP Technology

2

Web Applications and Web

Containers



Web application



A collection of HTML/XML documents,


Web components and other resources



Provides dynamic and interactive content to


browser
-
based clients.



Web container



A runtime environment for a Web application



Provides Web components with a naming


context and life cycle management.


3

Dynamic Content Creation



Common Gateway Interface (CGI) scripts



䩡癡 獥牶汥l猠⁴s捨湯汯杹



䩡癡

Server Pages technology


4

Servlets



Portable platform
-

and Web server
-
independent



䕸瑥湳楢汥



健牦潲洠扥瑴敲⁴桡渠䍇䤠獣物灴r

5

JavaServer Pages Technology



Providing a declarative, presentation
-
centric


method




䍯C瑥湴n慮搠摩獰污d 汯杩挠l牥⁳ 灡牡瑥d



Reusing code through a component
-
based


architecture


6

Application Designs

7

Applications with Basic

JSP Pages and Servlets

8

JSP pages with JavaBeans


components

9

EJB
-
Centric Applications



Model
-

Represents the data on which an application is based



View
-

Presents the data represented by the model in a way that's


targeted at a specific type of client


Controller
-
A Central point of control. Maintains the data in the


model and ensures data and view consistency.

10

Controller Conversion of HTTP

Request to Model Change Event

11


Servlet Technology


What Is a Servlet?


Servlet API and Servlet life cycle


HTTP Support


Security Issues



12

What Is a Servlet?


A
servlet

is a Java programming
language class used to extend the
capabilities of servers that host
applications accessed via a request
-
response programming model.


13



A servlet is a Java component that can be plugged into a

Java
-
enabled web server to provide custom services:


New features


Runtime changes to content


Runtime changes to presentation


New standard protocols (such as FTP)


New custom protocols




卥牶汥r猠慲攠摥獩杮敤⁴漠w潲欠w楴桩i 愠牥煵r獴⽲敳e潮獥


processing model.

What Is a Servlet?

14

Servlet API

Defining the interface between servlets and

servers:


Package javax.servlet


Package javax.servlet.http

15

Servlet API Functions



Servlet life cycle management



䅣捥獳 瑯⁳敲癬v琠t潮瑥硴



啴楬U瑹 捬c獳敳



HTTP
-
specific support classes

16

Servlet Life Cycle



three main methods:



init()



service()



destroy()



two ancillary methods:


getServletConfig()


getServletInfo()


17

Servlet life cycle





I f a n i n s t a n c e o f t h e s e r v l e t d o e s n o t e x i s t, t h e W e b
c o n t a i n e r



L o a d s t h e s e r v l e t c l a s s.


C r e a t e s a n i n s t a n c e o f t h e s e r v l e t c l a s s.


I n i t i a l i z e s t h e s e r v l e t i n s t a n c e b y c a l l i n g t h e i n i t
m e t h o d.


I n v o k e s t h e s e r v i c e m e t h o d, p a s s i n g a r e q u e s t a n d


r e s p o n s e o b j e c t.



I f t h e c o n t a i n e r n e e d s t o r e m o v e t h e s e r v l e t, i t f i n a l i z e s
t h e s e r v l e t b y c a l l i n g t h e s e r v l e t's d e s t r o y m e t h o d.

18

Writing Service Methods


The general pattern for a service method
is to
extract information from the request
,
access external resources
, and then
populate the response

based on that
information.


Getting Information from Requests


Constructing Responses


19

Getting Information from Requests


All requests implement the
ServletRequest

interface. This interface defines methods
for accessing parameters, etal.


An input stream can be retrieved from the
request for manually parsing the data:


getInputStream( )


getReader( )


HTTP servlets are passed an HTTP
request object which contains the request
URL, HTTP headers, query string, and so
on.

20

Constructing Responses


All responses implement the
ServletResponse

interface:


Retrieve an output stream to use to send data to
the client.


getWriter(), return PrintWriter


getOutputStream( ), return
ServletOutputStream


Indicate the content type (for example, text/html),
being returned by the response.


Indicate whether to buffer output


Set localization information.

21

Sample Servlet
-
1

import java.io.*;

import javax.servlet.*;

public class SampleServlet implements Servlet {


private ServletConfig config;


public void init (ServletConfig config) throws ServletException

{


this.config = config;


}


public void destroy() {}

// do nothing


public ServletConfig getServletConfig() {


return config;


}


public String getServletInfo() {


return "A Simple Servlet";


}



22

public void service (ServletRequest req,


ServletResponse res


) throws ServletException, IOException {


res.setContentType( "text/html" );


PrintWriter out = res.getWriter();


out.println( "<html>" );


out.println( "<head> );


out.println( "<title>A Sample Servlet</title>" );


out.println( "</head>" );


out.println( "<body>" );


out.println( "<h1>A Sample Servlet</h1>" );


out.println( "</body>" );


out.println( "</html>" );


out.close();


}

}

Sample Servlet
-
2

23

Servlet Context



Servlet Initialization Information




Server Context Information




Servlet Context During a Service Request


24

Utility Classes



Interface javax.servlet.SingleThreadModel for


writing simple servlets



T睯⁥硣数瑩潮 捬c獳敳


javax.servlet.ServletException


javax.servlet.UnavailableException

25

HTTP Protocal


HTTP defines a set of text
-
based request messages called
HTTP methods.


The HTTP methods include:


GET
: Retrieves the resource identified by the request URL


HEAD
: Returns the headers identified by the request URL


POST
: Sends data of unlimited length to the Web server


PUT
: Stores a resource under the request URL


DELETE
: Removes the resource identified by the request
URL


OPTIONS
: Returns the HTTP methods the server supports


TRACE
: Returns the header fields sent with the
TRACE

request


26

HTTP Support


Javax.servlet.http.HttpServlet implements the
javax.servlet.Servlet interface.


HttpServlet methods:


Service(): Dispatches the HTTP message to one of severial





special methods.


Others:Called by the server (via the
service

method) to allow
a servlet to handle a GET, Post … request.


doGet():


doDelete()


doOptions()


doPost()


doTrace()


To write an HTTP servlet, you can extend HttpServlet
and add your own custom processing.

27

HTTP Support

28

Security Issues



The Servlet Sandbox




Access Control Lists (ACLs)

29


JSP Technology



Introduction



䩓J䅲捨楴散瑵牥



JSP Access Models



JSP Syntax Basics


30

What Is a JSP Page?


A
JSP page

is a text
-
based document
that contains two types of text: static
template data, which can be
expressed in any text
-
based format,
such as HTML, WML, and XML; and
JSP elements, which construct
dynamic content.


31

Components of JSP pages



Static HTML/XML components.



Special JSP tags



卮楰灥S猠潦o捯摥 睲w瑴敮e楮⁴桥h䩡癡


programming language called "scriptlets."


32

JSP Advantages



Separation of static from dynamic content



W物r攠佮捥 創R 䅮Aw桥牥



䑹湡浩挠捯湴敮琠捡渠扥⁳敲癥搠楮⁡i癡物r瑹


of formats



Recommended Web access layer for n
-
tier


architecture



Completely leverages the Servlet API

33

JSP Architecture

34

JSP Architecture

JSP pages are subject to a translation phase and


a request processing phase.

35

… ...

public class _0005cjsp_0005cjsptest_0002ejspjsptest_jsp_0

extends HttpJspBase {


… …

public void _jspService(HttpServletRequest request,

HttpServletResponse response)

throws IOException, ServletException {

JspFactory _jspxFactory = null;

… …

Date d = new Date();

String today = DateFormat.getDateInstance().format(d);

// end

// begin

out.write("
\
r
\
nToday is:
\
r
\
n<em> ");…...

Translation

36

The Life Cycle of a JSP Page

37

JSP Access Models
-
1

38

JSP Access Models
-
2

39

JSP Syntax Basics



Directives





獣物灴r湧 敬e浥湴m




standard actions

40

Directives



<%@ ... %>

tag.




two primary directives


page

<%@ page import="java.util.*, com.foo.*" buffer="16k" %>


include

<%@ include file="copyright.html" %>

41

scripting elements



Declarations ,<%! ... %> tag.


<%! int i=0; %>



Expressions, <%= ... %> tag


<%= fooVariable %>


<%= fooBean.getName() %>



卣物灴S整猬s<% ⸮⸠%㸠瑡g



Comments, <%
--

...
--
%> tag

42

Standard Actions



Using JavaBean Components


<jsp:useBean id="user" class="com.jguru.Person"


scope="session" />



䙯牷慲摩a朠剥煵敳瑳


<jsp:forward page="somePage.jsp" />



Including Requests


<jsp:include page="shoppingcart.jsp" flush="true"/>

43

Java Bean


JAVA BEANS components are Java classes
that can be easily reused and composed
together into applications. Any Java class
that follows certain design conventions can
be a JavaBeans component.


44

Java Bean Design Pattern


The bean encapsulates its properties by
declaring them private;


Read/write, read
-
only, or write
-
only


Simple, which means it contains a single value, or indexed,
which means it represents an array of values


The bean provides public accessor
(getter/setter) methods for reading and
modifying properties’ values.



For each readable property, the bean must have a method
of the form
PropertyClass getProperty() { ... }



For each writable property, the bean must have a method
of the form
setProperty(PropertyClass pc) { ... }

45

Java Bean Design Pattern


In addition to the property methods, a
JavaBeans component must define a
constructor that takes no parameters.



46


An Example of Java Bean

public class Currency {


private Locale locale;


private double amount;


public Currency() {



locale = null;



amount = 0.0;



}


public void setLocale(Locale l) {



locale = l;



}


public void setAmount(double a) {



amount = a;



}


public String getFormat() {



NumberFormat nf =



NumberFormat.getCurrencyInstance(locale);



return nf.format(amount);



}

}


47

Object Scope

48

Accessing Objects from a JSP Page


49

Servlets and JSP Pages



Can be used to solve identical problems.



Servlets are expressed in Java ; JSP pages are


text
-
based documents



A 獥牶汥l⁵獥猠灲楮琠p瑡瑥浥湴m 瑯t灯獴 䡔䵌


data


Cannot preview the look and feel of an HTML


page


Difficult to maitain when data or its display


format needs changes


50



JSP can use graphical development tools , use


JavaBeans components and/or custom tags that


handle complex dynamic formatting of HTML



䱩步⁳ 牶汥l猬s䩓J⁣ 渠睯牫⁩渠w⁰潲瑡扬b


platform
-

or application
-
independent means and


supports a reusable component model



A 獯灨楳瑩s慴敤eW敢⁡灰汩l慴楯渠捡渠捯湳楳琠


solely of JSP pages, custom tags, and JavaBeans


components



獥牶汥l猠s牥⁲ 牥汹 湥捥獳慲y.

Servlets and JSP Pages

51

When Servlet More Appropriate



Generating Binary Data



Extending a Web Server's Functionality



䉥獴 獵楴敤e景爠f潷
-
汥l敬e慰灬楣a瑩潮t晵湣瑩潮猠


not frequently modified



Communicating with Applets or applications