Advanced Java Server Pages An more detailed look at JSPs.

Slides:



Advertisements
Similar presentations
J0 1 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialitica in Informatica – Università di Trento.
Advertisements

9 Copyright © 2005, Oracle. All rights reserved. Modularizing JavaServer Pages Development with Tags.
JSP and web applications
Web Development with Karsten Schulz Terp-Nielsen Master Principal Sales Consultant Oracle Denmark.
CS4273: Distributed System Technologies and Programming I Lecture 11: JavaServer Pages (JSP)
© Yaron Kanza Advanced Java Server Pages Written by Dr. Yaron Kanza, Edited by permission from author by Liron Blecher.
 2002 Prentice Hall. All rights reserved. Chapter 9: Servlets Outline 9.1 Introduction 9.2 Servlet Overview and Architecture Interface Servlet and.
Java II--Copyright © Tom Hunter. J2EE JSP Custom Tag Libraries.
An introduction to Java Servlet Programming
JSP Tag Extensions And Java Bean. JSP Tag Extensions Tag extensions look like HTML (or rather, XML) tags embedded in a JSP page. They have a special meaning.
Java Server Pages by Jon Pearce. JSP Documents JSP docs are XHTML Documents containing: –Fixed-Template Data: FTD HTML Components XML markup –JSP Components:
Generate Dynamic Content On Cache Server Master’s Project Proposal by Aparna Yeddula.
Q: According to Intel, the Pentium conforms to the IEEE standards 754 and 854 for floating point arithmetic. If you fly in aircraft designed using a Pentium,
The J2EE BookShop A detailed walk through of the J2EE BookShop.
Object-Oriented Enterprise Application Development JavaServer Pages Tag Libraries.
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.
ECE356 – Database Systems Lab 1 – Building a Web Project with NetBeans Tiuley Alguindigue Lab Instructor – University of Waterloo, E & CE Dept. Fall 2013.
Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.
11. Java-Based Web: JSP, JSF. 2 Motto: Rule 1: Our client is always right Rule 2: If you think our client is wrong, see Rule 1. - Anonymous.
OOSSE - OO Review Review session A review of other OO technologies and may be useful for exam (not required for assignment) OO web development philosophy.
Basic Elements JSP For a Tutorial, see:
CSC 2720 Building Web Applications Using Java Beans, Custom Tags and Tag Libraries in JSP pages.
Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 1 of 37 J2EE Web Components Pre-assessment Questions 1.The _____________ attribute of a JSP page.
Chapter 111© copyright Janson Industries 2011 Custom Tags ▮ Tag Handlers ▮ XML ▮ Tag Libraries ▮ Web Deployment Descriptor.
Introduction to JSP Based on: Marty Hall, Larry Brown, Core Servlets and JavaServer Pages.
Li Tak Sing COMPS311F. Static attributes in Servlets Since Servlets are also Java classes, you can also use static attributes to store values that can.
® IBM Software Group © 2007 IBM Corporation JSP Custom Tags
Custom Tags1 Usage Rules PowerPoint slides for use only in for-credit courses at degree-granting institutions Slides can be modified.
Chapter 7 Java Server Pages. Objectives Explain how the separation of concerns principle applies to JSP Describe the operation and life-cycle of a JSP.
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)
JSP Architecture Outline  Model 1 Architecture  Model 2 Architecture.
Chapter 8 Script-free pages. Problem with scripting in JSP When you use scripting (declaration, scriplet, expressions) in your JSP, you actually put Java.
JSP Most of the web developers deploying web applications using servlets mixes the presentation logic and business logic. Separation of business logic.
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,
Slides © Marty Hall, book © Sun Microsystems Press 1 JSP Scripting Elements Core Servlets & JSP book:
16-Oct-15 JSP Implicit Objects. 2 JSP Implicit Objects are the Java objects that the JSP Container makes available to developers in each page and developer.
1 JSP with Custom Tags Blake Adams Introduction Advanced Java Server Pages – Custom Tags Keyterms: - Tag Library Descriptor(TLD) - Tag Libraries.
Fall 2007cs4201 Advanced Java Programming Umar Kalim Dept. of Communication Systems Engineering
Java Servlets & Java Server Pages Lecture July 2013.
COMP 321 Week 11. Overview Lab 8-1 Solution Tag Files Custom Tags Web Application Deployment.
MVC Model 2 Architecture & AddressBook case study
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.
CSC 2720 Building Web Applications JavaServer Pages (JSP) The Basics.
JSP Tag Libraries Lec Last Lecture Example We incorporated JavaBeans in “Course Outline” Example But still have to write java code inside java.jsp.
COMP 321 Week 10. Overview Using Beans in JSP Expression Language JSTL Lab 10-1 Introduction Exam Review.
Java Server Pages (JSP)
Java Server Pages An introduction to JSP. Containers and Components Several clients – one system.
CSC 2720 Building Web Applications Frameworks for Building Web Applications.
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.
JavaServer Page by Antonio Ko. Overview ► Introduction ► What is a servlet? ► What can servlets do? ► Servlets Vs JSP ► Syntax ► Samples ► JavaBean ►
JSP Custom Tags. Prerequisites Servlet API Mapping to JSP implicit objects JavaServer Pages Basic syntax Implementation via servlet API XML.
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.
CSI 3125, Preliminaries, page 1 SERVLET. CSI 3125, Preliminaries, page 2 SERVLET A servlet is a server-side software program, Responds oriented other.
Advanced Java Session 6 New York University School of Continuing and Professional Studies.
Basic JSP Celsina Bignoli Problems with Servlets Servlets contain –request processing, –business logic –response generation all lumped.
COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 3 1COMP9321, 15s2, Week.
Java Web 应用开发: J2EE 和 Tomcat 蔡 剑, Ph.D.. 本讲内容 Web 层技术 (III) Custom Tags JSP and XML JSTL.
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.
Introduction To HTML Dr. Magdi AMER. HTML elements.
 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.
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.
J2EE JSP Custom Tag Libraries 1 3 JSP: Custom Tag Libraries.
JSP Java Server Pages. Hello, !
17 Copyright © 2004, Oracle. All rights reserved. Integrating J2EE Components.
CS 562 Advanced Java and Internet Application Computer Warehouse Web Application By Team Alpha :-  Puja Mehta (102163)  Mona Nagpure (102147)
Enterprise Java v050228MVC1 Model, View, Controller Web Architecture.
Java Servlets By: Tejashri Udavant..
Pre-assessment Questions
Presentation transcript:

Advanced Java Server Pages An more detailed look at JSPs

Custom methods in JSPs Just as Servlets, JSPs have methods for initialization end destruction JspInit() corresponds to init() in Servlets JspDestroy corresponds to destroy() in Servlets Both JspInit() and JspDestroy() can implemented in your JSPs, i.e you can override the base class Think before you use them  If you need the init and destroy, is it really a JSP you should be writing? Probably not!

Servlets and JSPs Servlets should be used as front ends for JSPs  The Servlet should handle the control logic  The Servlet can initialize Beans that the JSP can use  The Beans can talk to databases, EJBs and so on  This is basic MVC But how is this done?

Before the explanation Assume there is a Bean, DataBean that fetches records from a database  The records are fetched to the bean with the fetch() method  The records are returned from the bean with the getRecords() method

The Servlet part Create the Bean instance Call your methods on the Bean Add the bean to the request or the session Get a RequestDispatcher Use the forward() method in the RequestDispatcher

The Servlet part, using the request public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { DataBean db = new DataBean(); db.fetch(); request.setAttribute("dbBean", db); RequestDispatcher rd = request.getRequestDispatcher("jsppage.jsp"); rd.forward(request, response); }

The Servlet part, using the session public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession ses = request.getSession(); DataBean db = new DataBean(); db.fetch(); ses.setAttribute("dbBean", db); RequestDispatcher rd = request.getRequestDispatcher("jsppage.jsp"); rd.forward(request, response); }

The JSP part The JSP uses the to define the bean, but it should not result in a new instance. The instance that was added in the Servlet is used  The same scope in as in the Servlet (request and session in our examples)  The same id in as the name in the Servlet The JSP uses to call methods in the Bean

The JSP part, with request Error, the bean should have been created in the servlet!

The JSP part, with session Error, the bean should have been created in the servlet!

Custom Tags We don’t want Java code in our JSPs The built in tags is quite limited in functionality What we need is a way to extend the built in tags with our own  Custom Tags

Custom Tags Custom Tags separates the logic from the presentation even more that In our BookShop for example, the JSP with a book list and the shopping cart can consist of two lines of code 

Two types of Custom Tags Simple Tag (Tag)  A tag with or without arguments  Doesn’t use the Tag body  Implements javax.servlet.jsp.tagext.Tag Body Tag  With or without arguments  Evaluate the body of the tag This is the body  Implements javax.servlet.jsp.tagext.BodyTag

Simple Tag, an example import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; public class HelloWorldTag implements Tag { private PageContext pageContext; private Tag parent; /** * Constructor */ public HelloWorldTag() { super(); } /** * Method called at start of Tag either a EVAL_BODY_INCLUDE or a SKIP_BODY */ public int doStartTag() throws javax.servlet.jsp.JspTagException { return SKIP_BODY; }

Simple Tag, an example /** * Method Called at end of Tag either EVAL_PAGE or SKIP_PAGE */ public int doEndTag() throws javax.servlet.jsp.JspTagException { try { pageContext.getOut().write("Hello World!"); } catch(java.io.IOException e) { throw new JspTagException("IO Error: " + e.getMessage()); } return EVAL_PAGE; } /** * Method called to releases all resources */ public void release() {} /** Method used by the JSP container to set the current PageContext pageContext, the current PageContext */ public void setPageContext(final javax.servlet.jsp.PageContext pageContext) { this.pageContext=pageContext; }

Simple Tag, an example /** Method used by the JSP container to set the parent of the Tag parent, the parent Tag */ public void setParent(final javax.servlet.jsp.tagext.Tag parent) { this.parent=parent; } /** Method for retrieving the parent the parent */ public javax.servlet.jsp.tagext.Tag getParent() { return parent; }

Simple Tag All methods in the interface must be implemented!  All work is done in the doStartTag() and doEndTag() The doStartTag() is called at the start of the tag The doEndTag() is called at the start of the tag Instead of implementing Tag, extend TagSupport  A helper class included in the package that has implementation for all required methods. Just implement the one that you will use!

Simple Tag, with TagSupport (complete) import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; public class HelloWorldTag extends TagSupport { private PageContext pageContext; private Tag parent; /** * Method Called at end of Tag either EVAL_PAGE or SKIP_PAGE */ public int doEndTag() throws javax.servlet.jsp.JspTagException { try { pageContext.getOut().write("Hello World!"); } catch(java.io.IOException e) { throw new JspTagException("IO Error: " + e.getMessage()); } return EVAL_PAGE; }

Simple Tags In other words, use TagSupport! The HelloTag implements our simple tag Custom Tags are defined in a Tag Library Descriptor (tld)

Tag Library Descriptor mt /mytag My first Tag library helloWorld tags.HelloWorldTag empty A Hello world Tag

Web.xml The tld is referenced in web.xml  Web.xml binds a tag library to a web application mytags /WEB-INF/taglib.tld

Using your tag The uri specified in web.xml is used in the JSP  At the start of the page  Prints “Hello World”

Parameterized tags Both Tag and BodyTag can take parameters  Just as in JavaBeans, use a set-method and a member variable  private String name=“”;  public void setName(_name){ name=_name;}

Parameterized tags import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; public class HelloNameTag extends TagSupport{ private String name=“”; public void setName(String _name){name=_name;} public int doEndTag() throws javax.servlet.jsp.JspTagException{ try{ pageContext.getOut().write("Hello “ + name); } catch(java.io.IOException e){  throw new JspTagException("IO Error: " + e.getMessage()); } return EVAL_PAGE; }

TLD for HelloName hello HelloNameTag empty A Hello Tag name false false

Using the parameterized tag

BodyTag Just as with Tag, there are a lot of methods to write if implementing BodyTag There is a helper class call BodyTagSupport just as with TagSupport BodyTag is often used for looping over some result

Body Tag package se.upright.education.uu.pvk.tags; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; public class LoopTag extends BodyTagSupport { private int iterations = 5; /** * Method used by the JSP container to set the parameter Name. */ public void setIterations(int _iterations) { this.iterations=_iterations; } /** * Method called by the Container to set the BodyContent */ public void setBodyContent(BodyContent bodyContent) { this.bodyContent=bodyContent; }

Body Tag public int doAfterBody() throws JspTagException { if(iterations>1) { //decrease the number of iterations left to do iterations--; return EVAL_BODY_TAG; } else { return SKIP_BODY; }

Body Tag /** * Method Called at end of tag either EVAL_PAGE or SKIP_PAGE */ public int doEndTag() throws JspTagException { try { if(bodyContent != null) // Check if we even entered the body bodyContent.writeOut(bodyContent.getEnclosingWriter()); } catch(java.io.IOException e) { throw new JspTagException("IO Error: " + e.getMessage()); } return EVAL_PAGE; } }

Body Tag tld loop se.upright.education.uu.pvk.tags.LoopTag JSP iterations true

Using Body Tag Looping

Next Time Java Standard Tag Library  A Tag Library with common functionallity XML XSLT