Basic Elements JSP For a Tutorial, see:

Slides:



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

J0 1 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialitica in Informatica – Università di Trento.
JSP and web applications
Java Server Pages Jeffrey Jongko. Introduction Java Server Pages (JSP) technology was created by Sun Microsystems and is built on top of Sun’s Java Servlet.
Java II--Copyright © Tom Hunter. J2EE JSP Custom Tag Libraries.
 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.
Advanced Java Server Pages An more detailed look at JSPs.
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.
28/1/2001 Seminar in Databases in the Internet Environment Introduction to J ava S erver P ages technology by Naomi Chen.
DT211/3 Internet Application Development
18-Jun-15 JSP Java Server Pages Reference: Tutorial/Servlet-Tutorial-JSP.html.
CS320 Web and Internet Programming JSP Scripting Elements and Page Directives Chengyu Sun California State University, Los Angeles.
Generate Dynamic Content On Cache Server Master’s Project Proposal by Aparna Yeddula.
JSP Java Server Pages Reference:
Java Server Pages B.Ramamurthy. Java Server Pages Servlets are pure Java programs. They introduce dynamism into web pages by using programmatic content.
Java Server Pages Russell Beale. What are Java Server Pages? Separates content from presentation Good to use when lots of HTML to be presented to user,
Comp2513 Java Server Pages Daniel L. Silver, Ph.D.
1 CS6320 – JSP L. Grewe 2 Java Server Pages Servlets require you to write out entire page delivered with print statements Servlets require you to write.
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,
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.
Java Servlets and JSP.
Introduction to Java web programming Dr Jim Briggs JWP intro1.
Java Server Pages B.Ramamurthy. Topics for Discussion 8/20/20152 Inheritance and Polymorphism Develop an example for inheritance and polymorphism JSP.
DAT602 Database Application Development Lecture 15 Java Server Pages Part 1.
M. Taimoor Khan * Java Server Pages (JSP) is a server-side programming technology that enables the creation of dynamic,
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.
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.
Introduction to Java Server Pages (JSPs) Robert Thornton.
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.
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.
JSP Most of the web developers deploying web applications using servlets mixes the presentation logic and business logic. Separation of business logic.
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.
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.
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
COMP 321 Week 11. Overview Lab 8-1 Solution Tag Files Custom Tags Web Application Deployment.
JSP Fundamentals Elements of a JSP Using Beans with JSP Integrating Servlets and JSP.
Java server pages. A JSP file basically contains HTML, but with embedded JSP tags with snippets of Java code inside them. A JSP file basically contains.
Copyright © 2002 ProsoftTraining. All rights reserved. JavaServer Pages.
Java Server Pages (JSP)
A seminar on j2ee by saritha. s. What is J2EE J2EE (Java 2 Platform, Enterprise Edition) is a Java platform designed for the mainframe-scale computing.
JSP Custom Tags. Prerequisites Servlet API Mapping to JSP implicit objects JavaServer Pages Basic syntax Implementation via servlet API XML.
An Introduction to JavaServer™ Pages Prepared by Nicole Swan.
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.
Web Technologies Java Beans & JSP By Praveen Kumar G.
JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.
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.
STRUCTURE OF JSP PRESENTED BY: SIDDHARTHA SINGH ( ) SOMYA SHRIVASTAV ( ) SONAM JINDAL ( )
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.
Chapter 6 Chapter 6 Server Side Programming (JSP) Part 1 1 (IS 203) WebProgramming (IS 203) Web Programming.
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.
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.
World Wide Web has been created to share the text document across the world. In static web pages the requesting user has no ability to interact with the.
JSP Thanks to Course-Materials/csajsp2.html.
JSP (Java Server Page) JSP is server side technology which is used to create dynamic web pages just like Servlet technology. This is mainly used for implementing.
Knowledge Byte In this section, you will learn about:
Pre-assessment Questions
Chengyu Sun California State University, Los Angeles
MSIS 655 Advanced Business Applications Programming
JSP Basic Elements For a Tutorial, see:
Java Server Pages B.Ramamurthy.
Presentation transcript:

Basic Elements JSP For a Tutorial, see:

Simple.jsp

JSP Lifecycle Browser generated Servlet Compiled Servlet JSP Page Server Web

Syntactic elements: JSP nuts and bolts Implicit Objects: request response pageContext session application out config page

Syntactic elements:  Interaction with the CONTAINER  In the initialization of the JSP  In the service method JSP nuts and bolts

A scriptlet is a block of Java code executed during the request- processing time. In Tomcat all the scriptlets gets put into the service() method of the servlet. They are therefore processed for every request that the servlet receives. Scriptlets

Examples : <% // Get the Employee's Name from the request out.println(" Employee: " + request.getParameter("employee")); // Get the Employee's Title from the request out.println(" Title: " + request.getParameter("title")); %> Scriptlet

An expression is a shorthand notation that sends the evaluated Java expression back to the client (in the form of a String). Examples : Sono le Expressions

Buongiorno Expressions

A declaration is a block of Java code used to: define class-wide variables and methods in the generated servlet. They are initialized when the JSP page is initialized. Examples: Declarations

A directive is used as a message mechanism to: pass information from the JSP code to the container Main directives: page include (for including other STATIC resources at compilation time) taglib (for including custom tag libraries) Directives

main attributes: Directives

Standard action are tags that affect the runtime behavior of the JSP and the response sent back to the client. For including STATIC or DYNAMIC resources at request time Standard actions

A bean is a Java class that:  Provides a public no-argument constructor  Implements java.io.Serializable  Follows JavaBeans design patterns Has Set/get methods for properties Has Add/remove methods for events  Java event model (as introduced by JDK 1.1)  Is thread safe/security conscious Can run in an applet, application, servlet,... public class SimpleBean implements Serializable { private int counter; SimpleBean() {counter=0;} int getCounter() {return counter;} void setCounter(int c) {counter=c;} } See What is a Java bean?

<jsp:useBean id=“name” class=“fully_qualified_pathname” scope=“{page|request|session|application}” /> Standard actions involving beans

When should I use a JSP directive, and when should I use a action? A JSP directive (for example, ) includes literal text "as is" in the JSP page and is not intended for use with content that changes at runtime. The include occurs only when the servlet implementing the JSP page is being built and compiled. The action allows you to include either static or dynamic content in the JSP page. Static pages are included just as if the directive had been used. Dynamic included files, though, act on the given request and return results that are included in the JSP page. The include occurs each time the JSP page is served. See also or ?

When should I use JSP-style comments instead of HTML-style comments? Always use JSP-style comments unless you specifically want the comments to appear in the HTML that results from serving a JSP page. JSP-style comments are converted by the JSP page engine into Java comments in the source code of the servlet that implements the JSP page. Therefore, JSP-style comments don't appear in the output produced by the JSP page when it runs. HTML-style comments pass through the JSP page engine unmodified. They appear in the HTML source passed to the requesting client. JSP-style comments do not increase the size of the document that results from the JSP page, but are useful to enhance the readability of the JSP page source, and to simplify debugging the servlet generated from the JSP page. (taken from: or ?

outWriter requestHttpServletRequest responseHttpServletResponse sessionHttpSession pagethis nel Servlet applicationservlet.getServletContext area condivisa tra i servlet configServletConfig exceptionsolo nella errorPage pageContext sorgente degli oggetti, raramente usato Predefined Objects

UseRequest <% // Get the User's Name from the request out.println(" Hello: " + request.getParameter("user") + " "); %> request

UseSession <% // Try and get the current count from the session Integer count = (Integer)session.getAttribute("COUNT"); // If COUNT is not found, create it and add it to the session if ( count == null ) { count = new Integer(1); session.setAttribute("COUNT", count); } else { count = new Integer(count.intValue() + 1); session.setAttribute("COUNT", count); } // Get the User's Name from the request out.println(" Hello you have visited this site: " + count + " times. "); %> session

Common patterns JSP

SERVER Common JSP patterns JSP or Servlet Enterprise JavaBeans Page-centric (client-server) DB CLIENT

Common JSP patterns JSP Business Processing Page View request response Page-centric 1 (client-server)

Common JSP patterns JSP Business Processing Page View with Bean request response Worker Bean Page-centric 2 (client-server)

Common JSP patterns Mediating JSP Presentation JSP Dispatcher (n-tier) Worker bean Presentation JSPWorker bean Presentation JSP request response service Business Processing Mediator - View

WebApps (Tomcat configuration)

JSP pages To let Tomcat serve JSP pages, we follow the same procedure that we described for static pages. In the myApp folder we can depost the JSP files. On our Tomcat server, the URL for the hello.jsp file becomes: The WEB-INF directory still contains the same web.xml file as in the static case must be provided. To actually see the webapp, you might have to restart Tomcat (with older Tomcat versions) myApp hello.jsp WEB-INF webapps web.xml

Tag Extension JSP

Ideally, JSP pages should contain no code written in the Java programming language (that is, no expressions or scriptlets). Anything a JSP page needs to do with Java code can be done from a custom tag  Separation of form and function.  Separation of developer skill sets and activities.  Code reusability.  Clarified system design. JSP custom tag

First custom tag This is static output HELLO THERE This is static output a JSP custom tag hello.doStartTag() hello.doEndTag()

package jsptags; import java.io.IOException; import java.util.Date; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; public class HelloTag extends TagSupport { public int doStartTag() throws JspTagException { try { pageContext.getOut().write("Start tag found here "); } catch (IOException e) { throw new JspTagException("Fatal error: could not write to JSP out"); } return EVAL_BODY_INCLUDE; // return SKIP_BODY; } a JSP custom tag

… public class HelloTag extends TagSupport { … public int doEndTag() throws JspTagException { try { pageContext.getOut().write("End tag found "); } catch (IOException e) { throw new JspTagException("Fatal error: could not write to JSP out"); } return EVAL_PAGE; // return SKIP_PAGE; } a JSP custom tag

Javax.servlet.jsp.tagext.Tag interface TagPagina JSP setPageContext(pageContext) setParent(enclosingTag) setAttribute1(pageContext) doStartTag() doEndTag() release()

Class Diagram API A BodyTag can manipulate its body, using its BodyContent object, while a normal Tag cannot. BodyTags are useful when you want to use or transform the contents of the tag.

First custom tag This is static output HELLO THERE This is static output a JSP custom tag hello.doInitBody() hello.doEndTag() hello.doStartTag() hello.doAfterBody()

package jsptags; … public class HelloTag extends BodyTagSupport { public int doStartTag() throws JspTagException { … } public void doInitBody() throws JspTagException { try { pageContext.getOut().write("Init Body "); } catch (IOException e) { throw new JspTagException("Fatal error: could not write to JSP out"); } a JSP custom tag

public int doAfterBody() throws JspTagException { try { pageContext.getOut().write("After Body "); } catch (IOException e) { throw new JspTagException("Fatal error: could not write to JSP out"); } return EVAL_BODY_TAG; // return SKIP_BODY; } */ public int doEndTag() throws JspTagException { … } a JSP custom tag

Javax.servlet.jsp.tagext.BodyTag interface TagPagina JSP setPageContext(pageContext) setParent(enclosingTag) setAttribute1() doStartTag() setBodyContent(out) release() PageContext pushBody() doInitBody() doEndTag() doAfterBody() popBody()

import java.io.IOException; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; public class ReverseTag extends BodyTagSupport { public int doEndTag() throws JspTagException { BodyContent bodyContent = getBodyContent(); if (bodyContent != null) {// Do nothing if there was no body content StringBuffer output = new StringBuffer(bodyContent.getString()); output.reverse(); try { bodyContent.getEnclosingWriter().write(output.toString()); } catch (IOException ex) { throw new JspTagException("Fatal IO error"); } } return EVAL_PAGE; } reversing body content

structure of the war file hello hello.jsp META-INF WEB-INF MANIFEST.MF web.xml tldsclasses hello.tldHelloTag.class A war file is a jar file with special directories and a file named web.xml in the WEB-INF directory

<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" " examples Simple example library. reverse tagext.ReverseTag JSP Simple example TLD

tagext Tag extensions examples 0 /hello /WEB-INF/tlds/hello.tld web.xml

Public Tag Libraries See e.g.: JSTL by Apache doc/intro.html Open Source JSP Tag Libraries by JavaSource.net libraries