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

Slides:



Advertisements
Similar presentations
8 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: JavaServer Pages.
Advertisements

Java Server Pages (JSP)
JSP1 Java Server Pages (JSP) Introducing JavaServer Pages TM (JSP TM ) JSP scripting elements.
 Copyright Wipro Technologies JSP Ver 1.0 Page 1 Talent Transformation Java Server Pages.
Chapter 51 Scripting With JSP Elements JavaServer Pages By Xue Bai.
Java Servlet & JSP © copyright 2005 SNU OOPSLA Lab.
JSP – Java Server Pages Part 1 Representation and Management of Data on the Internet.
WEB1P servintro1 Introduction to servlets and JSP Dr Jim Briggs.
18-Jun-15 JSP Java Server Pages Reference: Tutorial/Servlet-Tutorial-JSP.html.
JSP Java Server Pages Reference:
JSP Architecture  JSP is a simple text file consisting of HTML or XML content along with JSP elements  JSP packages define the interface for the compiled.
Servlets Compiled by Dr. Billy B. L. Lim. Servlets Servlets are Java programs which are invoked to service client requests on a Web server. Servlets extend.
Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.
Gayle J Yaverbaum, PhD Professor of Information Systems Penn State Harrisburg.
1 CIS336 Website design, implementation and management (also Semester 2 of CIS219, CIS221 and IT226) Lecture 9 JavaServer Pages (JSP) (Based on Møller.
CSC 2720 Building Web Applications Using Java Beans, Custom Tags and Tag Libraries in JSP pages.
1 JSP – Java Server Pages Representation and Management of Data on the Internet.
Introduction to JSP Based on: Marty Hall, Larry Brown, Core Servlets and JavaServer Pages.
AN OVERVIEW OF SERVLET TECHNOLOGY SERVER SETUP AND CONFIGURATION WEB APPLICATION STRUCTURE BASIC SERVLET EXAMPLE Java Servlets - Compiled By Nitin Pai.
CSC 2720 Building Web Applications
Introduction to Java Server Pages (JSPs) Robert Thornton.
Introduction to JavaServer Pages (JSP) Slides from Dr. Mark Llewellyn.
Java Server Pages Lecture July Java Server Pages Java Server Pages (JSPs) provide a way to separate the generation of dynamic content (java)
Jordan Anastasiade. All rights reserved.
 Embeds Java code  In HTML tags  When used well  Simple way to generate dynamic web-pages  When misused (complex embedded Java)  Terribly messy.
Web Server Programming 1. Nuts and Bolts. Premises of Course Provides general introduction, no in-depth training Assumes some HTML knowledge Assumes some.
Stanisław Osiński, 2002JSP – A technology for serving dynamic web content Java Server Pages™ A technology for serving dynamic web content Stanisław Osiński,
JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA James Faeldon CS 119 Enterprise Systems Programming.
Slides © Marty Hall, book © Sun Microsystems Press 1 JSP Scripting Elements Core Servlets & JSP book:
Java Servlets & Java Server Pages Lecture July 2013.
Introduction to JavaServer Pages. 2 JSP and Servlet Limitations of servlet  It’s inaccessible to non-programmers JSP is a complement to servlet  focuses.
SE-2840 Dr. Mark L. Hornick 1 Java Server Pages. HTML/JSPs are intended to be used as the views in an MVC- based web application Model – represents an.
JSP Fundamentals Elements of a JSP Using Beans with JSP Integrating Servlets and JSP.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 27 JavaServer Page.
CSC 2720 Building Web Applications JavaServer Pages (JSP) The Basics.
Copyright © 2002 ProsoftTraining. All rights reserved. JavaServer Pages.
Java Server Pages (JSP)
Java Server Pages An introduction to JSP. Containers and Components Several clients – one system.
CS-4220 Dr. Mark L. Hornick 1 Java Server Pages. HTML/JSPs are intended to be used as the views in an MVC- based web application Model – represents an.
Chapter 11 Invoking Java Code with JSP Scripting Elements.
CSC 2720 Building Web Applications JavaServer Pages (JSP) JSP Directives and Action Elements.
JSP Pages. What and Why of JSP? JSP = Java code imbedded in HTML or XML –Static portion of the page is HTML –Dynamic portion is Java Easy way to develop.
JSP. Types of JSP Scripting Elements Expressions of the form, which are evaluated and inserted into the servlet's output. Scriptlets of the form, which.
Web Technologies Java Beans & JSP By Praveen Kumar G.
Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection.
JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.
CSI 3125, Preliminaries, page 1 SERVLET. CSI 3125, Preliminaries, page 2 SERVLET A servlet is a server-side software program, Responds oriented other.
1 Introduction to Servlets. Topics Web Applications and the Java Server. HTTP protocol. Servlets 2.
Basic JSP Celsina Bignoli Problems with Servlets Servlets contain –request processing, –business logic –response generation all lumped.
CSI 3125, Preliminaries, page 1 SERVLET. CSI 3125, Preliminaries, page 2 SERVLET A servlet is a server-side software program, written in Java code, that.
COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 3 1COMP9321, 15s2, Week.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 43 JavaServer Page.
HTTP protocol Java Servlets. HTTP protocol Web system communicates with end-user via HTTP protocol HTTP protocol methods: GET, POST, HEAD, PUT, OPTIONS,
1 Java Server Pages A Java Server Page is a file consisting of HTML or XML markup into which special tags and code blocks are inserted When the page is.
1 Chapter 27 JavaServer Page. 2 Objectives F To know what is a JSP page is processed (§27.2). F To comprehend how a JSP page is processed (§27.3). F To.
INVOKING JAVA CODE WITH JSP SCRIPTING ELEMENTS. Creating Template Text A large percentage of your JSP document consists of static text (usually HTML),
 Java Server Pages (JSP) By Offir Golan. What is JSP?  A technology that allows for the creation of dynamically generated web pages based on HTML, XML,
JSP JavaServer Pages. What is JSP? Java based technology that simplifies the development of dynamic web sites JSP pages are HTML pages with embedded code.
Java Programming: Advanced Topics 1 Building Web Applications Chapter 13.
Java Server Pages. 2 Servlets The purpose of a servlet is to create a Web page in response to a client request Servlets are written in Java, with a little.
Chapter 14 Using JavaBeans Components in JSP Documents.
Bayu Priyambadha, S.Kom. Static content  Web Server delivers contents of a file (html) 1. Browser sends request to Web Server 3. Web Server sends HTML.
Developing JavaServer Pages
Java Servlets By: Tejashri Udavant..
Java Server Pages.
Java Server Pages By: Tejashri Udavant..
Java Servlets.
Introduction to Java Servlets
Introduction to JSP Dept. of B.Voc Software Development and System Administration St. Joseph’s College(Autonomous) Trichy-02 By Dr. J. Ronald Martin Introduction.
Web Technologies Java Beans & JSP
Presentation transcript:

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

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

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

Web Engineering (703512) SERVLETS 4

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

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

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

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

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

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) – – Specific mapping: using the configuration file “web.xml” –A servlet is specifically mapped to a user defined URL 10

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: –Invoker class mapping –Specific mapping –For more mapping examples, see example “web.xml” 11 HelloW HelloWorld HelloW hello

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)

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

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

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

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

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

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

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

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

Web Engineering (703512) Getting User Data Two ways –URL parameter (Get) …/somefile?p1=value1&p2=value2 –HTML Form (Get or Post) 21 Servlet HTML Form URL parameter doGet doPost Post Get User Input

Web Engineering (703512) URL with Parameters User data can be sent with “get” request – …/somefile?p1=value1&p2=value2 Applications –Template page for data item details –Page content selection –User input –Conditions/configuration –Page display/format, page content, error message, … 22

Web Engineering (703512) Handling “Get” Parameter In HTTP “get”, user data are sent with URL – 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

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

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

Web Engineering (703512) JAVA SERVER PAGES 26

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

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

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

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

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

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.

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

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.

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”

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

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!!”; } %>

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

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

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

Web Engineering (703512) JSP Page Directive Attributes import=“java.util.*, java.sql.*” contentType=“text/html; charset=ISO ” 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”

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

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.

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.

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.

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

Web Engineering (703512) JAVA BEANS 47

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.

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

Web Engineering (703512) Using Java Beans & JSP There are three main constructs to use Java Beans in JSP. –

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

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

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

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:

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

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:

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:

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:

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:

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

Web Engineering (703512) Bibliography Mandatory reading –Java Servlet Tutorial –Java Server Pages Tutorial –Java Beans Tutorial 61

Web Engineering (703512) Tools Apache Tomcat – Eclipse – Eclipse Web Tools – 62

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

Web Engineering (703512) Questions? 64