Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "© Copyright 2008 STI - INNSBRUCK www.sti-innsbruck.at Web Engineering Web Technologies II Lecture X – 9 th December 2008 Federico M. Facca."— Presentation transcript:

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

2 Web Engineering (703512) Where are we? #DateTitleLecturer 17 th OctWeb Engineering Introduction and OverviewF. M. Facca 214 h OctCollection Requirements for Web ApplicationsF. M. Facca 321 st OctWeb Application ModelingF. M. Facca 428 th OctDeveloping Applications with WebMLF. M. Facca 54 th NovWeb Application Architectures IF. M. Facca 611 th NovWeb Application Architectures IIF. M. Facca 718 th NovTesting and Usability on the WebF. M. Facca 825 th NovMid Term ExamF. M. Facca 92 nd DecWeb Technologies IF. M. Facca 109 th DecWeb Technologies IIF. M. Facca 1116 th DecWeb Technologies IIIF. M. Facca 1213 th JanWeb 2.0 Mash-upsF. Daniel (UNITN) 1320 th JanWeb Application Development Process/ Project Management for Web Applications F. M. Facca 1427 th JanFinal ExamF. M. Facca 2

3 Web Engineering (703512) Overview Servlet JSP Java Beans Wrap-up 3

4 Web Engineering (703512) SERVLETS 4

5 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

6 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

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

8 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

9 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

10 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

11 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 HelloW HelloWorld HelloW hello

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

13 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

14 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

15 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

16 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

17 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(“ ”); … out.println(“ ”); 17

18 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.append(“ "); … String s= html.toString(); This is very useful when generating HTML page strings

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

20 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

21 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 doPost Post Get User Input

22 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

23 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

24 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

25 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

26 Web Engineering (703512) JAVA SERVER PAGES 26

27 Web Engineering (703512) What is JSP Servlets – HTML in java code JSP – java code in HTML Java Server Pages JSP

28 Web Engineering (703512) JSP Lifecycle JSP to Servlet Translation JSP to Servlet Translation Servlet Compiled Servlet Compiled Servlet Loaded Servlet Loaded jspInit() called jspInit() called _jspService() called _jspService() called

29 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 June 1, 2015

30 Web Engineering (703512) The benefits of JSP Easier to write and maintain HTML Can use standard HTML tools Can divide up development team

31 Web Engineering (703512) Advantages The Java advantage Extensive API Easy to learn Big development community Standardization & server support

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

33 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 –Scriptlets –Declarations

34 Web Engineering (703512) JSP Expressions A JSP expression is used to insert java code directly into the output. Syntax Eg: Current Time: Output: Current Time: Tue Aug 22 21:05:47 IST 2006 The expression is evaluated, converted to string and inserted into the page.

35 Web Engineering (703512)June 1, 2015 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 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: Eg: <% String str = request.getParameter(“name”); out.print(“Name : ”+str); %> June 1, 2015

37 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: Eg: <%! private String getMessage(){ return “This is a simple message!!”; } %>

38 Web Engineering (703512) XML Syntax XML like syntax for JSP expression, scriptlet & declaration – … Supported by JSP versio 1.2 & above These are case sensitive, should be in lowercase

39 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: – There are three types: –page, include & taglib

40 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

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

42 Web Engineering (703512) Including Files There are three ways of including external files into a JSP document. – … – – …

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

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

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

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

47 Web Engineering (703512) JAVA BEANS 47

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

49 Web Engineering (703512) Java Bean (example) public class Person { private int age; private Stringname; … … … 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 Web Engineering (703512) Using Java Beans & JSP There are three main constructs to use Java Beans in JSP. –

51 Web Engineering (703512) jsp:useBean Used to load a bean to be used in the JSP document. Syntax: Eg: Equivalent to:

52 Web Engineering (703512) Getting bean properties Used to read properties from beans. Syntax: Eg: Equivalent to:

53 Web Engineering (703512) Setting bean properties Used to set properties of beans. Syntax: Eg: Equivalent to:

54 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: Eg:

55 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: Scopes: page, request, session & application

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

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

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

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

60 Web Engineering (703512) WRAP-UP That’s almost all for day… 60

61 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

62 Web Engineering (703512) Tools Apache Tomcat –http://tomcat.apache.org/http://tomcat.apache.org/ Eclipse –http://www.eclipse.org/http://www.eclipse.org/ Eclipse Web Tools –http://www.eclipse.org/webtools/ 62

63 Web Engineering (703512) Next Lecture 63 #DateTitleLecturer 17 th OctWeb Engineering Introduction and OverviewF. M. Facca 214 h OctCollection Requirements for Web ApplicationsF. M. Facca 321 st OctWeb Application ModelingF. M. Facca 428 th OctDeveloping Applications with WebMLF. M. Facca 54 th NovWeb Application Architectures IF. M. Facca 611 th NovWeb Application Architectures IIF. M. Facca 718 th NovTesting and Usability on the WebF. M. Facca 825 th NovMid Term ExamF. M. Facca 92 nd DecWeb Technologies IF. M. Facca 109 th DecWeb Technologies IIF. M. Facca 1116 th DecWeb Technologies IIIF. M. Facca 1213 th JanWeb 2.0 Mash-upsF. Daniel (UNITN) 1320 th JanWeb Application Development Process/ Project Management for Web Applications F. M. Facca 1427 th JanFinal ExamF. M. Facca

64 Web Engineering (703512) Questions? 64


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

Similar presentations


Ads by Google