32
5/21/2013 1 © Copyright 2013 Ioan Toma & Srdjan Komazec Web Engineering Web Technologies II 2 Where we are? # Date Title 1 5 th March Web Engineering Introduction and Overview 2 12 th March Requirements Engineering for Web Applications 3 19 th March Web Application Modeling 4 9 th April Web Application Architectures 5 16 th April Developing Applications with WebML 6 23 rd April Testing and Usability of Web Applications 7 30 th April Maintenance and Performance of Web Applications 8 7 th May Web Technologies I 9 14 th May Web Technologies II 10 21 st May Web Application Development Process 11 28 th May Project Management for Web Applications 12 4 th June Web Application Security 13 11 th June Mobile Application Development I 14 18 th June Mobile Application Development II 14 25 th June Final Exam

Web Technologies II - STI Innsbruck · 10 Servlet Mapping • Servlet class needs to be mapped to an accessible URI (mainly through HTTP) • For convenience, a servlet can be accessed

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Web Technologies II - STI Innsbruck · 10 Servlet Mapping • Servlet class needs to be mapped to an accessible URI (mainly through HTTP) • For convenience, a servlet can be accessed

5/21/2013

1

1© Copyright 2013  Ioan Toma & Srdjan Komazec

Web Engineering

Web Technologies II

2

Where we are?

# Date Title

1 5th March Web Engineering Introduction and Overview

2 12th March Requirements Engineering for Web Applications

3 19th March Web Application Modeling

4 9th April Web Application Architectures

5 16th April Developing Applications with WebML

6 23rd April Testing and Usability of Web Applications

7 30th April Maintenance and Performance of Web Applications 

8 7th May Web Technologies I

9 14th May Web Technologies II

10 21st May Web Application Development Process

11 28th May Project Management for Web Applications

12 4th June Web Application Security

13 11th June Mobile Application Development I

14 18th June Mobile Application Development II

14 25th June Final Exam

Page 2: Web Technologies II - STI Innsbruck · 10 Servlet Mapping • Servlet class needs to be mapped to an accessible URI (mainly through HTTP) • For convenience, a servlet can be accessed

5/21/2013

2

3

Overview

• Servlet

• JSP

• Java Beans

• Summary

4

SERVLETS

Page 3: Web Technologies II - STI Innsbruck · 10 Servlet Mapping • Servlet class needs to be mapped to an accessible URI (mainly through HTTP) • For convenience, a servlet can be accessed

5/21/2013

3

5

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”

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 4: Web Technologies II - STI Innsbruck · 10 Servlet Mapping • Servlet class needs to be mapped to an accessible URI (mainly through HTTP) • For convenience, a servlet can be accessed

5/21/2013

4

7

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

{ … }

}

8

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 5: Web Technologies II - STI Innsbruck · 10 Servlet Mapping • Servlet class needs to be mapped to an accessible URI (mainly through HTTP) • For convenience, a servlet can be accessed

5/21/2013

5

9

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

10

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

Page 6: Web Technologies II - STI Innsbruck · 10 Servlet Mapping • Servlet class needs to be mapped to an accessible URI (mainly through HTTP) • For convenience, a servlet can be accessed

5/21/2013

6

11

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

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

12

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 7: Web Technologies II - STI Innsbruck · 10 Servlet Mapping • Servlet class needs to be mapped to an accessible URI (mainly through HTTP) • For convenience, a servlet can be accessed

5/21/2013

7

13

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

14

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, …

Page 8: Web Technologies II - STI Innsbruck · 10 Servlet Mapping • Servlet class needs to be mapped to an accessible URI (mainly through HTTP) • For convenience, a servlet can be accessed

5/21/2013

8

15

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 … )

}

16

Response Processing

• Response body– Generating HTML or other content for display

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

Page 9: Web Technologies II - STI Innsbruck · 10 Servlet Mapping • Servlet class needs to be mapped to an accessible URI (mainly through HTTP) • For convenience, a servlet can be accessed

5/21/2013

9

17

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>”);

18

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 10: Web Technologies II - STI Innsbruck · 10 Servlet Mapping • Servlet class needs to be mapped to an accessible URI (mainly through HTTP) • For convenience, a servlet can be accessed

5/21/2013

10

19

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 browser– <p>, <br>, …

20

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!)

Page 11: Web Technologies II - STI Innsbruck · 10 Servlet Mapping • Servlet class needs to be mapped to an accessible URI (mainly through HTTP) • For convenience, a servlet can be accessed

5/21/2013

11

21

Getting User Data

• Two ways– URL parameter (Get)

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

– HTML Form (Get or Post)

Servlet

HTML Form

URL parameter

doGet

doPostPost

User Input

22

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, …

Page 12: Web Technologies II - STI Innsbruck · 10 Servlet Mapping • Servlet class needs to be mapped to an accessible URI (mainly through HTTP) • For convenience, a servlet can be accessed

5/21/2013

12

23

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

24

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 13: Web Technologies II - STI Innsbruck · 10 Servlet Mapping • Servlet class needs to be mapped to an accessible URI (mainly through HTTP) • For convenience, a servlet can be accessed

5/21/2013

13

25

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()

26

JAVA SERVER PAGES

Page 14: Web Technologies II - STI Innsbruck · 10 Servlet Mapping • Servlet class needs to be mapped to an accessible URI (mainly through HTTP) • For convenience, a servlet can be accessed

5/21/2013

14

27

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>

28

JSP Lifecycle

JSP to ServletTranslation

ServletCompiled

ServletLoaded

jspInit()called

_jspService()called

Page 15: Web Technologies II - STI Innsbruck · 10 Servlet Mapping • Servlet class needs to be mapped to an accessible URI (mainly through HTTP) • For convenience, a servlet can be accessed

5/21/2013

15

29

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

30

The benefits of JSP

• Easier to write and maintain HTML

• Can use standard HTML tools

• Can divide up development team

Page 16: Web Technologies II - STI Innsbruck · 10 Servlet Mapping • Servlet class needs to be mapped to an accessible URI (mainly through HTTP) • For convenience, a servlet can be accessed

5/21/2013

16

31

Advantages

• The Java advantage

• Extensive API

• Easy to learn

• Big development community

• Standardization & server support

32

Location of JSP pages

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

Page 17: Web Technologies II - STI Innsbruck · 10 Servlet Mapping • Servlet class needs to be mapped to an accessible URI (mainly through HTTP) • For convenience, a servlet can be accessed

5/21/2013

17

33

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

34

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 May 21 09:05:47 IST 2013

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

Page 18: Web Technologies II - STI Innsbruck · 10 Servlet Mapping • Servlet class needs to be mapped to an accessible URI (mainly through HTTP) • For convenience, a servlet can be accessed

5/21/2013

18

35

May 21, 2013

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”

36

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

%>

Page 19: Web Technologies II - STI Innsbruck · 10 Servlet Mapping • Servlet class needs to be mapped to an accessible URI (mainly through HTTP) • For convenience, a servlet can be accessed

5/21/2013

19

37

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() %>

38

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 version 1.2 & above

• These are case sensitive, should be in lowercase

Page 20: Web Technologies II - STI Innsbruck · 10 Servlet Mapping • Servlet class needs to be mapped to an accessible URI (mainly through HTTP) • For convenience, a servlet can be accessed

5/21/2013

20

39

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

40

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 21: Web Technologies II - STI Innsbruck · 10 Servlet Mapping • Servlet class needs to be mapped to an accessible URI (mainly through HTTP) • For convenience, a servlet can be accessed

5/21/2013

21

41

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”

42

Including Files

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

– <%@ include …>

– <jsp:plugin …>…

Page 22: Web Technologies II - STI Innsbruck · 10 Servlet Mapping • Servlet class needs to be mapped to an accessible URI (mainly through HTTP) • For convenience, a servlet can be accessed

5/21/2013

22

43

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>

44

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 23: Web Technologies II - STI Innsbruck · 10 Servlet Mapping • Servlet class needs to be mapped to an accessible URI (mainly through HTTP) • For convenience, a servlet can be accessed

5/21/2013

23

45

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

46

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 24: Web Technologies II - STI Innsbruck · 10 Servlet Mapping • Servlet class needs to be mapped to an accessible URI (mainly through HTTP) • For convenience, a servlet can be accessed

5/21/2013

24

47

JAVA BEANS

48

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 25: Web Technologies II - STI Innsbruck · 10 Servlet Mapping • Servlet class needs to be mapped to an accessible URI (mainly through HTTP) • For convenience, a servlet can be accessed

5/21/2013

25

49

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;}… … …

}

50

Using Java Beans & JSP

• There are three main constructs to use Java Beans in JSP.

– <jsp:useBean ……… />

– <jsp:getProperty ……… />

– <jsp:setProperty ……… />

Page 26: Web Technologies II - STI Innsbruck · 10 Servlet Mapping • Servlet class needs to be mapped to an accessible URI (mainly through HTTP) • For convenience, a servlet can be accessed

5/21/2013

26

51

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

52

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 27: Web Technologies II - STI Innsbruck · 10 Servlet Mapping • Servlet class needs to be mapped to an accessible URI (mainly through HTTP) • For convenience, a servlet can be accessed

5/21/2013

27

53

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

54

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 28: Web Technologies II - STI Innsbruck · 10 Servlet Mapping • Servlet class needs to be mapped to an accessible URI (mainly through HTTP) • For convenience, a servlet can be accessed

5/21/2013

28

55

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

56

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 29: Web Technologies II - STI Innsbruck · 10 Servlet Mapping • Servlet class needs to be mapped to an accessible URI (mainly through HTTP) • For convenience, a servlet can be accessed

5/21/2013

29

57

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

58

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 30: Web Technologies II - STI Innsbruck · 10 Servlet Mapping • Servlet class needs to be mapped to an accessible URI (mainly through HTTP) • For convenience, a servlet can be accessed

5/21/2013

30

59

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

60

WRAP-UPThat’s almost all for day…

Page 31: Web Technologies II - STI Innsbruck · 10 Servlet Mapping • Servlet class needs to be mapped to an accessible URI (mainly through HTTP) • For convenience, a servlet can be accessed

5/21/2013

31

61

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

62

Tools

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

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

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

Page 32: Web Technologies II - STI Innsbruck · 10 Servlet Mapping • Servlet class needs to be mapped to an accessible URI (mainly through HTTP) • For convenience, a servlet can be accessed

5/21/2013

32

63

Next Lecture

# Date Title

1 5th March Web Engineering Introduction and Overview

2 12th March Requirements Engineering for Web Applications

3 19th March Web Application Modeling

4 9th April Web Application Architectures

5 16th April Developing Applications with WebML

6 23rd April Testing and Usability of Web Applications

7 30th April Maintenance and Performance of Web Applications 

8 7th May Web Technologies I

9 14th May Web Technologies II

10 21st May Web Application Development Process

11 28th May Project Management for Web Applications

12 4th June Web Application Security

13 11th June Mobile Application Development I

14 18th June Mobile Application Development II

14 25th June Final Exam

64

Questions?