<% if (Math.random()<0.5){ %> <B>nice</B> day! <% } else { %> <B>lousy</B> day! <% } %>

Similar documents
Introduction to J2EE Web Technologies

Java Server Pages Tutorial

Complete Java Web Development

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

Java 2 Web Developer Certification Study Guide Natalie Levi

Building Web Applications, Servlets, JSP and JDBC

JSP Java Server Pages

Web Programming: Announcements. Sara Sprenkle August 3, August 3, Assignment 6 due today Project 2 due next Wednesday Review XML

2.8. Session management

Exam Prep. Sun Certified Web Component Developer (SCWCD) for J2EE Platform

INSIDE SERVLETS. Server-Side Programming for the Java Platform. An Imprint of Addison Wesley Longman, Inc.

Developing a J2EE Application. Web Auction. Gerald Mo

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

CS506 Web Design and Development Solved Online Quiz No. 01

Java 2 Web Developer Certification Study Guide Natalie Levi; Philip Heller

Class Focus: Web Applications that provide Dynamic Content

ACM Crossroads Student Magazine The ACM's First Electronic Publication

Web Container Components Servlet JSP Tag Libraries

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

Java Server Pages and Java Beans

11.1 Web Server Operation

If you wanted multiple screens, there was no way for data to be accumulated or stored

Glassfish, JAVA EE, Servlets, JSP, EJB

Oracle Forms Services Secure Web.Show_Document() calls to Oracle Reports Server 6i

Oracle Forms Services Secure Web.Show_Document() calls to Oracle Reports

Ch-03 Web Applications

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

CS506- Web Design and Development Solved Subjective From Final term Papers. Final term FALL(Feb 2012) CS506- Web Design and Development

Programming the Web Server. Robert M. Dondero, Ph.D. Princeton University

Usability. Usability

WIRIS quizzes web services Getting started with PHP and Java

Controlling Web Application Behavior

Tutorial: Building a Web Application with Struts

IT6503 WEB PROGRAMMING. Unit-I

Servlet 3.0. Alexis Moussine-Pouchkine. mercredi 13 avril 2011

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

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

Web Applications and Struts 2

CSc31800: Internet Programming, CS-CCNY, Spring 2004 Jinzhong Niu May 9, Java Servlets

Web Pages. Static Web Pages SHTML

Internet Technologies. World Wide Web (WWW) Proxy Server Network Address Translator (NAT)

Announcements. Comments on project proposals will go out by in next couple of days...

Model-View-Controller. and. Struts 2

Course Number: IAC-SOFT-WDAD Web Design and Application Development

Java Application Developer Certificate Program Competencies

Java 2 Web Developer Certification Study Guide Natalie Levi

An introduction to creating JSF applications in Rational Application Developer Version 8.0

CS108, Stanford Handout #33 Young. HW5 Web

Course Name: Course in JSP Course Code: P5

AN OVERVIEW OF SERVLET AND JSP TECHNOLOGY

Building Java Servlets with Oracle JDeveloper

Java 7 Recipes. Freddy Guime. vk» (,\['«** g!p#« Carl Dea. Josh Juneau. John O'Conner

The Web Web page Links 16-3

Bitrix Site Manager 4.0. Quick Start Guide to Newsletters and Subscriptions

CIS 455/555: Internet and Web Systems

Building a Multi-Threaded Web Server

INTRODUCTION TO WEB TECHNOLOGY

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

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

The end. Carl Nettelblad

Managing Data on the World Wide-Web

Tableau Server Trusted Authentication

Load Balancing BEA WebLogic Servers with F5 Networks BIG-IP

Managed Beans II Advanced Features

Java Server Pages (JSP)

Web Frameworks and WebWork

Developing XML Solutions with JavaServer Pages Technology

Java Enterprise Edition The Web Tier

OVERVIEW OF ASP. What is ASP. Why ASP

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

Development. with NetBeans 5.0. A Quick Start in Basic Web and Struts Applications. Geertjan Wielenga

Please send your comments to:

Arjun V. Bala Page 25

7 Why Use Perl for CGI?

Instructor: Betty O Neil

OCS Training Workshop LAB13. Ethernet FTP and HTTP servers

LabVIEW Internet Toolkit User Guide

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

Web Applications. For live Java training, please see training courses at

Client-server 3-tier N-tier

Client-Server Architecture & J2EE Platform Technologies Overview Ahmed K. Ezzat

A Java proxy for MS SQL Server Reporting Services

SSC - Web applications and development Introduction and Java Servlet (II)

Building Web Applications with Servlets and JavaServer Pages

Tableau Server Trusted Authentication

Get Success in Passing Your Certification Exam at first attempt!

Java Servlet Specification Version 2.3

Module 13 Implementing Java EE Web Services with JAX-WS

COMP 112 Assignment 1: HTTP Servers

An introduction to web programming with Java

Xtreeme Search Engine Studio Help Xtreeme

10CS73:Web Programming

SOA Software API Gateway Appliance 7.1.x Administration Guide

A DIAGRAM APPROACH TO AUTOMATIC GENERATION OF JSP/SERVLET WEB APPLICATIONS

1 Introduction: Network Applications

Java Servlet 3.0. Rajiv Mordani Spec Lead

Web development... the server side (of the force)

A Pattern Language for J2EE Web Component Development 1 Chris Richardson 2

Load Balancing IBM WebSphere Servers with F5 Networks BIG-IP System

Recommended readings. Lecture 11 - Securing Web. Applications. Security. Declarative Security

Transcription:

Java Server Pages (JSP) Let you separate the dynamic part of your pages from the static HTML. The JSP page just gets converted to a normal servlet: Static HTML simply being printed to the output stream associated with the servlet's service (doget, dopost) method. This is normally done the first time the page is requested. Aside from the regular HTML, there are three main types of JSP constructs that you embed in a page: o Scripting elements: Java code that will become part of the resultant servlet o Directives: control the overall structure of the servlet o Actions specify existing components that should be used and otherwise control the behavior of the JSP engine. To simplify the scripting elements, you have access to a number of predefined variables Static HTML (Template Text) Template text looks just like normal HTML, follows all the same syntax rules, and is simply "passed through" to the client by the servlet created to handle the page. The one minor exception to the "template text is passed straight through" rule is that, if you want to have "<%" in the output, you need to put "<\%" in the template text. JSP Scripting Elements JSP scripting elements let you insert Java code into the servlet that will be generated from the current JSP page. There are three forms: JSP expression is used to insert Java values directly into the output. o It has the one of the following forms: <%= Java Expression %> <jsp:expression>java Expression</jsp:expression> o The Java expression is evaluated, converted to a string, and inserted in the page. This evaluation is performed when the page is requested and thus has full access to information about the request. E.g.: Your hostname: <%= request.getremotehost() %> JSP scriptlets let you insert arbitrary code into the servlet method that will be built to generate the page. o Scriptlets have one of the following forms: <% Java Code %> <jsp:scriptlet>code</jsp:scriptlet> o A code inside a scriptlet gets inserted exactly as written and any static HTML before or after a scriptlet gets converted to print statements. <% if (Math.random()<0.5){ %> <B>nice</B> day! <% } else { %> <B>lousy</B> day! <% } %> if (Math.random() < 0.5) { out.println("<b>nice</b> day!"); } else { out.println("<b>lousy</b> day!"); }

JSP declarations define methods or fields that get inserted into the main body of the servlet class (outside of the service method processing the request). o It has one of the following forms: <%! Java Code %> <jsp:declaration>code</jsp:declaration> o Do not generate any output o Used in conjunction with JSP expressions or scriptlets. E.g.: <%! private int accesscount = 0; %>Accesses to page since server reboot: <%= ++accesscount %> JSP Directives Affect the overall structure of the servlet class. It usually has the following form: <%@ directive attribute="value" %> <%@ directive attribute1="value1"... attributen="valuen" %> <jsp:directive.directivetype attribute=value /> There are two main types of directive: The page directive lets you define one or more of the following case-sensitive attributes: o import="package.class" - package to be imported. E.g. : <%@ page import="java.util.*" %> o contenttype="mime-type" - The default is text/html. E.g. <%@ page contenttype="text/plain" %> equals to <% response.setcontenttype("text/plain"); %> o isthreadsafe="true false". True (the default) - multiple requests processed with a single servlet instance (assuming synchronized access provided) False - servlet should implement SingleThreadModel o session="true false". True (the default) - the predefined variable session should be bound to the existing session if one exists, otherwise a new session should be created and bound to it. False - no sessions used o buffer="sizekb none" - specifies the buffer size for the JspWriter out. The default is server-specific, but must be at least 8kb. o autoflush="true false". True (the default) - the buffer should be flushed when full. False - an exception should be thrown when the buffer overflows. o extends="package.class" - superclass of servlet that will be generated o info="message" - a string retrieved via the getservletinfo method o errorpage="url" - specifies a JSP page that should process any Throwables thrown but not caught in the current page. o iserrorpage="true false" - whether or not the current page can act as the error page for another JSP page. The default is false. o language="java" - intended to specify the underlying language. For now java is both the default and the only legal choice.

JSP include directive - lets you include files at the time the JSP page is translated into a servlet. o E.g. <BODY><%@ include file="/navbar.html" %></BODY> o The directive inserts the files at the time the page is translated, thus if the navigation bar changes, you need to re-translate all the JSP pages that refer to it. Predefined Variables To simplify code in JSP expressions and scriptlets, you are supplied with eight automatically defined variables, sometimes called implicit objects: request: o HttpServletRequest associated with the request o Lets you look at the request parameters (via getparameter) the request type (GET, POST, HEAD, etc.) the incoming HTTP headers (cookies, Referer, etc.). Strictly speaking, request is allowed to be a subclass of ServletRequest other than HttpServletRequest, if the protocol in the request is something other than HTTP. This is almost never done in practice. response: o HttpServletResponse associated with the response to the client. out: o PrintWriter used to send output to the client (a buffered version of PrintWriter called JspWriter). session: o HttpSession object associated with the request. o Sessions are created automatically, so this variable is bound even if there was no incoming session reference. application: o ServletContext as obtained via getservletconfig().getcontext(). config: o ServletConfig object for this page. pagecontext: o PageContext - encapsulates use of server-specific features like higher performance JspWriters. The idea is that, if you access them through this class rather than directly, your code will still run on "regular" servlet/jsp engines. page: a synonym for this Actions JSP actions control the behavior of the servlet engine. Available actions include: The jsp:include Action - lets you insert files into the page being generated. o The syntax looks like this: <jsp:include page="relurl" flush="true" /> o Inserts the file at the time the page is requested.

Cons: penalty in efficiency, precludes the included page from containing general JSP code (e.g. cannot set HTTP headers) Pros: flexibility. (E.g. each time the headlines change, author leave the main JSP page unchanged). The jsp:usebean Action - lets you load in a JavaBean to be used in the JSP page. o The simplest syntax for specifying that a bean should be used is: <jsp:usebean id="name" class="package.class" /> <jsp:usebean...>body</jsp:usebean> o This means instantiating an object of the specified class and binding it to a variable with the name specified by id (only if there is no existing one with the same id and scope) obtaining references to existing beans o Attributes that you can use: id - A name of the variable that will reference the bean class - Designates the full package name of the bean. scope - context in which the bean should be made available: page, request, session, and application. The reason that the scope matters is that a jsp:usebean entry will only result in a new object being instantiated if there is no previous object with the same id and scope. type - type of the variable that will refer to the object (must match the classname or be a superclass or an implemented interface). beanname - The name of the bean, supplied to the instantiate method of Beans. It is permissible to supply a type and a beanname, and omit the class attribute. jsp:setproperty gives values to properties of beans You can use it o after, but outside of, a jsp:usebean element, as below: is executed regardless of whether a new bean was instantiated or an existing bean was found. o inside the body of a jsp:usebean element, as below: is executed only if a new object was instantiated, not if an existing one was found. o Possible attributes: name - designates the bean entity property the property to set ("*" means that all request parameters whose names match bean property names will be passed to the appropriate setter methods) value optional, value for the property param optional, designates the request parameter from which the property should be derived. If you omit both value and param, it is the same as if you supplied a param name that matches the property name. The jsp:getproperty Action - retrieves the value of a bean property, converts it to a string, and inserts it into the output. The two required attributes are: o name - the name of a bean previously referenced via jsp:usebean o property - the property whose value should be inserted

The jsp:forward Action - lets you forward the request to another page. It has a single attribute, page, which should consist of a relative URL. The jsp:plugin Action - lets you insert the browser-specific OBJECT or EMBED element needed to specify that the browser run an applet using the Java plugin. Comments and Character Quoting Conventions There are a small number of special constructs you can use in various cases to insert comments or characters that would otherwise be treated specially. Here's a summary: Syntax <%-- comment -- %> <!-- comment -- > Purpose A JSP comment. Ignored by JSP-to-scriptlet translator. Any embedded JSP scripting elements, directives, or actions are ignored. An HTML comment. Passed through to resultant HTML. Any embedded JSP scripting elements, directives, or actions are executed normally. <\% Used in template text (static HTML) where you really want "<%". %\> Used in scripting elements where you really want "%>". \' \" A single quote in an attribute that uses single quotes. Remember, however, that you can use either single or double quotes, and the other type of quote will then be a regular character. A double quote in an attribute that uses double quotes. Remember, however, that you can use either single or double quotes, and the other type of quote will then be a regular character. %\> %> in an attribute. <\% <% in an attribute.