JavaServer Pages (JSP)

Similar documents
COMP9321 Web Application Engineering

COMP9321 Web Application Engineering

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:

COMP9321 Web Application Engineering

Java Server Pages JSP

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

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

CE212 Web Application Programming Part 3

JSP. Basic Elements. For a Tutorial, see:

JavaServer Pages. What is JavaServer Pages?

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

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

Université du Québec à Montréal

01KPS BF Progettazione di applicazioni web

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

Basic Principles of JSPs

Java Server Page (JSP)

JavaServer Pages. Juan Cruz Kevin Hessels Ian Moon

Servlet Basics. Agenda

Integrating Servlets and JavaServer Pages Lecture 13

Java Server Pages. JSP Part II

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

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

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

Database Systems Lab. 11. JSP I 충남대학교컴퓨터공학과 데이타베이스시스템연구실

Servlet And JSP. Mr. Nilesh Vishwasrao Patil, Government Polytechnic, Ahmednagar. Mr. Nilesh Vishwasrao Patil

Servlet and JSP Review

DEZVOLTAREA APLICATIILOR WEB LAB 4. Lect. Univ. Dr. Mihai Stancu

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

ServletConfig Interface

Principles and Techniques of DBMS 6 JSP & Servlet

Trabalhando com JavaServer Pages (JSP)

CIS 3952 [Part 2] Java Servlets and JSP tutorial

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

Web based Applications, Tomcat and Servlets - Lab 3 -

CSC309: Introduction to Web Programming. Lecture 10

Chapter 2 How to structure a web application with the MVC pattern

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

Java Enterprise Edition. Java EE Oct Dec 2016 EFREI/M1 Jacques André Augustin Page 1

JAVA 2 ENTERPRISE EDITION (J2EE)

sessionx Desarrollo de Aplicaciones en Red A few more words about CGI CGI Servlet & JSP José Rafael Rojano Cáceres

Servlets1. What are Servlets? Where are they? Their job. Servlet container. Only Http?

Java E-Commerce Martin Cooke,

JAVA SERVLET. Server-side Programming INTRODUCTION

JSP CSCI 201 Principles of Software Development

Trabalhando com JavaServer Pages (JSP)

Advanced Internet Technology Lab # 4 Servlets

Web applications and JSP. Carl Nettelblad

JSP Scripting Elements

Component Based Software Engineering

Unit 5 JSP (Java Server Pages)

Servlets and JSP (Java Server Pages)

COMP201 Java Programming

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

JSP: Servlets Turned Inside Out

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

Sun Sun Certified Web Component Developer for J2EE 5 Version 4.0

INTRODUCTION TO SERVLETS AND WEB CONTAINERS. Actions in Accord with All the Laws of Nature

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

Stateless -Session Bean

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

UNIT 6:CH:14 INTEGRATING SERVLETS AND JSPTHE MVC ARCHITECTURE

Servlet. Web Server. Servlets are modules of Java code that run in web server. Internet Explorer. Servlet. Fire Fox. Servlet.

Introduction to JSP and Servlets Training 5-days

The Basic Web Server CGI. CGI: Illustration. Web based Applications, Tomcat and Servlets - Lab 3 - CMPUT 391 Database Management Systems 4

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

ADVANCED JAVA COURSE CURRICULUM

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

Advanced Java Programming

AJP. CHAPTER 5: SERVLET -20 marks

Java Server Pages(JSP) Unit VI

Handout 31 Web Design & Development

Session 8. JavaBeans. Reading & Reference. Reading. Reference. Session 8 Java Beans. 2/27/2013 Robert Kelly, Head First Chapter 3 (MVC)

HttpServlet ( Class ) -- we will extend this class to handle GET / PUT HTTP requests

SWE642 Oct. 22, 2003

Enterprise Java Technologies (Part 1 of 3) Component Architecture. Overview of Java EE. Java Servlets

Lab session Google Application Engine - GAE. Navid Nikaein

How to structure a web application with the MVC pattern

Using Java servlets to generate dynamic WAP content

Fast Track to Java EE

A Gentle Introduction to Java Server Pages

Advantage of JSP over Servlet

Université du Québec à Montréal

Unit 4 Java Server Pages

Introduction. This course Software Architecture with Java will discuss the following topics:

Servlet Fudamentals. Celsina Bignoli

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

Integrating Servlets and JSP: The MVC Architecture

Web. 2 Web. A Data Dependency Graph for Web Applications. Web Web Web. Web. Web. Java. Web. Web HTTP. Web

JAVA. Web applications Servlets, JSP

Introduction. Literature: Steelman & Murach, Murach s Java Servlets and JSP. Mike Murach & Associates Inc, 2003

Module 4: SERVLET and JSP

DVS WEB INFOTECH DEVELOPMENT TRAINING RESEARCH CENTER

SNS COLLEGE OF ENGINEERING, Coimbatore

Java4570: Session Tracking using Cookies *

CSC309: Introduction to Web Programming. Lecture 11

JSP. Common patterns

JSP (Java Server Page)

Web Programming. Lecture 11. University of Toronto

JSP MOCK TEST JSP MOCK TEST IV

Transcription:

JavaServer Pages (JSP)

The Context The Presentation Layer of a Web App the graphical (web) user interface frequent design changes usually, dynamically generated HTML pages Should we use servlets? No difficult to develop and maintain (the view) lack of flexibility lack of role separation: design designer

Example: HelloServlet.java import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class Hello extends HttpServlet { public void doget(httpservletrequest request, HttpServletResponse response) throws IOException { response.setcontenttype("text/html"); } } PrintWriter out = new PrintWriter(response.getWriter()); out.println("<html>" + "<head><title>welcome!</title></head>" + "<body>" + "<h1><font color=\"red\"> Welcome! </font></h1>" + "<h2>current date and time:" + new java.util.date() + " </h2>" + "</body>" + "</html>"); out.close();

Example: hello.jsp <html> <head> <title>welcome!</title> </head> <body> <h1><font color="red"> Welcome! </font></h1> <h2>current date and time: <%= new java.util.date() %> </h2> </body> </html>

The Concept Standard component to create the presentation, that is conform to View Helper design pattern

JavaServer Pages (JSP) The standard solution to create web content easily that has both static and dynamic components Web pages Provides a natural, declarative, presentation oriented approach to creating static content Templates Are based on and benefit from all the dynamic capabilities of Java Servlet technology Every JSP is actually a servlet A first (small) step towards the role separation

The main features of JSP A JSP page is a text document that contains: static data (template) (HTML, SVG, WML, etc.) JSP elements, which construct the content JSP elements may be: JSP tags scriptles (code sequences written in Java...) JSP uses an expression language for accessing server-side objects The source files may be.jsp,.jspf,.jspx

The Life Cycle of a JSP Page

Translation Phase //Example taken from Apache Tomcat application server //This is the source file of the servlet generated for hello.jsp package org.apache.jsp; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.jsp.*; implements javax.servlet.servlet public final class hello_jsp extends org.apache.jasper.runtime.httpjspbase implements org.apache.jasper.runtime.jspsourcedependent {... public void _jspservice(httpservletrequest request, HttpServletResponse response) throws java.io.ioexception, ServletException {... out.write("<html>");... out.write("</html>"); } }

JSP Syntax <%-- Hello World example, not again... --%> JSP Comment <%@ page language="java" contenttype="text/html; pageencoding="utf-8"%> <html> <head> <title>first JSP</title> </head> <%@ page import="java.util.date" %> <%@ include file="header.html" %> JSP Directives <%! String message = "Hello World"; %> <body> <% for(int i=1; i<=4; i++) { out.println("<h" + i + ">" + message + "</H" + i + ">"); } %> <!-- Page generated at: <%= new Date() %> --> </body> </html> JSP Expression JSP Declaration JSP Scriptlet

JSP Elements Element Standard Syntax XML Syntax Comment <%--.. --%> <!--.. --> Declarations <%!..%> <jsp:declaration>.. </jsp:declaration> Directives <%@ include.. %> <%@ page.. %> <%@ taglib.. %> <jsp:directive.include.. /> <jsp:directive.page.. /> xmlns:prefix="tag library URL" Expressions <%=..%> <jsp:expression>.. </jsp:expression> Scriptles <%..%> <jsp:scriptlet>.. </jsp:scriptlet> Standard actions <jsp:action>.. </jsp:action> <jsp:usebean>.. </jsp:usebean> <jsp:forward>.. </jsp:forward> <jsp:include>.. </jsp:include>.. Everything else besides the JSP elements is considered static content and it appears unaltered in the output. <html> <!-- this is static content --> Hello world! </html>

Scopes

JSP Objects Object Type Scope request HttpServletRequest REQUEST response HttpServletResponse PAGE application ServletContext APPLICATION pagecontext PageContext PAGE out JSPWriter PAGE config ServletConfig PAGE page (this) HttpJspPage PAGE session HttpSession SESSION exception Throwable PAGE

Examples of using JSP objects Get a parameter from the request <%= request.getparameter("nume") %> Get the IP address of the client and the type of his browser <%= request.getremoteaddr() %> <%= request.getheader("user-agent") %> <% session.setattribute("user", "Duke"); synchronized (application) { application.setattribute("sharedobject", new Object()); } out.println("<h1> Hello </h1>"); response.sendredirect("http://www.google.ro"); response.senderror(404); %>

Exception Handling <%@ page errorpage="showerror.jsp" %> <html> <head> <title>error Handling Example</title> </head> <body> <% // Throw an exception to invoke the error page int x = 1; if (x == 1) { throw new RuntimeException("Boom!"); } %> </body> </html> Some JSP Page <%@ page iserrorpage="true" %> <html> <head> <title>show Error Page</title> </head> <body> <h1>opps...</h1> <p>sorry, an error occurred.</p> Error Handler <p>here is the exception stack trace: </p> <pre> <% exception.printstacktrace(response.getwriter()); %> </pre> </body> </html>

JSP Actions An action encapsulates some logic, that will be accesible using an XML tag The goal is: to eliminate scriptlets from a page to promote reusability There are two types of JSP actions: standard, of the form <jsp:action /> custom

JSP Standard Actions Accessing the Data Layer (The Model) <jsp:usebean> <jsp:setproperty> <jsp:getproperty> Dispatching the request... <jsp:forward> <jsp:include>

JavaBean Components Reusable components that encapsulate some data exposed as properties Described by classes that are public, serializable and have a default constructor JavaBeans are used to store information at a specific scope: request, session, etc. JSP Pages and Servlets will store and retrieve data using JavaBeans The Model

Example of a JavaBean package test; public class Person { Person is a bean private String name; private BigDecimal salary;... public String getname() { return name; } Name is a property public void setname(string name) { this.nume = nume; } }...

Standard Actions for JavaBeans Creating/Accessing an instance of the bean <jsp:usebean id="pers" class="test.person" scope="session" > pers.setname("duke");... </jsp:usebean> Accessing the properties of the bean <jsp:setproperty name="pers" property="name" value="joe" /> Person, your name is: <jsp:getproperty name="pers" property="name"/> A (small) step towards accesing the data layer without writing Java code...

Model 1

Using a JavaBean in a JSP <html> <jsp:usebean id="pers" scope="request" class="test.person" /> <%-- We use introspection to populate the bean --%> <jsp:setproperty name="pers" property="*" /> <% if ( request.getparameter("name") == null ) { %> <form method="get"> Nume: <input type="text" name="name" size="25" /> <br/> <input type="submit" value="ok"/> </form> <% } else { <!-- If we decide not to use introspection: <jsp:setproperty name="pers" property="name" value="<%= request.getparameter("name") %>" /> --> Salut, <jsp:getproperty name="pers" property="name" />! <% } %> </html>

Using a JavaBean in a servlet public void doget (HttpServletRequest request, HttpServletResponse response) { //Get the JavaBean from its scope Person person = (Person) request.getattribute("pers"); // Use the JavaBean person.setname("joe"); //Eventually, forward the request getservletcontext().getrequestdispatcher("result.jsp").forward(request, response); }

<jsp:forward page=... />

<jsp:include page=... /> <%@ include %> directive <jsp: include /> action

Request Chaining The first JSP of the chain maps the input elements of the form to the properties of the bean. <jsp:setproperty name="pers" property="name" value="*" />

Model View Controller