Java Server Pages (JSP)



Similar documents
2. Follow the installation directions and install the server on ccc

Java Server Pages and Java Beans

2.8. Session management

Web Programming II JSP (Java Server Pages) ASP request processing. The Problem. The Problem. Enterprise Application Development using J2EE

Outline. CS 112 Introduction to Programming. Recap: HTML/CSS/Javascript. Admin. Outline

Servlets. Based on Notes by Dave Hollinger & Ethan Cerami Also, the Online Java Tutorial by Sun

Hello World RESTful web service tutorial

11.1 Web Server Operation

Outline. Lecture 9: Java Servlet and JSP. Servlet API. HTTP Servlet Basics

c. Write a JavaScript statement to print out as an alert box the value of the third Radio button (whether or not selected) in the second form.

JavaScript Introduction

Intell-a-Keeper Reporting System Technical Programming Guide. Tracking your Bookings without going Nuts!

JWIG Yet Another Framework for Maintainable and Secure Web Applications

Working with forms in PHP

JavaScript and Dreamweaver Examples

Application Security

Web Container Components Servlet JSP Tag Libraries

Internet Technologies

Server-Side Web Development JSP. Today. Web Servers. Static HTML Directives. Actions Comments Tag Libraries Implicit Objects. Apache.

Developing XML Solutions with JavaServer Pages Technology

2- Forms and JavaScript Course: Developing web- based applica<ons

JSP Java Server Pages

Sample HP OO Web Application

Creating Java EE Applications and Servlets with IntelliJ IDEA

Database Applications Recitation 10. Project 3: CMUQFlix CMUQ s Movies Recommendation System

Agenda. Summary of Previous Session. Application Servers G Session 3 - Main Theme Page-Based Application Servers (Part II)

SSC - Web development Model-View-Controller for Java web application development

Services. Custom Tag Libraries. Today. Web Development. Role-Based. Development. Code Reuse. Tag Libraries Custom Tags. Tag Lifecycle.

Pure server-side Web Applications with Java, JSP. Application Servers: the Essential Tool of Server-Side Programming. Install and Check Tomcat

Web Programming with Java Servlets

Yandex.Widgets Quick start

Real SQL Programming 1

Novell Identity Manager

Forms, CGI Objectives. HTML forms. Form example. Form example...

Tutorial: Using PHP, SOAP and WSDL Technology to access a public web service.

Web Development and Core Java Lab Manual V th Semester

CS170 Lab 11 Abstract Data Types & Objects

CIS 445 Advanced Visual Basic.NET ASP.NET

CS506 Web Design and Development Solved Online Quiz No. 01

Implementing Specialized Data Capture Applications with InVision Development Tools (Part 2)

Java EE Introduction, Content. Component Architecture: Why and How Java EE: Enterprise Java

Configuring iplanet 6.0 Web Server For SSL and non-ssl Redirect

CGI Programming. What is CGI?

«W3Schools Home Next Chapter» JavaScript is THE scripting language of the Web.

Viewing Form Results

Creating a Simple, Multithreaded Chat System with Java

Introduction to J2EE Web Technologies

Lab 5 Introduction to Java Scripts

JavaScript Basics & HTML DOM. Sang Shin Java Technology Architect Sun Microsystems, Inc. sang.shin@sun.com

Client-side Web Engineering From HTML to AJAX

Controlling Web Application Behavior

CS412 Interactive Lab Creating a Simple Web Form

BAPI. Business Application Programming Interface. Compiled by Y R Nagesh 1

Class Focus: Web Applications that provide Dynamic Content

JAVASCRIPT AND COOKIES

ACM Crossroads Student Magazine The ACM's First Electronic Publication

Hello World Portlet Rendered with JSP for WebSphere Portal Version 4.1

Website Login Integration

NGASI AppServer Manager SaaS/ASP Hosting Automation for Cloud Computing Administrator and User Guide

JavaScript By: A. Mousavi & P. Broomhead SERG, School of Engineering Design, Brunel University, UK

JavaScript: Arrays Pearson Education, Inc. All rights reserved.

MASTERTAG DEVELOPER GUIDE

7 Web Databases. Access to Web Databases: Servlets, Applets. Java Server Pages PHP, PEAR. Languages: Java, PHP, Python,...

<option> eggs </option> <option> cheese </option> </select> </p> </form>

Form Validation. Server-side Web Development and Programming. What to Validate. Error Prevention. Lecture 7: Input Validation and Error Handling

Website as Tool for Compare Credit Card Offers

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

WIRIS quizzes web services Getting started with PHP and Java

PHP Tutorial From beginner to master

Part I. Multiple Choice Questions (2 points each):

Configuring Tomcat for a Web Application

PA165 - Lab session - Web Presentation Layer

Perl/CGI. CS 299 Web Programming and Design

Explain the relationship between a class and an object. Which is general and which is specific?

It has a parameter list Account(String n, double b) in the creation of an instance of this class.

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

HTML Forms and CONTROLS

Web Programming with PHP 5. The right tool for the right job.

Dynamic Web Pages With The Embedded Web Server. The Digi-Geek s AJAX Workbook

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

AP Computer Science Java Subset

Basic Java Syntax. Program Structure

TABLE OF CONTENTS...2 INTRODUCTION...3 APPLETS AND APPLICATIONS...3 JAVABEANS...4 EXCEPTION HANDLING...5 JAVA DATABASE CONNECTIVITY (JDBC)...

Example of a Java program

No no-argument constructor. No default constructor found

HTML Tables. IT 3203 Introduction to Web Development

Motivation retaining redisplaying

Specialized Programme on Web Application Development using Open Source Tools

Transcription:

Java Server Pages (JSP) What is JSP JSP simply puts Java inside HTML pages. You can take any existing HTML page and change its extension to ".jsp" instead of ".html". Scripting elements are used to provide dynamic pages

JSP and Servlets Each JSP page is turned into a Java servlet, compiled and loaded. This compilation happens on the first request. After the first request, the file doesn't take long to load anymore. Every time you change the JSP file, it will be re-compiled again. Java Server Page translator Java servlet source code compiler Java Servlet class file Generated servlets You can examine the source code produced by the JSP translation process. There is a directory called generated in Sun Java J2EE Application Server where you can find the source code. Note that the _jspservice method corresponds to the servlet service method (which is called by doget or dopost)

JSP elements (overview) Directives of the form <%@... %> Scripting elements Expressions of the form <%= expr %> Scriptlets of the form <% code %> Declarations of the form <%! code %> JSP Comments <%--... --%> Standard actions Example: <jsp:usebean>... </jsp:usebean> Implicit variables like request, response, out JSP Directives They have the form <%@ name attribute1="...", attribute2="..."... %> page include taglib Specify page properties Include a file at translation time Specify custom tags

JSP Directive Examples Import java packages <%@ page import="java.util.*,java.sql.*" %> Multiple import statements <%@ page import="java.util.*" %> <%@ page import="java.sql.*" %> Including file at translation time <%@ include file="header.html" %> For include the path is relative to the JSP file JSP Scripting Elements: Expressions For an expression scripting element like <%= expr %>, expr is evaluated and the result is converted to a string and placed into the JSP's servlet output stream. In a Java servlet this would be equivalent to PrintWriter out = response.getwriter();... out.print(expr);

JSP Expression Examples Displaying request parameters (request is an implicit object available in a JSP) Your name is <%= request.getparameter("name") %> and your age is <%= request.getparameter("age") %> Doing calculations The value of pi is <%= Math.PI %> and the square root of two is <%= Math.sqrt(2.0) %> and today's date is <%= new java.util.date() %>. JSP Scripting Elements: Scriptlet For a scriplet <% statements %> the Java statements are placed in the translated servlet's _jspservice method body (which is called by either doget or dopost) public void _jspservice(httpservletrequest request, HttpServletResponse response) throws java.io.ioexception, ServletException {... statements...

JSP Scriplet Examples Check a request parameter <% String name = request.getparameter("name"); if (name == null) { %> <h3>please supply a name</h3> <% else { %> <h3>hello <%= name %></h3> <% %> In this example, there are 3 scriptlets elements and one expression element JSP Scripting Elements: Declaration For a declaration <%! declarations %> the Java statements are placed in the class outside the _jspservice method. Declarations can be Java instance variable declarations or Java methods // declarations would go here public void _jspservice(...) {...

JSP Declaration examples Declaring instance variables <%! private int count = 0; %>... The count is <%= count++ %>. Declaring methods <%! private int toint(string s) { return Integer.parseInt(s); %> Including Files Including files at translation time (when JSP is translated to a servlet) <%@ include file="filename" %> Including files at request time <jsp:include page="filename" flush = "true" />

A simple JSP <html> <head><title>jsp Test</title></head> <body> <h1>jsp Test</h1> Time: <%= new java.util.date() %> </body> </html> The expression scripting element <%=... %> is equivalent to the scriptlet <% out.print(...); %> The Implicit out Object In a scriptlet <%... %> you can use the out object to write to the output stream. Example: <% %> out.print("the sum is "); out.print("1 + 2 = " + (1+2));

The Implicit request Object In a scriptlet <%... %> you can use the request object to access GET/POST parameters. Example: <html> <head><title>...</title></head> <body> <h1>...</h1> <p><%= request.getparameter("greeting") %></p> </body></html> Example: HTML Form Submission Using GET <html> <head><title>jsp Processing...</title></head> <body> <h1>jsp Processing form with GET</h1> <form action= processform.jsp" method="get"> First name: <input type="text" name="firstname"><br /> Last name: <input type="text" name="lastname"> <p><input type="submit" name="button" value="submitname"></p> </form> </body> </html>

Example: HTML Form Submission Using POST <html> <head><title>jsp Processing...</title></head> <body> <h1>jsp Processing form with POST</h1> <form action= processform.jsp" method="post"> First name: <input type="text" name="firstname"><br /> Last name: <input type="text" name="lastname"> <p><input type="submit" name="button" value="submitname"></p> </form> </body> </html> Processing Submitted HTML Forms ProcessForm.jsp: <html> <head> <title>jsp Form Results</title> </head> <body> <h1>jsp Form Results</h1> Hello <%= request.getparameter("firstname") %> <%= request.getparameter("lastname") %> </body> </html>

Temperature Conversion Example <%@ page import="java.text.decimalformat" %> <html> <head><title>fahrenheit... Conversion</title></head> <body> <h1>fahrenheit to Celsius Conversion</h1> <% String self = request.getrequesturi(); if (request.getparameter("convert") == null) { %> <form action="<%= self %>" method="post"> Fahrenheit temperature: <input type="text" name="fahrenheit" /> <p><input type="submit" name="convert" value="convert to Celsius" /></p> </form> Temperature Conversion Example (Contd.) <% else { double fahr = 0.0; try { fahr = Double.parseDouble( request.getparameter("fahrenheit")); catch (NumberFormatException e) { // do nothing, accept default value

Temperature Conversion Example (Contd.) double celsius = (fahr - 32.0) * (5.0/9.0); DecimalFormat f2 = new DecimalFormat("0.00"); %> <%= f2.format(fahr) %>F is <%= f2.format(celsius) %>C <p><a href="<%= self %>">Another conversion</a> </p> <% %> </body> </html> Java Beans Special classes that encapsulate some data They have a default constructor get and set methods for data fields (properties) A bean can be constructed in JSP using <jsp:usebean id = "..." class = "..." /> If the bean already exists this statement does nothing

Setting properties To set a property of a bean use <jsp:setproperty name="..." property="..." value="..." /> To set a property using the value of a request parameter use <jsp:setproperty name="..." property="..." param="..." /> Getting properties To get a property of a bean use <jsp:getproperty name="..." property="..." />

A Greeting Bean package beans; public class Greeting { private String greeting; // the property public Greeting() { greeting = "Hello World"; public String getgreeting() { return greeting; public void setgreeting(string g) { greeting = (g == null)? "Hello World" : g; Java Beans Naming convention If the property name is greeting The get method must have the name getgreeting The set method must have the name setgreeting

Creating a Bean Create a bean and use default property <jsp:usebean id="hello" class="beans.greeting" /> Create a bean and set its property when it is constructed <jsp:usebean id="hello" class="beans.greeting" > <jsp:setproperty name="hello" property="greeting" value="hello JSP World" /> </jsp:usebean> Here <jsp:setproperty> is in the body of the <jsp:usebean> element. Creating a Bean (Contd.) Create a bean and set its property after it has been constructed <jsp:usebean id="hello" class="beans.greeting" /> <jsp:setproperty name="hello" property="greeting" value="hello JSP World" /> The <jsp:setproperty> tag is now outside the <jsp:usebean> tag so it will always set the property, not just when the bean is constructed

Example 1: Using Java Beans in JSP Files <jsp:usebean id="hello" class="beans.greeting" /> <jsp:setproperty name="hello" property="greeting" value="hello JSP World" /> <html> <head> <title>greeting JSP that uses a Greeting bean</title> </head> <body> <h1>greeting JSP that uses a Greeting bean</h1> <p><jsp.getproperty name="hello" property="greeting" /> </p> </body> </html> Example 2: Using Java Beans in JSP Files Two Beans: One initialized explicitly and the other is initialized using a request parameter <jsp:usebean id="greet1" class="beans.greeting" /> <jsp:usebean id="greet2" class="beans.greeting" /> <jsp:setproperty name="greet1" property="greeting" value="hello JSP World" /> <jsp:setproperty name="greet2" property="greeting" param="greeting" /> <html><head><title>greeting JSP using two Greeting beans</title></head><body> <h1>greeting JSP using two Greeting beans</h1> <p>1st bean: <jsp:getproperty name="greet1" property="greeting" /></p> <p>2nd bean: <jsp:getproperty name="greet2" property="greeting" /></p></body></html>

Example 3: Using Java Beans in JSP Files Three Beans: One initialized explicitly, one is initialized using a request parameter, and one is initialized using getparameter <jsp:usebean id="greet1" class="beans.greeting" /> <jsp:usebean id="greet2" class="beans.greeting" /> <jsp:usebean id="greet3" class="beans.greeting" /> <jsp:setproperty name="greet1" property="greeting" value="hello JSP World" /> <jsp:setproperty name="greet2" property="greeting" param="greeting" /> <%-- Following works but param method is better --%> <jsp:setproperty name="greet3" property="greeting" value="<%= request.getparameter(\"greeting\") %>" /> Reference COS 2206 JSP, Barry G. Adams, Department of Mathematics and Computer Science, Laurentain University.