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

Similar documents
Java Server Page (JSP)

Scope and State Handling in JSP

JavaServer Pages. What is JavaServer Pages?

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

JSP MOCK TEST JSP MOCK TEST IV

Experiment No: Group B_2

JSP. Basic Elements. For a Tutorial, see:

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

COMP9321 Web Application Engineering

COMP9321 Web Application Engineering

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

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

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

COMP9321 Web Application Engineering

Ch04 JavaServer Pages (JSP)

Unit 5 JSP (Java Server Pages)

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

Advantage of JSP over Servlet

CE212 Web Application Programming Part 3

COMP201 Java Programming

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

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:

A Gentle Introduction to Java Server Pages

SSC - Web development Model-View-Controller for Java Servlet

Java Server Pages. JSP Part II

112. Introduction to JSP

112-WL. Introduction to JSP with WebLogic

Using JavaBeans with JSP

Module 5 Developing with JavaServer Pages Technology

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.

Java E-Commerce Martin Cooke,

JavaServer Pages (JSP)

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

UNIT -5. Java Server Page

JSP (Java Server Page)

JavaServer Pages. Juan Cruz Kevin Hessels Ian Moon

CISH-6510 Web Application Design and Development. JSP and Beans. Overview

Advanced Java Programming

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

Java Server Pages, JSP

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

Component Based Software Engineering

CHAPTER 1. Core Syntax Reference

Presentation and content are not always well separated. Most developers are not good at establishing levels of abstraction in JSPs

Integrating Servlets and JavaServer Pages Lecture 13

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

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

Advance Java. Configuring and Getting Servlet Init Parameters per servlet

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

JSP Scripting Elements

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

Basic Principles of JSPs

JSP source code runs on the web server via JSP Servlet Engine. JSP files are HTML files with special Tags

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

01KPS BF Progettazione di applicazioni web

Principles and Techniques of DBMS 6 JSP & Servlet

Introduction to JSP and Servlets Training 5-days

Université du Québec à Montréal

Model View Controller (MVC)

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

ddfffddd CS506 FINAL TERMS SOLVED BY MCQS GHAZAL FROM IEMS CAMPUS SMD CS506 Mcqs file solved by ghazal

Unit 4 Java Server Pages

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

Trabalhando com JavaServer Pages (JSP)

ADVANCED JAVA COURSE CURRICULUM

Sun Sun Certified Web Component Developer for J2EE 5 Version 4.0

SNS COLLEGE OF ENGINEERING, Coimbatore

Servlet and JSP Review

JAVA 2 ENTERPRISE EDITION (J2EE)

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

One application has servlet context(s).

Java Server Pages(JSP) Unit VI

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

Java Server Pages. Copyright , Xiaoping Jia. 7-01/54

GUJARAT TECHNOLOGICAL UNIVERSITY

Session 21. Expression Languages. Reading. Java EE 7 Chapter 9 in the Tutorial. Session 21 Expression Languages 11/7/ Robert Kelly,

Jakarta Struts: An MVC Framework

Web applications and JSP. Carl Nettelblad

Trabalhando com JavaServer Pages (JSP)

Servlets. An extension of a web server runs inside a servlet container

Session 22. Intra Server Control. Lecture Objectives

IT6503 WEB PROGRAMMING. Unit-I

SRI VIDYA COLLEGE OF ENGINEERING & TECHNOLOGY- VIRUDHUNAGAR

Module 4: SERVLET and JSP

Making applica+ons with Java. Text Technologies 2013 Chris Culy

CSC309: Introduction to Web Programming. Lecture 11

LTBP INDUSTRIAL TRAINING INSTITUTE

NetRexx Server Pages. 24th International Rexx Language Symposium Raleigh/Durham, NC René Vincent Jansen, May 8th, 2013.

JSP MOCK TEST JSP MOCK TEST III

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

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

Web Programming. Lecture 11. University of Toronto

JSP: Servlets Turned Inside Out

CS506 Web Design & Development Final Term Solved MCQs with Reference

/ / JAVA TRAINING

First Simple Interactive JSP example

Integrating Servlets and JSP: The MVC Architecture

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

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

Session 11. Expression Language (EL) Reading

Transcription:

Introduction to Java Server Pages Jeff Offutt & Ye Wu http://www.ise.gmu.edu/~offutt/ SWE 432 Design and Implementation of Software for the Web From servlets lecture. Enabling Technologies - Plug-ins Scripted Pages Scripted pages look like HTML pages that happen to process business logic Execution is server-side, not client-side (like JavaScripts) They are HTML pages that access software on the server to get and process data Common scripted pages: Allaire s Cold Fusion Microsoft s Active Server Pages (ASP) Java Server Pages (JSP) JSPs are compiled and run as servlets (very clean and efficient) Scripted pages are generally easier to develop, deploy, and modify 11/12/2007 Offutt 2 1

Java Server Pages (JSP) Java Scripts provide client-side execution ability Interpreted Cumbersome and error prone Non-portable Java Servlets provide server-side execution Compiled Portable Robust Not integrated with HTML Java creates HTML Mixes static (HTML) with dynamic (business logic) 11/12/2007 Offutt 3 Java Server Pages (2) JSPs turn servlets "inside-out": Instead of HTML in Java Java in HTML JSPs are translated to servlets, compiled, then executed This encourages separation of tasks: Page Layout Writing HTML Integration w/ JSP Application Development Java, JavaBeans Graphics designer? Webby Java programmer Java programmer 11/12/2007 Offutt 4 2

First Look at JSP Code <%@page import = "java.util.date"%> <HTML> <BODY> <CENTER> <H1>Java Server Page example</h1> The current time is <%= new Date() %> </CENTER> </ BODY > </ HTML > http://apps-inst.ite.gmu.edu:8080/offutt/jsp/date.jsp 11/12/2007 Offutt 5 JSP Processing Simple View 1. request 5. response Server with JSP container 3. Run JSP 4. Response 2. Read Hello.jsp It s actually a little more complicated JSP execution mental model of JSP developer 11/12/2007 Offutt 6 3

Client 1. request JSP Processing Server with JSP container 2. if no hello.class or hello.jsp newer than hello.class 2. timestamp Yes hello.jsp 3. Translate hello.java 6. response No 2. timestamp 4. Compile 5. Execute JSP execution actual implementation hello.class 11/12/2007 Offutt 7 JSP Servlet Methods jspinit() jspdestroy() _jspservice() Can be defined in JSP declarations Called when servlet starts and ends Servlet service() Includes all Java from JSP 11/12/2007 Offutt 8 4

JSP Example http://ise.gmu.edu/~offutt/classes/432/examples/jsp/ Just do one, save the others 11/12/2007 Offutt 9 JSP Elements JSP syntax: <%X %>// X is one of the following: 1. @ Directive: Global information for page Language, import statements, etc. 2. Scripting Elements: Java code! Declarations: Class level variables and methods (blank) Scriptlets: A block of Java code Can make external calls = Expressions: Values to be printed 3. Actions: To modify runtime behavior 11/12/2007 Offutt 10 5

1) JSP Directives Messages sent to the JSP container <%@ page attribute=value %> Page attributes are listed in book You will usually use the defaults <%@ include <filename> %> File inserted into the JSP inline before JSP is compiled <%@ taglib uri="tagliburi" prefix="tagprefix" %> 11/12/2007 Offutt 11 2) JSP Scripts Declarations Java code to define class-level variables and methods <%! int Sum = 0; private void AddToCount (int X) { // To be called from a scriptlet Sum = Sum + X; } %> jspinit() and jspdestroy() can also be defined here to initialize and clean up state 11/12/2007 Offutt 12 6

2) JSP Scripts Scriptlets Blocks of general Java code Placed in _jspservice() Can access variables from the JSP Declaration Scriptlets can access servlet objects request : our usual req response : our usual res out : for printing Note that the name "request" must be used. <% String nameval = request.getparameter ("LASTNAME"); out.println (nameval); %> 11/12/2007 Offutt 13 2) JSP Scripts Expressions Abbreviated scriptlet print statement <P> The user's last name is <%= nameval %> </P> Expression is evaluated and turned into a string 11/12/2007 Offutt 14 7

3) JSP Actions Tags to change the behavior of the JSP <jsp:include page="myjsp.jsp" flush="true" /> myjsp.jsp is compiled myjsp.jsp is executed output from myjsp is included in the current JSP Action types: <jsp: usebean> <jsp: setproperty > <jsp: getproperty > <jsp: param > <jsp: include > <jsp: forward > <jsp: plugin > 11/12/2007 Offutt 15 3) JSP Actions Java Beans A Java Bean is a Java class with 3 characteristics: 1. public class 2. public constructor with no arguments 3. public get and set methods Property: A special, simple data object (that is, variable) getname () <jsp:getproperty> setname (String name) <jsp:setproperty> Note that a bean is not a Java language feature, but a design convention (pattern) 11/12/2007 Offutt 16 8

3) JSP Actions Java Beans usebean causes a JavaBean object to be instantiated usebean gives a name to the new object (id=) usebean defines the scope usebean declares the location (bean details) 11/12/2007 Offutt 17 3) JSP Actions Java Bean Example Syntax for using a bean: <%@ page import= color.*" %> <jsp:usebean id="lettercolor" scope="page" class= color.alphabetcode" /> Converts to Java import statement, Java 4 requires all imports to be packages ID name to use for object (AlphabetCode LetterColor = new ) Note that scope="application" allows Beans to be shared among different servlets DON'T USE IT! That can lead to interactions among each other. 11/12/2007 Offutt 18 9

3) JSP Actions Properties setproperty gives a value to a property in a bean <jsp:setproperty name="langbean" property="language" value="java"/> Equivalent to the call: langbean.setlanguage ("Java"); <jsp:setproperty name="langbean" property="*" /> Sets all of the properties with values from HTML FORM getproperty retrieves the value of a property <jsp:getproperty name="langbean" property="language"/> Equivalent to the call: langbean.getlanguage(); 11/12/2007 Offutt 19 3) JSP Actions Java Bean Summary Using Java Beans allows for more separation between the HTML and Java The Beans / Property pattern provides a very convenient standard for implementing standard Java classes JSP's usebean uses Java reflection to translate property names ("language") to method calls that are assumed to exist ("setlanguage()" and "getlanguage()") 11/12/2007 Offutt 20 10

3) JSP Actions Forwarding jsp:forward allows a request to be forwarded to another JSP <jsp:forward page="anotherpage.jsp" /> When this statement is reached, execution will jump to the JSP anotherpage.jsp Use as a front-end when we need to decide which JSP to execute based on some input data 11/12/2007 Offutt 21 Sharing Data Between JSP Pages, Requests, and Users JSPs provide different scopes for sharing data objects Page: Within the same web page Request: Within the same request Session: Within all requests from the same session Application: Within all sessions for one servlet context 11/12/2007 Offutt 22 11

Sharing Data Objects request page forward request Client 1 request page forward request session page Client 2 application 11/12/2007 Offutt 23 Sharing Data Between JSP Pages, Requests, and Users (1) Using JSP Scriptlet getparameter(); // retrieves client form data request.getattribute(), request.setattribute(); session.getattribute(), session.setattribute(); context.getattribute(), context.setattribute(); For example: <% session.setattribute ("ID",request.getParameter ("ID")); %> Predefined variable 11/12/2007 Offutt 24 12

Sharing Data Between JSP Pages, Requests, and Users (2) The previous approach makes the code clumsy Alternative approach JavaBean: Use the scope attribute in the <jsp:usebean> action <jsp:usebean id = languagebean scope= session class = lang.languagebean > <jsp:getproperty name= languagebean property= name > 11/12/2007 Offutt 25 Sharing Sessions and Application Data Sharing sessions <jsp:usebean scope= session > Sharing application data <jsp:usebean scope= application > 11/12/2007 Offutt 26 13

Installing JSPs on Our Server It turns out to be subtle to get JSPs to interface with Java Beans A JSP is converted to a Java servlet, which is then compiled by the servlet engine Therefore the bean has to be in a directory that is in the Java CLASSPATH of the servlet engine On our server, the Java servlet engine CLASSPATH includes the directory where we put servlets: /apps/tomcat/swe432/web-inf/classes/ We all have write permissions there 11/12/2007 Offutt 27 Deploying JSPs on Our Server 1. Import the bean into JOUseBean.jsp: <%@ page import= offutt.jobean" %> 2. Copy the bean's.class file into the classes directory: cp JOBean.class /apps/tomcat/swe432/web-inf/classes/offutt/ 3. Copy your JSP file into the JSP directory: cp JOUseBean.jsp /apps/tomcat/swe432/jsp/ 4. Now you can run your JSP from your browser by entering the URL: http://apps-swe432.ite.gmu.edu:8080/swe432/jsp/jousebean.jsp 11/12/2007 Offutt 28 14

JSP & Java Bean Examples http://ise.gmu.edu/~offutt/classes/432/examples/jsp/ 11/12/2007 Offutt 29 15