JavaServer Pages. What is JavaServer Pages?

Similar documents
Java Server Page (JSP)

COMP9321 Web Application Engineering

COMP9321 Web Application Engineering

COMP9321 Web Application Engineering

PES INSTITUTE OF TECHNOLOGY, SOUTH CAMPUS DEPARTMENT OF MCA INTERNAL TEST (SCHEME AND SOLUTION) II

Introduction to Java Server Pages. Enabling Technologies - Plug-ins Scripted Pages

Fast Track to Java EE 5 with Servlets, JSP & JDBC

JSP - SYNTAX. Any text, HTML tags, or JSP elements you write must be outside the scriptlet. Following is the simple and first example for JSP:

JavaServer Pages (JSP)

directive attribute1= value1 attribute2= value2... attributen= valuen %>

Java Server Pages. JSP Part II

Introduction to JSP and Servlets Training 5-days

JSP - ACTIONS. There is only one syntax for the Action element, as it conforms to the XML standard:

JSP MOCK TEST JSP MOCK TEST IV

Unit 5 JSP (Java Server Pages)

1. Introduction. 2. Life Cycle Why JSP is preferred over Servlets? 2.1. Translation. Java Server Pages (JSP) THETOPPERSWAY.

Université du Québec à Montréal

This course is intended for Java programmers who wish to write programs using many of the advanced Java features.

CSc31800: Internet Programming, CS-CCNY, Spring 2004 Jinzhong Niu May 9, JSPs 1

PSD1B Advance Java Programming Unit : I-V. PSD1B- Advance Java Programming

Scope and State Handling in JSP

a. Jdbc:ids://localhost:12/conn?dsn=dbsysdsn 21. What is the Type IV Driver URL? a. 22.

01KPS BF Progettazione di applicazioni web

Fast Track to Java EE

A JavaBean is a class file that stores Java code for a JSP

Java E-Commerce Martin Cooke,

JSP. Basic Elements. For a Tutorial, see:

20/08/56. Java Technology, Faculty of Computer Engineering, KMITL 1

Advance Java. Configuring and Getting Servlet Init Parameters per servlet

A Gentle Introduction to Java Server Pages

JAVA 2 ENTERPRISE EDITION (J2EE)

112. Introduction to JSP

Sun Sun Certified Web Component Developer for J2EE 5 Version 4.0

112-WL. Introduction to JSP with WebLogic

Table of Contents. Introduction... xxi

Principles and Techniques of DBMS 6 JSP & Servlet

Unit 4 Java Server Pages

CS506 Web Design & Development Final Term Solved MCQs with Reference

Module 5 Developing with JavaServer Pages Technology

Specialized - Mastering JEE 7 Web Application Development

COMP201 Java Programming

CE212 Web Application Programming Part 3

JavaServer Pages. Juan Cruz Kevin Hessels Ian Moon

Session 12. JSP Tag Library (JSTL) Reading & Reference

ADVANCED JAVA COURSE CURRICULUM

Java Server Pages, JSP

Islamic University of Gaza Faculty of Engineering Department of Computer Engineering ECOM Advanced Internet Technology Lab.

JSP CSCI 201 Principles of Software Development

Web applications and JSP. Carl Nettelblad

Module 3 Web Component

Java Server Pages JSP

More JSP. Advanced Topics in Java. Khalid Azim Mughal Version date: ATIJ More JSP 1/42

Trabalhando com JavaServer Pages (JSP)

6- JSP pages. Juan M. Gimeno, Josep M. Ribó. January, 2008

ADVANCED JAVA TRAINING IN BANGALORE

GUJARAT TECHNOLOGICAL UNIVERSITY

A.1 JSP A.2 JSP JSP JSP. MyDate.jsp page contenttype="text/html; charset=windows-31j" import="java.util.calendar" %>

Advanced Java Programming

C H A P T E RJSP.2. JSP.2.1 Syntax of expressions in JSP pages: ${} vs #{}

SNS COLLEGE OF ENGINEERING, Coimbatore

Basic Principles of JSPs

LTBP INDUSTRIAL TRAINING INSTITUTE

About the Authors. Who Should Read This Book. How This Book Is Organized

Trabalhando com JavaServer Pages (JSP)

Advantage of JSP over Servlet

Servlet and JSP Review

DVS WEB INFOTECH DEVELOPMENT TRAINING RESEARCH CENTER

Ch04 JavaServer Pages (JSP)

UNIT -5. Java Server Page

Component Based Software Engineering

Modernizing Java Server Pages By Transformation. S h a n n o n X u T h o m a s D e a n Q u e e n s U n i v e r s i t y

FINALTERM EXAMINATION Spring 2009 CS506- Web Design and Development Solved by Tahseen Anwar

LTBP INDUSTRIAL TRAINING INSTITUTE

COMP9321 Web Application Engineering

COMP9321 Web Application Engineering

Database. Request Class. jdbc. Servlet. Result Bean. Response JSP. JSP and Servlets. A Comprehensive Study. Mahesh P. Matha

Peers Techno log ies Pv t. L td. Core Java & Core Java &Adv Adv Java Java

First Simple Interactive JSP example

Java.. servlets and. murach's TRAINING & REFERENCE 2ND EDITION. Joel Murach Andrea Steelman. IlB MIKE MURACH & ASSOCIATES, INC.

Building Web Applications With The Struts Framework

Oracle 10g: Build J2EE Applications

Experiment No: Group B_2

Contents at a Glance

What's New in the Servlet and JSP Specifications

JSP Scripting Elements

/smlcodes /smlcodes /smlcodes JSP. Java Server Pages. Small Codes. Programming Simplified. A SmlCodes.Com Small presentation

Enterprise Computing with Java MCA-305 UNIT II. Learning Objectives. JSP Basics. 9/17/2013MCA-305, Enterprise Computing in Java

JAVA MICROSERVICES. Java Language Environment. Java Set Up. Java Fundamentals. Packages. Operations

Exercise. (1) Which of the following can not be used as the scope when using a JavaBean with JSP? a. application b. session c. request d.

AN ISO 9001:2008 CERTIFIED COMPANY ADVANCED. Java TRAINING.

Integrating Servlets and JavaServer Pages Lecture 13

Courses For Event Java Advanced Summer Training 2018

Oracle 1z Java Enterprise Edition 5 Web Component Developer Certified Professional Exam. Practice Test. Version:

JSP: Servlets Turned Inside Out

Call us: /

Vision of J2EE. Why J2EE? Need for. J2EE Suite. J2EE Based Distributed Application Architecture Overview. Umair Javed 1

sessionx Desarrollo de Aplicaciones en Red EL (2) EL (1) Implicit objects in EL Literals José Rafael Rojano Cáceres

XML and XSLT. XML and XSLT 10 February

Welcome To PhillyJUG. 6:30-7:00 pm - Network, eat, find a seat 7:00-7:15 pm - Brief announcements 7:15-8:30 pm - Tom Janofsky's presentation

11.1 Introduction to Servlets

Transcription:

JavaServer Pages SWE 642, Fall 2008 Nick Duan What is JavaServer Pages? JSP is a server-side scripting language in Java for constructing dynamic web pages based on Java Servlet, specifically it contains three types vocabularies A set of special tags for interfacing with the Java language An expression language (EL) for accessing server-side JavaBeans and built-in objects An tag extension mechanism to the define customized tags An XML document containing a combination of HTML/JavaScripts, CSS, and JSP elements Any difference between JSP and JavaScripts? 10/28/2008 Nick Duan 2 1

Directive Sample JSP Action <%@ page info= Samle JSP for SWE642 %> <jsp:usebean id= mybean" class= edu.gmu.beanclass"/> <jsp:setproperty name= mybean" property= "*"/> <html> <body> <%@ import file= header.html %> Directive Scriptlet <% if (request.getparameter("name")==null ) { %> <h1>user Info Request Form</h1> <form method= get" action="/process.jsp"> <br> Name: <input type="text" name= "name > <p> <input type="submit" value= Submit"> </form> <% } else { %> <p> <b>user Name provided</b>: <p> Name: <jsp:getproperty name= mybean" property="name"/> <% } %> </body></html> Action August 21, 2008 Nick Duan 3 JSP Lifecycle Hello.jsp 1. Request 6. Response JSP Container J2EE Server 3. Generate 4. Compile Hello_jsp.java Follow the servlet lifecycle here Hello_jsp.class If JSP is already compiled, compilation is skipped 10/28/2008 Nick Duan 4 2

Types of JSP Elements JSP Element Type Syntax Comment <%-- my comments --%> Declaration <%! int count=0; %> Directives <%@ include file= myjsp.jsp %> <%@ page {attribute= value }* %> <%@ taglib uri= http://me.com prefix= me %> Expression <%= variable %> Scriptlet <% String mystring= mystring ; %> Actions <jsp:usebean {attribute= value }* /> August 21, 2008 Nick Duan 5 Page Directives Defines one of more page-dependent properties Commonly used attributes of @ page import = a java class to this page extends = a base class for this page errorpage = error page url contenttype = MINE type of this page pageencoding = character encoding of this page August 21, 2008 Nick Duan 6 3

Standard Actions in JSP XML elements with the prefix jsp, predefined by the JSP engine Standard Actions <jsp:usebean /> Actions for Dealing with Java Beans <jsp: setproperty /> <jsp: getproperty /> <jsp: param /> <jsp: include /> <jsp: forward /> <jsp: plugin /> <jsp:params /> <jsp:element /> <jsp:attribute /> <jsp:text /> 10/28/2008 Nick Duan 7 What is a JavaBean? A Java class with standard naming conventions for data members (properties) and public access methods (getters/setters) Declared as a public class with default constructor First introduced in JDK 1.0 with different types of properties (simple, indexed, change, vetoable) and introspection APIs Java Beans with simple properties are most commonly used Sometimes it may refer to any Java class 10/28/2008 Nick Duan 8 4

Sample Java Bean Definition public class User { } private String name; private String phonenumber; public String getname() { return name; } public void setname(string val) { name=val; } public String getphonenumber () { return phonenum; } public void setphonenumber(string val) { phonenumber=val; } August 21, 2008 Nick Duan 9 The usebean Action Create a Java Bean object within the page or reference an existing one in request or session object Syntax <jsp:usebean id= bname scope= page request session application class= edu.gmu.swe642.user /> id Variable name of the Java Bean object to be used throughout the page An usebean action to be defined first before any setproperty and getproperty action in a JSP 10/28/2008 Nick Duan 10 5

The setproperty Action setproperty sets a value of a data member in a Java Bean object Performs the corresponding setter function of the bean <jsp:setproperty name= bname" property= name" value= Charlie Brown"/> <jsp:setproperty name= bname" property="*" /> Set the bean properties with values of request parameters with identical names 10/28/2008 Nick Duan 11 The getproperty Action getproperty performs the corresponding getter method of the bean and output the result to the jspwriter <jsp:getproperty name= bname" property= phonenumber"/> Equivalent to the jsp expression: <%=bname.getphonenumber()%> bname must have been defined as an id in a previous usebean action August 21, 2008 Nick Duan 12 6

The forward & include Action Similar to the forward and include method of the servlet dispatcher (What s the difference between forward and include?) Syntax <jsp:include page= yours.jsp" flush= true false /> <jsp:forward page= yours.jsp /> <jsp:include page= yours.jsp" flush= true false > {<jsp:param name= name value= val />}* </jsp:include> <jsp:forward page= yours.jsp > {<jsp:param name= name value= val />}* </jsp:forward> 10/28/2008 Nick Duan 13 Implicit Objects in JSP Predefined variables for standard Servlet/JSP objects request HttpServletRequest response HttpServletResponse session HttpSession application ServletContext config ServletConfig out JspWriter Example Current session ID: <%=session.getid()%> August 21, 2008 Nick Duan 14 7

Unified Expression Language A generic scripting technology not limited to JSP (also used in JSF) Functionalities built into the JSP engine Direct access to JavaBeans/various data structures (array, enum and collections), and implicit objects via various resolvers Allow direct data mapping to/from JavaBeans (or bean-like) data objects Invoke static/public methods Built-in string and arithmetic operations August 21, 2008 Nick Duan 15 EL Examples ${expr} immediate evaluation #{expr} deferred evaluation (used mostly in JSF technology) ${employee.name} JavaBeans, Java enum, Map ${array[0]} array ${employee[ name ]} JavaBeans, Java enum, Map ${employee.age + 20} Arithmetic expr. ${planet == earth } Logical expr. August 21, 2008 Nick Duan 16 8

EL Resolvers A set of standard Java classes extending ELResolver for resolving JSP objects and accessing their properties Types of resolver classes include ArrayELResolver BeanELResolver ListELResolver MapELResolver ResourceBundleResolver ImplicitObjectResolver August 21, 2008 Nick Duan 17 Custom Tags in JSP A customer tag is a JSP element designed to perform JSP functions (e.g EL eval) and/or HTML rendering of a page segment Format in JSP <pref:tagname attri_name= value../> (with or without body) Able to access all objects available to JSP and communicate with each other Reusable and flexible, more popular than JSP fragment. Can be bundled into a tag library Can be implemented either using a combination of EL and existing tags/scripts, or via creation of tag classes using tag APIs August 21, 2008 Nick Duan 18 9

Configure Custom Tags Create tag files with extension.tag and package under WEB-INF/tags/dir, or provide tag classes under WEB-INF/classes Reference tag library in JSP <%@ taglib prefix= pref tagdir= WEB- INF/tags/subdir %> <%@ taglib prefix= pref" uri= uri" %> url references the location of the tld file In case of API implementation, define tag library descriptor file(s) with the extension.tld under WEB-INF or a subdirectory Implicit tld file is generated in case of tag files August 21, 2008 Nick Duan 19 JSP Standard Tag Library What is JSTL? A collection of commonly used custom tags for JSP A JCP standard, Latest version: 1.2 Defined in multiple categories according to tag functionality Category Uri Prefix Core http://java.sun.com/jstl/core c XML Processing http://java.sun.com/jstl/xml xml I18N formatting http://java.sun.com/jstl/fmt fmt Database http://java.sun.com/jstl/sql sql Functions http://java.sun.com/jstl/functions fn August 21, 2008 Nick Duan 20 10

Example of JSTL <%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <sql:query var="rs" datasource="jdbc/proj642db"> select * from employee </sql:query> <table border=1 bgcolor="lightblue" width="300"> <tr><td>name</td> <td>address</td> <td>age</td> </tr> <c:foreach var="row" items="${rs.rows}"> <tr><td>${row.name}</td> <td>${row.address}</td> <td>${row.age}</td> </tr> </c:foreach> </table> August 21, 2008 Nick Duan 21 Summary JSP is a combination of various techniques for creating presentation-tier of web applications. If used properly, JSP can be a efficient tool in developing web applications The best practice in JSP nowadays is to use JavaBeans and Bean-like components, and custom tags to define page logic and HTML rendering, instead of using embedded Java code Design a JSP into reusable fragments implemented using tags August 21, 2008 Nick Duan 22 11

Quiz How to create aperform request dispatching in JSP (servlet-chaining in JSP)? Should you ever use request dispatching in JSP? What would be the best way to instantiate a Java ArrayList object in a JSP? How to define a custom tag that displays the content of a Map in HTML table format? How to create a single customer tag that renders an SQL result in table format? August 21, 2008 Nick Duan 23 12