Web Engineering

64
© Copyright 2008 STI - INNSBRUCK www.sti- innsbruck.at Web Engineering Web Technologies II Lecture X – 9 th December 2008 Federico M. Facca

description

Web Technologies II Lecture X – 9 th December 2008 Federico M. Facca. Web Engineering. Where are we?. Overview. Servlet JSP Java Beans Wrap-up. ServletS. Servlet Overview. Servlet is an extension to the web server that adds additional functionalities and programmability - PowerPoint PPT Presentation

Transcript of Web Engineering

Page 1: Web Engineering

© Copyright 2008 STI - INNSBRUCK www.sti-innsbruck.at

Web EngineeringWeb Technologies II

Lecture X – 9th December 2008

Federico M. Facca

Page 2: Web Engineering

Web Engineering (703512)

Where are we?

# Date Title Lecturer

1 7th Oct Web Engineering Introduction and Overview F. M. Facca

2 14h Oct Collection Requirements for Web Applications F. M. Facca

3 21st Oct Web Application Modeling F. M. Facca

4 28th Oct Developing Applications with WebML F. M. Facca

5 4th Nov Web Application Architectures I F. M. Facca

6 11th Nov Web Application Architectures II F. M. Facca

7 18th Nov Testing and Usability on the Web F. M. Facca

8 25th Nov Mid Term Exam F. M. Facca

9 2nd Dec Web Technologies I F. M. Facca

10 9th Dec Web Technologies II F. M. Facca

11 16th Dec Web Technologies III F. M. Facca

12 13th Jan Web 2.0 Mash-ups F. Daniel (UNITN)

13 20th Jan Web Application Development Process/Project Management for Web Applications

F. M. Facca

14 27th Jan Final Exam F. M. Facca

2

Page 3: Web Engineering

Web Engineering (703512)

Overview

• Servlet• JSP• Java Beans• Wrap-up

3

Page 4: Web Engineering

Web Engineering (703512)

SERVLETS

4

Page 5: Web Engineering

Web Engineering (703512)

Servlet Overview

• Servlet is an extension to the web server that adds additional functionalities and programmability

• Servlet makes full use of the Java platform• A servlet is basically a Java class. Every servlet

extends “javax.servlet.http.HttpServlet” class• Related classes are wrapped in the package of

“javax.servlet” and “javax.servlet.http”

5

Page 6: Web Engineering

Web Engineering (703512)6

Java Servlet Web Application

• Servlet development life cycle– Development

• Defining servlet classes, methods and properties

– Deployment• Servlet mapping to web environment• Deployment on Web Application Server

– Execution• Understand its execution life cycle

Page 7: Web Engineering

Web Engineering (703512)

Basic Servlet Structure

public class HelloWorld extends HttpServlet{

public void doGet(HttpServletRequest request, HttpServletResponse response )

throws ServletException, java.io.IOException

{ … }

public void doPost(HttpServletRequest request, HttpServletResponse response )

throws ServletException, IOException{ … }

}

Page 8: Web Engineering

Web Engineering (703512)

Constructor and “Main” Method

• Servlet instances are created (invoked) by servlet container automatically when requested – not by user classes or methods– No need to define constructor

• The entry point is NOT the “main” method, but the two methods– Use “doGet” or “doPost” to perform tasks

Page 9: Web Engineering

Web Engineering (703512)

Servlet Deployment

• Web content root folder (public_html)– The starting point of the whole web application– All files and sub-directories goes here:

html, images, documents …

• /public_html/WEB-INF/– This folder contains configuration files

and compiled class– Not directly accessible through the web

• /public_html/WEB-INF/classes/– All compiled classes (servlet classes and other classes)

are in this folder

Page 10: Web Engineering

Web Engineering (703512)

Servlet Mapping

• Servlet class needs to be mapped to an accessible URI (mainly through HTTP)

• For convenience, a servlet can be accessed in a general pattern (invoker servlet)– http://[domain]/[context]/servlet/[ServletClassName]– http://localhost:8988/servletintro/servlet/SimpleServlet

• Specific mapping: using the configuration file “web.xml”– A servlet is specifically mapped to a user defined URL

10

Page 11: Web Engineering

Web Engineering (703512)

“web.xml” Configuration

• Using the file “web.xml” for more specific mapping– The file is in the

“WEB-INF” folder

• Example– Servlet class

• HelloWorld.class– Application context:

• http://localhost:8080/servletintro/– Invoker class mapping

• http://localhost:8080/servletintro/servlet/HelloWorld– Specific mapping

• http://localhost:8080/servletintro/hello– For more mapping examples, see example “web.xml”

11

<servlet> <servlet-name>HelloW</servlet-name> <servlet-class>HelloWorld

</servlet-class></servlet>

<servlet-mapping> <servlet-name>HelloW</servlet-name> <url-pattern>hello</url-pattern></servlet-mapping>

Page 12: Web Engineering

Web Engineering (703512)

Servlet Execution Life Cycle

Servlet Class

Servlet Instance in Memory

init()

service()doPost()

doGet()

First Request

Subsequent Requests (can

be from different users and sessions)

Page 13: Web Engineering

Web Engineering (703512)

Response and Request Message

• Web servers and web clients communicate through HTTP messages (protocols)– Request message: client server– Response message: server client

• HTTP message consists of header and body– HTTP header: information describing the message and the

context of the message– HTTP body: content usually for display

13

Page 14: Web Engineering

Web Engineering (703512)

Request Processing

• Major request method type– Get

• User data is sent as part of the request URL• No request message body• Triggering actions: address bar (in browser), link, form, …

– Post• User data is sent in the request message body• Triggering actions: form, …

14

Page 15: Web Engineering

Web Engineering (703512)

doGet() Method

• Servlet class uses the “doGet()” method to process general (servlet) URL request

public void doGet( HttpServletRequest request, HttpServletResponse response )

throws ServletException,IOException

{

( generating response message … )

}

15

Page 16: Web Engineering

Web Engineering (703512)

Response Processing

• Response body– Generating HTML or other content for display

• Response header– Manipulating response message header data to affect its

behavior

16

Page 17: Web Engineering

Web Engineering (703512)

Generating Response Content

• Using “response” (javax.servlet.http.HttpServletResponse) and “java.io.PrintWriter” to generate HTML to the output stream

java.io.PrintWriter out = response.getWriter();

out.println(“<html>”);

out.println(“</html>”);

17

Page 18: Web Engineering

Web Engineering (703512)

Using StringBuffer

• When there are frequent changes to a string, using StringBuffer (java.lang.StringBuffer) is more efficient

StringBuffer html = new StringBuffer();html.append(“<html>”);html.append(“<head>"); ……String s= html.toString();

• This is very useful when generating HTML page strings

Page 19: Web Engineering

Web Engineering (703512)

HTML Paragraph Formatting

• Formatting HTML source code, or final display in the browser?

• Use escape characters (/n, /t, etc) to format source HTML code

• Use HTML tags to format the final output in broswer– <p>, <br>, …

19

Page 20: Web Engineering

Web Engineering (703512)

Dynamically Generating HTML

• Use Java programming capability to dynamically generate HTML– Dynamic content: date/time, user name, etc.– Dynamic HTML elements (tags)– Dynamic element attributes– … (Be creative!)

20

Page 21: Web Engineering

Web Engineering (703512)

Getting User Data

• Two ways– URL parameter (Get)

• http:// …/somefile?p1=value1&p2=value2

– HTML Form (Get or Post)

21

Servlet

HTML Form

URL parameter

doGet

doPostPost

Get

GetUser Input

Page 22: Web Engineering

Web Engineering (703512)

URL with Parameters

• User data can be sent with “get” request– http:// …/somefile?p1=value1&p2=value2

• Applications– Template page for data item details

• http://www.newegg.com/Product/Product.asp?Item=N82E16827152058• http://www2.cis.gsu.edu/cis/people/display.asp?pk=3

– Page content selection• http://forum.java.sun.com/category.jspa?categoryID=20• http://javaweb.jackzheng.net/cis3270su06/index.jsp?page=schedule

– User input• http://www.google.com/search?q=java

– Conditions/configuration• http://finance.yahoo.com/q/bc?s=MSFT&t=5d&l=on&z=l&q=l• http://msdn.e-academy.com/elms/Storefront/Home.aspx?campus=gsu_cis

– Page display/format, page content, error message, …

22

Page 23: Web Engineering

Web Engineering (703512)

Handling “Get” Parameter

• In HTTP “get”, user data are sent with URL

– http://…/GetData?p1=value1&p2=value2

• Using the request object (given by the doGet method) to retrieve these data

– String p1=request.getParameter(“p1”);– String p2=request.getParameter(“p2”);– String p[ ]=request.getParameterValues(“p1”);

• Note: parameter names are case sensitive

23

Page 24: Web Engineering

Web Engineering (703512)

Number Conversion

• All parameter values are String type– String p1=request.getParameter(“p1”);

• What if numbers are needed?– Integer.parseInt()– Double.parseDouble()

• Conversion exception needs to be handled

Page 25: Web Engineering

Web Engineering (703512)

Irregular Parameters

• Missing value (empty string is returned)– …/GetData?data=

• Missing parameter (parameter undefined, null)– …/GetData? // no parameter at all– …/GetData?data // ”data” is still not defined– …/GetData?Data=red // case sensitive

• Blanks– …/GetData?data=hello world

• Redundant parameter– …/GetData?data=blue&data=red // use

getParameterValues()

25

Page 26: Web Engineering

Web Engineering (703512)

JAVA SERVER PAGES

26

Page 27: Web Engineering

Web Engineering (703512)

What is JSP

• Servlets – HTML in java code• JSP – java code in HTML

<HTML><HEAD>

<TITLE>Java Server Pages</TITLE></HEAD><BODY>

<H1>JSP</H1><%= “Java Server Pages.” %><HR>

</BODY></HTML>

Page 28: Web Engineering

Web Engineering (703512)

JSP Lifecycle

JSP to ServletTranslation

JSP to ServletTranslation

ServletCompiled

ServletCompiled

ServletLoaded

ServletLoaded

jspInit()called

jspInit()called

_jspService()called

_jspService()called

Page 29: Web Engineering

Web Engineering (703512)

The need for JSP

• With servlets– It is hard to write and maintain HTML– Cannot use standard HTML tools– HTML is inaccessible to non-java developers

April 20, 2023

Page 30: Web Engineering

Web Engineering (703512)

The benefits of JSP

• Easier to write and maintain HTML• Can use standard HTML tools• Can divide up development team

Page 31: Web Engineering

Web Engineering (703512)

Advantages

• The Java advantage• Extensive API• Easy to learn• Big development community• Standardization & server support

Page 32: Web Engineering

Web Engineering (703512)

Location of JSP pages

• Unlike servlets, JSP pages can be located in any of the locations where HTML files can be put.

Page 33: Web Engineering

Web Engineering (703512)

JSP Scripting Elements

• JSP scripting elements enable us to insert java code into JSP files.

• There are three types of elements– Expressions <%= Java Expression %>– Scriptlets <% Java Code %>– Declarations <%! Field/Method %>

Page 34: Web Engineering

Web Engineering (703512)

JSP Expressions

• A JSP expression is used to insert java code directly into the output.

Syntax<%= Expression %>

Eg:Current Time: <%= new java.util.Date() %>

Output:Current Time: Tue Aug 22 21:05:47 IST 2006

• The expression is evaluated, converted to string and inserted into the page.

Page 35: Web Engineering

Web Engineering (703512) April 20, 2023

Predefined Variables

• To simplify expressions, JSP provides a number of predefined variables (implicit objects).

– request – the HttpServletRequest– response – the HttpServletResponse– session – the HttpSession– out – the Writer (buffered version of type

JspWriter)– application – the ServletContext– config – the ServletConfig– pageContext – introduced to give single point of

access to page attributes– page – synonym for “this”

Page 36: Web Engineering

Web Engineering (703512)

JSP Scriptlets

• To something more than just output the value of a simple expression.

• Allows the programmer to insert arbitrary code into the servlets _jspService method.

Syntax:<% Java Code %>

Eg:<%

String str = request.getParameter(“name”);out.print(“Name : ”+str);

%>

April 20, 2023

Page 37: Web Engineering

Web Engineering (703512)

JSP Declarations

• JSP declarations lets the programmer define methods or fields that get inserted into the main body of the generated servlet (outside the _jspService() method)

Syntax:<%! Field/Method definition %>

Eg:<%!

private String getMessage(){return “This is a simple message!!”;

}%><%= getMessage() %>

Page 38: Web Engineering

Web Engineering (703512)

XML Syntax

• XML like syntax for JSP expression, scriptlet & declaration– <jsp:expression>…</jsp:expression>– <jsp:scriptlet>…</jsp:scriptlet>– <jsp:declaration>…</jsp:declaration>

• Supported by JSP versio 1.2 & above• These are case sensitive, should be in lowercase

Page 39: Web Engineering

Web Engineering (703512)

JSP Directives

• A JSP directive affects the overall structure of the servlet that results from the JSP page.

• A JSP directive has the form:– <%@ directive attribute=“value” … … %>

• There are three types:– page, include & taglib

Page 40: Web Engineering

Web Engineering (703512)

JSP Page Directive

• The page directive controls the structure of the servlet by importing classes, customizing the superclass, changing content type, etc.

• The JSP Page directive has the following attributes:– import, contentType, pageEncoding, session, isELIgnored,

buffer, autoFlush, info, errorPage, isThreadSafe, language & extends

Page 41: Web Engineering

Web Engineering (703512)

JSP Page Directive Attributes

• import=“java.util.*, java.sql.*”• contentType=“text/html; charset=ISO-8859-1”• pageEncoding=“Shift_JIS”• session=“true/false”• isELIgnored=“false/true”• buffer=“size in kb”• autoFlush=“true/false”• info=“Some info message.”• errorPage=“error.jsp”• isErrorPage=“false/true”• isThreadSafe=“true/false”• language=“java”• extends=“package.class”

Page 42: Web Engineering

Web Engineering (703512)

Including Files

• There are three ways of including external files into a JSP document.– <jsp:include …>…– <%@ include …>– <jsp:plugin …>…

Page 43: Web Engineering

Web Engineering (703512)

The jsp:include Action

• This includes the output of a secondary page at the time the main page is requested.

• The output of the sub page must be HTML generated by a servlet or JSP.

<jsp:include page=“/inc/header.jsp” flush=“true” />

<jsp:include page=“/inc/header.jsp” flush=“true”><jsp:param name=“paramName” value=“xyz”>

</jsp:include>

Page 44: Web Engineering

Web Engineering (703512)

The Include Directive

• This includes directive is used to include a file in the main JSP at the time of translation into a servlet.

• The code of the included file is added to that of the JSP document.

<%@ include page=“/inc/header.jsp” %>

Page 45: Web Engineering

Web Engineering (703512)

Forwarding Requests

• This action is used to get the output of a JSP file completely from another JSP or servlet.

• The output of the auxiliary JSP or servlet is sent to the client, not that of the current JSP.

<jsp:forward page=“xyz.jsp” />

Page 46: Web Engineering

Web Engineering (703512)

The jsp:plugin Action

• Used to embed a java applet into the generated output.

• Java applets are rarely used in web pages now a days.<jsp:plugin type=“applet”

code=“MyApplet.class”

width=“400” height=“300”>

</jsp:plugin>

Page 47: Web Engineering

Web Engineering (703512)

JAVA BEANS

47

Page 48: Web Engineering

Web Engineering (703512)

Java Beans

• What Are Beans?

• Beans are standard java objects.– Must have a zero-arguments constructor.– Should have no public fields.– Values should be accessed through method calls, getXxx,

setXxx & isXxx.

Page 49: Web Engineering

Web Engineering (703512)

Java Bean (example)

public class Person {private int age;private String name;… … …public void setAge(int age){

this.age = age;}public void setName(String name){

this.name = name;}public int getAge(){

return this.age;}public String getName(){

return this.name;}

… … …}

Page 50: Web Engineering

Web Engineering (703512)

Using Java Beans & JSP

• There are three main constructs to use Java Beans in JSP.– <jsp:useBean ……… />– <jsp:getProperty ……… />– <jsp:setProperty ……… />

Page 51: Web Engineering

Web Engineering (703512)

jsp:useBean

• Used to load a bean to be used in the JSP document.

Syntax:<jsp:useBean id=“name” class=“package.Class” />

Eg:<jsp:useBean id=“person” class=“iiitmk.Person” />

Equivalent to:<% iiitmk.Person person = new iiitmk.Person(); %>

Page 52: Web Engineering

Web Engineering (703512)

Getting bean properties

• Used to read properties from beans.

Syntax:<jsp:getProperty id=“name” property=“propName” />

Eg:<jsp:getProperty id=“person” property=“name” />

Equivalent to:<%= person.getName() %>

Page 53: Web Engineering

Web Engineering (703512)

Setting bean properties

• Used to set properties of beans.

Syntax:<jsp:setProperty id=“name” property=“propName” value=“propValue” />

Eg:<jsp:setProperty id=“person” property=“name” value=“Popeye The Sailor” />

Equivalent to:<% person.setName(“Popeye The Sailor”); %>

Page 54: Web Engineering

Web Engineering (703512)

Properties & Request Parameters

• The value of a bean property can be set directly from the value of the corresponding request parameter.

Syntax:<jsp:setProperty id=“name” property=“propName” param=“propName” />

Eg:<jsp:setProperty id=“person” property=“name” param=“name” /><jsp:setProperty id=“person” property=“*” />

Page 55: Web Engineering

Web Engineering (703512)

Sharing Beans (scope)

• The scope of a bean defines where the bean is stored and how it is accessible. By default it is accessible as a local variable. Other places of storing beans are the request, session and application.

Syntax:<jsp:useBean … … … scope=“…” />

Scopes:page, request, session & application

Page 56: Web Engineering

Web Engineering (703512)

Page Scope

• The default scope of a bean. Bean is bound to a local variable in the _jspService method and also placed in the pageContext predefined variable, accessible by calling getAttribute() method.

Syntax:<jsp:useBean … … … scope=“page” />

<jsp:useBean … … … />

Page 57: Web Engineering

Web Engineering (703512)

Request Scope

• In addition to being bound to a local variable, the bean is also placed in the HttpServletRequest object (request) for the duration of the current request.

• Accessible by getAttribute() method.

Syntax:<jsp:useBean … … … scope=“request” />

Page 58: Web Engineering

Web Engineering (703512)

Session Scope

• In addition to being bound to a local variable, the bean is also placed in the HttpSession object (session).

• Accessible by getAttribute() method.

Syntax:<jsp:useBean … … … scope=“session” />

Page 59: Web Engineering

Web Engineering (703512)

Application Scope

• In addition to being bound to a local variable, the bean is also placed in the ServletContext object (application). The servlet context is shared by all the JSP and servlets in the web application.

• Accessible by getAttribute() method.

Syntax:<jsp:useBean … … … scope=“application” />

Page 60: Web Engineering

Web Engineering (703512)

WRAP-UPThat’s almost all for day…

60

Page 61: Web Engineering

Web Engineering (703512)

Bibliography

• Mandatory reading– Java Servlet Tutorial

• http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/Servlets.html

– Java Server Pages Tutorial• http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/JSPIntro.html

– Java Beans Tutorial• http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/JSPBeans.html

61

Page 62: Web Engineering

Web Engineering (703512)

Tools

• Apache Tomcat– http://tomcat.apache.org/

• Eclipse– http://www.eclipse.org/

• Eclipse Web Tools– http://www.eclipse.org/webtools/

62

Page 63: Web Engineering

Web Engineering (703512)

Next Lecture

63

# Date Title Lecturer

1 7th Oct Web Engineering Introduction and Overview F. M. Facca

2 14h Oct Collection Requirements for Web Applications F. M. Facca

3 21st Oct Web Application Modeling F. M. Facca

4 28th Oct Developing Applications with WebML F. M. Facca

5 4th Nov Web Application Architectures I F. M. Facca

6 11th Nov Web Application Architectures II F. M. Facca

7 18th Nov Testing and Usability on the Web F. M. Facca

8 25th Nov Mid Term Exam F. M. Facca

9 2nd Dec Web Technologies I F. M. Facca

10 9th Dec Web Technologies II F. M. Facca

11 16th Dec Web Technologies III F. M. Facca

12 13th Jan Web 2.0 Mash-ups F. Daniel (UNITN)

13 20th Jan Web Application Development Process/Project Management for Web Applications

F. M. Facca

14 27th Jan Final Exam F. M. Facca

Page 64: Web Engineering

Web Engineering (703512)

Questions?

64