Java Server 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.
Chapter 51 Scripting With JSP Elements JavaServer Pages By Xue Bai.
Objectives Ch. D - 1 At the end of this chapter students will: Know the general architecture and purpose of servlets Understand how to create a basic servlet.
CSE 190: Internet E-Commerce Lecture 7. HTML Templates Designed to separate server side logic from HTML presentation Key features –Escapes from HTML into.
18-Jun-15 JSP Java Server Pages Reference: Tutorial/Servlet-Tutorial-JSP.html.
JSP Java Server Pages Reference:
DT228/3 Web Development JSP: Directives and Scripting elements.
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,
Introduction to Servlet & JSP
Comp2513 Java Server Pages Daniel L. Silver, Ph.D.
Three types of scripting elements: 1.Expressions 2.Scriptlets 3.Declarations Scripting elements.
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.
CSCI 6962: Server-side Design and Programming History and Background.
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.
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.
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.
Java Server Pages Lecture July Java Server Pages Java Server Pages (JSPs) provide a way to separate the generation of dynamic content (java)
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.
JSP Fundamentals Elements of a JSP Using Beans with JSP Integrating Servlets and JSP.
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.
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.
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.
JavaServer Page by Antonio Ko. Overview ► Introduction ► What is a servlet? ► What can servlets do? ► Servlets Vs JSP ► Syntax ► Samples ► JavaBean ►
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.
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.
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 ( )
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.
INVOKING JAVA CODE WITH JSP SCRIPTING ELEMENTS. Creating Template Text A large percentage of your JSP document consists of static text (usually HTML),
Introduction To HTML Dr. Magdi AMER. HTML elements.
How CGI and Java Servlets are Run By David Stein 14 November 2006.
 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 Programming: Advanced Topics 1 Building Web Applications Chapter 13.
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.
Apr 3, 2013 JSP Java Server Pages. 2 A “Hello World” servlet (from the Tomcat installation documentation) public class HelloServlet extends HttpServlet.
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 Java Server Pages. Hello, !
JSP java server pages.
JSP Based on
Developing JavaServer Pages
Java Server Pages.
Java Server Pages By: Tejashri Udavant..
Pre-assessment Questions
Introduction to JSP Java Server Pages
Invoking Java Code from JSP
MSIS 655 Advanced Business Applications Programming
Servlets and Java Server Pages
JSP Directives 1-Jan-19.
COP 4610L: Applications in the Enterprise Spring 2005
JSP implicit objects & directive elements
Servlets Servlets are modules that extend the functionality of a “java-enabled” web-server They normally generate HTML code and web content dynamically.
Servlets Servlets are modules that extend the functionality of a “java-enabled” web-server They normally generate HTML code and web content dynamically.
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.
Scripted Page Web Application Development (Java Server Pages)
Java Chapter 7 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Java Server Pages

What a JSP is and how its implementation differs from servlets How to deploy a JSP How to use JavaBeans to hide Java functionality from the JSP How to develop a Web application using JSPs

What is a JSP? A JSP is just another servlet, and like HTTP servlets, a JSP is a server-side Web component that can be used to generate dynamic Web pages. The fundamental difference between Servlets and JSPs is • Servlets generate HTML from Java code. • JSPs embed Java code in static HTML. To illustrate this difference, Example 1 and 2,are the same Web page coded as a servlet and as a JSP, respectively. Each Web page simply reads a parameter called name from the HTTP request and creates an HTML page displaying the value of the name parameter.

Simple Dynamic Page As a Servlet import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class Hello extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType (“text/html”); PrintWriter out = res.getWriter(); String name = req.getParameter(“name”); out.println (“<HTML>”); out.println (“<HEAD><TITLE>Hello</TITLE></HEAD>”); out.println (“<BODY>”); out.println (“<H1>Hello “ + name + “</H1>”); out.println (“</BODY>”); out.println (“</HTML>”); }

Same Dynamic Page As a JSP <HTML> <HEAD><TITLE>Hello</TITLE></HEAD> <BODY> <% String name = request.getParameter(“name”); %> <H1>Hello <%=name%> </H1> </BODY> </HTML> Translation and Execution JSPs differ from servlets in one other respect. Before execution, a JSP must be converted into a Java servlet. This done in two stages: 1. The JSP text is translated into Java code. 2. The Java code is compiled into a servlet.

JSP Syntax and Structure JSP elements are embedded in static HTML. Like HTML, all JSP elements are enclosed in open and close angle brackets (< >). Unlike HTML, but like XML,all JSP elements are case sensitive. JSP elements are distinguished from HTML tags by beginning with either <% or <jsp: <jsp:useBean id=”agency” class=”web.AgencyBean”> </jsp:useBean>

JSP Elements Element Type JSP Syntax Description Directives <%@Directive…%> Information used to control the translation of JSP text into java code Scripting <% %> Embedded Java code Actions <jsp: > JSP-specific tags

Scripting Elements Declarations Scripting elements contain the code logic. It is these elements that get translated into a Java class and compiled. There are three types of scripting elements—declarations,scriptlets, and expressions. They all start with <% and end with %>. Declarations Declarations are used to introduce one or more variable or method declarations, each one separated by semicolons. A variable must be declared before it is used on a JSP page. Declarations are differentiated from other scripting elements with a <%! start tag. An example declaration that defines two variables is as follows: <%! String color = “blue”; int i = 42; %> You can have as many declarations as you need

Expressions JSP expressions are single statements that are evaluated, and the result is cast into a string and placed on the HTML page. An expression is introduced with <%= and must not be terminated with a semi-colon. The following is an expression that will put the contents of the i element in the items array on the output page. <%= items[i] %> JSP expressions can be used as values for attributes in JSP tags. The following example shows how the i element in the items array can be used as the value for a submit button on a form: <INPUT type=submit value=”<%= items[i] %>”>

Scriptlets A scriptlet contains Java code that is executed every time the JSP is invoked.  Scriptlets contain code fragments that are processed when a request is received by the JSP. They need not produce output. Scriptlets can be used to create local variables, for example <% int i = 42;%> <BIG>The answer is <%= i %></BIG>

Mixing Scriptlets and HTML Here is the JSP fragment to do it: <TABLE BORDER=2> <%     for ( int i = 0; i < n; i++ ) {          %> <TR> <TD>Number</TD> <TD><%= i+1 %></TD> </TR> <%     } %> </TABLE>

JSP Comments There are three types of comments in a JSP page <%-- this is a JSP comment --%> <% /* this is a java comment */ %> <!-- this is an HTML comment -->

First JSP example <HTML> <HEAD> <TITLE>JSP Date Example</TITLE> </HEAD> <BODY> <BIG> Today’s date is <%= new java.util.Date() %> </BIG> </BODY> </HTML>

JSP Directives Directives are used to define information about your page to the translator, they do not produce any HTML output. All directives have the following syntax: <%@ directive [ attr=”value” ] %> where directive can be page, include, or taglib.

Include Directive You use the include directive to insert the contents of another file into the JSP. The included file can contain HTML or JSP tags or both. It is a useful mechanism for including the same page directives in all your JSPs or reusing small pieces of HTML to create common look and feel

Full Text of dateBanner.jsp <HTML> <HEAD> <TITLE>JSP Date Example with common banner</TITLE> </HEAD> <BODY> <%@ include file=”banner.html” %> <BIG> Today’s date is <%= new java.util.Date() %> </BIG> </BODY> </HTML>

Full Text of banner.html <TABLE border=”0” width=”600” cellspacing=”0” cellpadding=”0”> <TR> <TD width=”350”><H1>Temporal Information </H1> </TD> <TD align=”right” width=”250”><IMG src=”clock.gif”> </TD> </TR> </TABLE> <BR>

The page Directive The page directive can contain the list of all imported packages Page directives are used to define page-dependent properties You can have more than one page directive in the JSP. A page directive applies to the whole JSP, together with any files incorporated via the include directive

Directive ----import Example ----<%@ page import=” java.math.*” %> Effect----A comma-separated list of package names to be imported for this JSP. The default import list is java.lang.*, javax.servlet.*, javax.servlet.jsp.*, and javax.servlet.http.*.

Directive---- isThreadSafe Example----<%@ page isThreadSafe=”true” %> <%@ page isThreadSafe=”false” %> Effects----If set to true, this indicates that this page can be run multithreaded. This is the default, so you should ensure that access to shared objects

Directive-------errorPage Example------<%@ page errorPage=”/agency/error.jsp” %> Effects-----The client will be redirected to the specified URL when an exception occurs that is not caught by the current page.

Directive----isErrorPage Example---- <%@ page isErrorPage=”true” %> <%@ page isErrorPage=”false” %> Effects----Indicates whether this page is the target URL for an errorPage directive. If true, an implicit scripting variable called “exception” is defined and references the exception thrown in the source JSP. The default is false.

Using HTTP Request Parameters <HTML> <TITLE>Agency Tables</TITLE> <BODY> <FORM action=table> Select a table to display: <SELECT name=table> <OPTION>Applicant <OPTION>ApplicantSkill <OPTION>Customer <OPTION>Job <OPTION>JobSKill </SELECT><P> <INPUT type=submit> </FORM> </BODY> </HTML>

<%@page import=”java.util.*, javax.naming.*, agency.*” %> <%@page errorPage=”errorPage.jsp” %> <% String table=request.getParameter(“table”); %> <HTML> <TITLE>Agency Table: <%= table %></TITLE> <BODY> <H1>Data for table <%= table %> </H1> <TABLE border=1> <% InitialContext ic = null; ic = new InitialContext(); AgencyHome agencyHome =AgencyHome)ic.lookup(“java:comp/env/ejb/Agency”);

Agency agency = agencyHome.create(); Collection rows = agency.select(table); Iterator it = rows.iterator(); while (it.hasNext()) { out.print(“<TR>”); String[] row = (String[])it.next(); for (int i=0; i<row.length; i++) { out.print(“<TD>”+row[i]+”</TD>”); } out.print(“</TR>”); %> </TABLE> </BODY> </HTML>