Developing JavaServer Pages

Slides:



Advertisements
Similar presentations
8 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: JavaServer Pages.
Advertisements

Java Server Pages (JSP)
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.
JSP1 Java Server Pages (JSP) Introducing JavaServer Pages TM (JSP TM ) JSP scripting elements.
 2002 Prentice Hall. All rights reserved. Chapter 9: Servlets Outline 9.1 Introduction 9.2 Servlet Overview and Architecture Interface Servlet and.
Chapter 51 Scripting With JSP Elements JavaServer Pages By Xue Bai.
JSP – Java Server Pages Part 1 Representation and Management of Data on the Internet.
WEB1P servintro1 Introduction to servlets and JSP Dr Jim Briggs.
18-Jun-15 JSP Java Server Pages Reference: Tutorial/Servlet-Tutorial-JSP.html.
JSP Java Server Pages Reference:
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,
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.
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.
Introduction to Java web programming Dr Jim Briggs JWP intro1.
1 CIS336 Website design, implementation and management (also Semester 2 of CIS219, CIS221 and IT226) Lecture 9 JavaServer Pages (JSP) (Based on Møller.
Java Server Pages CS-422. What are JSPs A logical evolution of java servlets –most servlets dynamically create HTML and integrate it with some computational.
1 JSP – Java Server Pages Representation and Management of Data on the Internet.
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 JSP Based on: Marty Hall, Larry Brown, Core Servlets and JavaServer Pages.
JSP Java Server Pages Softsmith Infotech.
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.
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.
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.
JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA James Faeldon CS 119 Enterprise Systems Programming.
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.
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.
JSP Fundamentals Elements of a JSP Using Beans with JSP Integrating Servlets and JSP.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 27 JavaServer Page.
CSC 2720 Building Web Applications JavaServer Pages (JSP) The Basics.
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.
Java Server Pages (JSP)
Java Server Pages An introduction to JSP. Containers and Components Several clients – one system.
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.
Java Server Pages Introduction. Servlet Drawbacks Web page designer will need to know servlets to design the page. Servlet will have to be compiled for.
Chapter 11 Invoking Java Code with JSP Scripting Elements.
CSC 2720 Building Web Applications JavaServer Pages (JSP) JSP Directives and Action Elements.
JSP. Types of JSP Scripting Elements Expressions of the form, which are evaluated and inserted into the servlet's output. Scriptlets of the form, which.
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.
STRUCTURE OF JSP PRESENTED BY: SIDDHARTHA SINGH ( ) SOMYA SHRIVASTAV ( ) SONAM JINDAL ( )
JSP – Java Server Page DBI – Representation and Management of Data on the Internet.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 43 JavaServer Page.
©SoftMoore ConsultingSlide 1 Overview of Servlets and JavaServer Pages (JSP)
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.
1 Chapter 27 JavaServer Page. 2 Objectives F To know what is a JSP page is processed (§27.2). F To comprehend how a JSP page is processed (§27.3). F To.
 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.
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.
JSP Directive and Objects. JSP Directives As discussed before There are 3 types of elements in JSP Directive Elements Scripting Elements Standard Action.
JSP Java Server Pages. Hello, !
Introduction to Servlets
JSP Elements Chapter 2.
Java Server Pages.
Pre-assessment Questions
Invoking Java Code from JSP
Java Server Pages (JSP)
Java Server Pages.
JSP Directives 1-Jan-19.
COP 4610L: Applications in the Enterprise Spring 2005
JSP implicit objects & directive elements
COSC 2956 Internet Tools Java Server Pages.
Introduction to JSP Dept. of B.Voc Software Development and System Administration St. Joseph’s College(Autonomous) Trichy-02 By Dr. J. Ronald Martin Introduction.
Presentation transcript:

Developing JavaServer Pages

JSP Technology JavaServer Pages (JSP) enable you to write standard HTML pages contaning tags that run powerful programs based on the Java programming language.

Hello Servlet Code public void generateResponse(HttpServletRequest request, HttpServletResponse response) throws IOException { // Determine the specified name (or use default) String name = request.getParameter("name"); if ( (name == null) || (name.length() ==0) ) { name = DEFAULTJSTAME; // Specify the content type is HTML response.setContentType("text/html"); PrintWriter out = response.getWriter(); // Generate the HTML response out.printIn("<HTML>"); out.println("<HEAD>"); out.println("<TITLE>Hello Servlet</TITLE>"); out.println("</HEAD>"); out.println("<B0DY BGCOLOR='white'>"); out.printIn("<B>Hello, " + name + "</B>"); out.printIn("</BODY>"); out.println("</HTML>"); out. close () ;

Hello JSP Code <%! private static final String DEFAULT_NAME = "World"; %> <HTML> <HEAD> <TITLE>Hello JavaServer Page</TITLE> </HEAD> <%-- Determine the specified name (or use default) --%> <% String name = request.getParameter("name"); if ( (name == null) || (name.length() ==0) ) { name = DEFAULT NAME; } %> <BODY BGCOLOR='white'> <B>Hello, <%= name %></B> </BODY> </HTML>

JSP Scripting Elements JSP scripting elements are embedded with the <% %> tags and are processed by the JSP engine during translation of the JSP page. <HTML> <%-- scripting element --%> </HTML>

JSP Scripting Elements There are five types of scripting elements: Comments <%-- comment --%> Directive tag <%@ directive %> Declaration tag <%! dec! %> Scriptlet tag <% code %> Expression tag <%= expr' %>

Comments Documentation is important to any application. There are three types of comments permitted in a JSP page: HTML comments <!-- This is an HTML comment. It will show up in the response. --> JSP page comments <%-- This is a JSP comment. It will only be seen in the JSP code. It will not show up in either the servlet code or the response. --%>

Comments Java comments can be embedded with scriptlet and declaration tags. /* This is a Java comment. It will show up in the servlet code. It will not show up in the response. */

<%@ DirectiveName [attr="value"]* %> Directive Tag A directive tag provides information that will affect the overall translation of the JSP page. The syntax for a directive tag is: <%@ DirectiveName [attr="value"]* %> Examples: <%@ page session="false" %> <%@ include file="incl/copyright.html" %>

<%@ page import="java.util.Date" %> The page Directive The page directive is used to modify the overall translation of the JSP page. <%@ page import="java.util.Date" %> Note: You can have more than one page directive, but can only declare any given attribute once per page, except for the import attribute. You can place a page directive anywhere in the JSP file. It is good practice to make the page directive the first statement in the JSP file.

The page Directive The page directive defines a number of page-dependent properties and communicates these to the Web container during translation time. The language attribute defines the scripting language to be used in the page. The extends attribute defines the (fully-qualified) class name of the superclass of the servlet class that is generated from this JSP page.

The page Directive The import attribute defines the set of classes and packages that must be imported in the servlet class definition. For example: import="java.sql.Date,java.util.*,j ava.text.*" The session attribute defines whether the JSP page is participating in an HTTP session. The value is either true (the default) or false.

The page Directive The buffer attribute defines the size of the buffer used in the outputstream (a JspWriter object). The value is either none or Nkb. For example: buffer="8kb" or buffer="none“ The autoFlush attribute defines whether the buffer output should be flushed automatically when the buffer is filled or whether an exception is thrown. The value is either true (automatically flush) or false (throw an exception).

The page Directive The isThreadSafe attribute allows the JSP page developer to declare that the JSP page is thread-safe or not. If the value is set to false, then this attribute instructs JSP parser to write the servlet code such that only one HTTP request will be processed at a time. The default value is true. The info attribute defines an informational string about the JSP page. The errorPage attribute indicates another JSP page that will handle all runtime exceptions thrown by this JSP page. The value is a URL that is either relative to the current Web hierarchy or relative to the context root.

The page Directive The isErrorPage attribute defines that the JSP page has been designed to be the target of another JSP page's errorPage attribute. The value is either true or false (default). All JSP pages that "are an error page" automatically have access to the exception implicit variable. The content Type attribute defines the MIME type of the output stream. The default is text/html. The pageEncoding attribute defines the character encoding of the output stream. The default is BO-8859-1. Other character encodings permit the inclusion of non-Latin character sets, such as Kanji or Cyrillic.

<%! JavaClassDeclaration %> Declaration Tag A declaration tag allows you to include members in the JSP servlet class, either attributes or methods. The syntax for a declaration tag is: <%! JavaClassDeclaration %> Examples: <%! public static final String DEFAULT_NAME = "World"; %> <%! public String getName(HttpServletRequest request) { return request.getParameter("name");} <%! int counter = 0; %>

Declaration Tag You can use a declaration tag to override the jsplnit and jspDestory life cycle methods. The signature of these methods has no arguments and returns void. For example: <%! public void jsplnit() { /* initialization code here */ public void jspDestroyO { /* clean up code here */ %>

Scriplet Tag A scriptlet tag allows the JSP page developer to include arbitrary Java technology code in the _j spService method. The syntax for a declarations tag is: <% JavaCode %> Examples: <% int i = 0; %> <% if ( i > 10 ) { %> I is a big number. <% } else { %> I is a small number <% } %>

Scriplet Tag <TABLE BORDERS=‘1’ CELLSPACING='0' CELLPADDING='5'> <TR><TH>number</TH> <TH>squared</TH> </TR> <% for ( int i=0; i<10; i++ ) { %> <TR><TD><%= i %></TD> <TD><%= (i * i) %></TD> <% } %> </TABLE>

<%= JavaExpression %> Expression Tag An expression tag holds a Java language expression that is evaluated during an HTTP request. The result of the expression is included in the HTTP response stream. The syntax for a declaration tag is: <%= JavaExpression %> Examples: <B>Ten is <%= (2*5) %></B> Thank you, <I><%= name %></l>, for registering for the soccer league. The current day and time is: <%= new java.util.Date() %>

Implicit Variables The Web container gives the JSP technology developer access to the following variables in scriptlet and expression tags. These variables represent commonly used objects for servlets that JSP developers might need to use. Variable name Description request The HttpServletRequest object associated with the request. response The HttpServletResponse object associated with the response that is sent back to the browser. out The JspWriter object associated with the output stream of tl response. session The HttpSession object associated with the session for the given user of the request. This variable is only meaningful if the JSP page is participating in an HTTP session.

Implicit Variables Variable name Description application The ServletContext object for the Web application. config The ServletConf ig object associated with the servlet for th JSP page. pageContext This object encapsulates the environment of a single request for this JSP page. page This variable is equivalent to the this variable in the Java programming language. exception The Throwable object that was thrown by some other JSP page. This variable is only available in a "JSP error page."

JSP Page Exception Handling The JSP specification provides a different mechanism for handling exceptions. The page directive allows you to specify another JSP page to be activated whenever the JSP page throws an exception.

Declaring an Error Page The errorPage attribute of the page directive is used to declare the error page to be used by the Web container when this JSP page throws an exception.

Developing an Error Page The errorPage attribute of the page directive is used to declare that this JSP page has access to the exception implicit variables. This is used by the Web container when another JSP page throws an exception.