Introduction.

Slides:



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

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.
 Copyright Wipro Technologies JSP Ver 1.0 Page 1 Talent Transformation Java Server Pages.
Chapter 51 Scripting With JSP Elements JavaServer Pages By Xue Bai.
28/1/2001 Seminar in Databases in the Internet Environment Introduction to J ava S erver P ages technology by Naomi Chen.
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,
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.
Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.
Overview of JSP Technology. The need of JSP With servlets, it is easy to – Read form data – Read HTTP request headers – Set HTTP status codes and response.
1 CIS336 Website design, implementation and management (also Semester 2 of CIS219, CIS221 and IT226) Lecture 9 JavaServer Pages (JSP) (Based on Møller.
Netbeans – jsp.zip Introduction to JSP Netbeans – jsp.zip.
1 JSP – Java Server Pages Representation and Management of Data on the Internet.
JSP Java Server Pages Softsmith Infotech.
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)
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.
Copyright © 2002 ProsoftTraining. All rights reserved. JavaServer Pages.
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.
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.
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.
CSI 3125, Preliminaries, page 1 JSP (Java Server Pages)
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.
INVOKING JAVA CODE WITH JSP SCRIPTING ELEMENTS. Creating Template Text A large percentage of your JSP document consists of static text (usually HTML),
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.
Chapter 14 Using JavaBeans Components in JSP Documents.
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 Page) JSP is server side technology which is used to create dynamic web pages just like Servlet technology. This is mainly used for implementing.
Developing JavaServer Pages
Java Server Pages (JSP)
Java Server Pages By: Tejashri Udavant..
COMP9321 Web Application Engineering Semester 2, 2017
Java Servlets.
Scripted Page Web App Development (Java Server Pages)
Pre-assessment Questions
Introduction to JSP Java Server Pages
Invoking Java Code from JSP
Java Server Pages (JSP)
MSIS 655 Advanced Business Applications Programming
JSP Syntax.
Java Server Pages.
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.
Scripted Page Web Application Development (Java Server Pages)
Web Technologies Java Beans & JSP
Presentation transcript:

Introduction

JavaServer Pages In this lesson you will be learning about: What JSP Technology is and how you can use it. How to define and write JSP Page. Syntax of JSP Page. How do JSP pages work. How is a JSP page invoked and compiled.

JavaServer Pages Technology JavaServer Pages (JSP) technology provides a simplified, fast way to create web pages that display dynamically-generated content. The JSP 1.2specification is an important part of the Java 2 Platform, Enterprise Edition. Using JSP and Enterprise JavaBeans technologies together is a great way to implement distributed enterprise applications with web-based front ends. The first place to check for information on JSP technology is http://java.sun.com/products/jsp/

JSP Page A JSP page is a page created by the web developer that includes JSP technology-specific tags, declarations, and possibly scriptlets, in combination with other static HTML or XML tags. A JSP page has the extension .jsp; this signals to the web server that the JSP engine will process elements on this page. Pages built using JSP technology are typically implemented using a translation phase that is performed once, the first time the page is called. The page is compiled into a Java Servlet class and remains in server memory, so subsequent calls to the page have very fast response times.

Overview JavaServer Pages (JSP) lets you separate the dynamic part of your pages from the static HTML. HTML tags and text <% some JSP code here %> <I> <%= request.getParameter("title") %> </I> You normally give your file a .jsp extension, and typically install it in any place you could place a normal Web page

JSP page translation and processing phases Translation phase Hello.jsp Read Request Client Server helloServlet.java Generate Response Execute Processing phase helloServlet.class

Template Pages translation Server Page Template <html> <title> A simple example </title> <body color=“#FFFFFF”> The time now is <%= new java.util.Date() %> </body> </html> Resulting HTML <html> <title> A simple example </title> <body color=“#FFFFFF”> The time now is Tue Nov 5 16:15:11 PST 2002 </body> </html> translation

Dividing Pure Servlets Public class MySelect { public void doGet(…){ if (isValid(..){ saveRecord(); out.println(“<html>”); …. } private void isValid(…){…} private void saveRecord(…) {…} controller view model Process request Servlet Presentation JSP JavaBeans Business logic Model-View-Controller (MVC) design

Translation Time A JSP application is usually a collection of JSP files, HTML files, graphics and other resources. A JSP page is compiled when your user loads it into a Web browser When the user loads the page for the first time, the files that make up the application are all translated together, without any dynamic data, into one Java source file (a .java file) The .java file is compiled to a .class file. In most implementations, the .java file is a Java servlet that complies with the Java Servlet API.

Simple JSP Page <%@ page info=“A Simple JSP Sample” %> <HTML> <H1> First JSP Page </H1> <BODY> <% out.println(“Welcome to JSP world”); %> </BODY> </HTML>

User Request – JSP File Requested How JSP Works? User Request – JSP File Requested Server File Changed Create Source from JSP Compile Execute Servlet

Copyright @ 2000 Jordan Anastasiade. All rights reserved. JSP Elements Declarations <%! code %> <jsp:declaration> </jsp:declaration > Expressions <%= expression %> <jsp:expression> </jsp:expression> Scriplets <% code %> <jsp:scriplet> </jsp:scriplet > Copyright @ 2000 Jordan Anastasiade. All rights reserved.

HTML Comment Generates a comment that is sent to the client. Syntax <!-- comment [ <%= expression %> ] --> Example: <!-- This page was loaded on <%= (new java.util.Date()).toLocaleString() %> -->

Declaration Declares a variable or method valid in the scripting language used in the JSP page. Syntax <%! declaration; [ declaration; ]+ ... %> Examples <%! String destin; %> <%! Public String getDestination() {return destin;}%> <%! Circle a = new Circle(2.0); %> You can declare any number of variables or methods within one declaration element, as long as you end each declaration with a semicolon. The declaration must be valid in the Java programming language.

Declaration Example <HTML> <HEAD><TITLE>JSP Declarations</TITLE></HEAD> <BODY><H1>JSP Declarations</H1> <%! private int keepCount = 0; %> <H2> Page accessed: <%= ++keepCount %> times </H2> </BODY> </HTML>

Predefined Variable – Implicit Objects request – Object of HttpServletRequest (request parameters, HTTP headers, cookies response – Object of HttpServletResponse out - Object of PrintWriter buffered version JspWriter session - Object of HttpSession associated with the request application - Object of ServletContext shared by all servlets in the engine config - Object of ServletConfig pageContext - Object of PageContext in JSP for a single point of access page – variable synonym for this object

Expression Contains an expression valid in the scripting language used in the JSP page. Syntax <%= expression %> <%! String name = new String(“JSP World”); %> <%! public String getName() { return name; } %> <B><%= getName() %></B> Description: An expression element contains a scripting language expression that is evaluated, converted to a String, and inserted where the expression appears in the JSP file. Because the value of an expression is converted to a String, you can use an expression within a line of text, whether or not it is tagged with HTML, in a JSPfile. Expressions are evaluated from left to right.

Expression Example <HTML> <HEAD> <TITLE>JSP Expressions</TITLE> </HEAD> <BODY> <H2>JSP Expressions</H2> <UL> <LI>Current time: <%= new java.util.Date() %> <LI>Your hostname: <%= request.getRemoteHost() %> <LI>Your session ID: <%= session.getId() %> </UL> </BODY> </HTML>

Scriptlet Contains a code fragment valid in the page scripting language. Syntax <% code fragment %> <% String var1 = request.getParameter("name"); out.println(var1); %> This code will be placed in the generated servlet method: _jspService()

Scriplet Example <HTML> <HEAD><TITLE>Weather</TITLE></HEAD> <BODY> <H2>Today's weather</H2> <% if (Math.random() < 0.5) { %> Today will be a <B>suny</B> day! <% } else { %> Today will be a <B>windy</B> day! <% } %> </BODY> </HTML>

Difference between Scriptlet and Declaration Declaration :- Used for declaring variables and methods. example : <%!  int num =0;  %> During translation and compilation phase of JSP life cycle all variables declared in jsp declaration become instance variables of servlet class and all methods become instance methods. Since instance variables are automatically initialized all variables declared in jsp declaration section gets their default values. Scriptlet:- Used for embedding java code fragments in JSP page. example : <%  num++; %> During translation phase of JSP Life cycle all scriptlet become part of _jspService() method. So we cannot declare methods in scriptlet since we cannot have methods inside other methods. As the variables declared inside scriptlet will get translated to local variables they must be initialized before use.

Example Using JSP Declarations … <body> <h1>JSP Declarations</h1> <%! private int accessCount = 0; %> <h2>Accesses to page since server reboot: <%= ++accessCount %></h2> </body></html> A hit counter in two lines of code. Using fields to store persistent values is exceedingly useful. There are some caveats though: The count starts over when the server reboots. So, use jsplnit and jspDestroy to save/reload the count. Maybe periodically write val to disk in case of Northern California style rolling blackouts. Fields for persistence fails with distributed Web applications on a clustered server. In such a case, either mark the particular Web app non-distributable or use a database or other mechanism for persistence. EJBTM allows much more powerful persistence mechanisms. After 15 total visits by an arbitrary number of different clients INE2720 – Web Application Software Development All copyrights reserved by C.C. Cheung 2003.

JSP Lifecycle jspService() jspInit() jspDestroy() Servlet from JSP Init Event jspService() Request Response jspDestroy() Destroy Event

JSP Page Directive page include Taglib Directives are messages to the JSP container and do not produce output into the current output stream Syntax: <%@ directive attribute=“value” %> <%@ directive attribute1=“value1” attribute1 =“value2” … %> There are three types of directives: page include Taglib XML form: <jsp:directive.directiveType attribute=“value” />

Page Directive Defines attributes that apply to an entire JSP page. [ language="java" ] [ extends="package.class" ] [ import="{package.class | package.*}, ..." ] [ session="true|false" ] [ buffer="none|8kb|sizekb" ] [ autoFlush="true|false" ] [ isThreadSafe="true|false" ] [ info="text" ] [ errorPage="relativeURL" ] [ contentType="mimeType [ ;charset=characterSet ]" [ isErrorPage="true|false" ] %>

Include Directive <%@ include file="relativeURL" %> Includes a static file in a JSP file, parsing the file's JSP elements. Syntax <%@ include file="relativeURL" %> The <%@ include %> directive inserts a file of text or code in a JSP file at translation time, when the JSP file is compiled. <%@ include %> process is static. A static include means that the text of the included file is added to the JSP file. The included file can be: JSP file, HTML file, text file.

Taglib Directive Defines a tag library and prefix for the custom tags used in the JSP page. Syntax <%@ taglib uri="URIToTagLibrary" prefix="tagPrefix" %> <%@ taglib uri="http://thathost/tags" prefix="public" %> <public:loop> </public:loop> The <%@ taglib %> directive declares that the JSP file uses custom tags, names the tag library that defines them, and specifies their tag prefix.

HelloWorld.jsp

JSP Tags + HTML Tags <h2>Table of Square Roots</h2> <table border=2> <tr> <td><b>Number</b></td> <td><b>Square Root</b></td> </tr> <% for (int n=0; n<=100; n++) { %> <td><%=n%></td> <td><%=Math.sqrt(n)%></td> } </table>

A First JSP calculator.html <html> <head></head> <body> <p>Enter two numbers and click the ‘calculate’ button.</p> <form action=“calculator.jsp” method=“get”> <input type=text name=value1><br> <input type=text name=value2 ><br> <input type=submit name=calculate value=calculate> </form> </body> </html> calculator.html

Calculator.jsp <html> <head><title>A simple calculator: results</title></head> <body> <%-- A simpler example 1+1=2 --%> 1+1 = <%= 1+1 %> <%-- A simple calculator --%> <h2>The sum of your two numbers is:</h2> <%= Integer.parseInt(request.getParameter("value1")) + Integer.parseInt(request.getParameter("value2")) %> </body> </html> Calculator.jsp

Server Redirection One can forward to a text file (HTML), a CGI script, a servlet or another JSP page. One can only forward to a new page, provided no output of the original page has been sent to the browser. One may pass as many parameters as one needs with this method by using the param tag. The forward action ends execution of the current JSP page and removes any existing buffered output. The new page has access to application, request, and session objects as the starting file. A new pageContext object is generated for the page. To the browser, it will appear you have the originally requested page, not the page to which you are transferred. Example: <jsp:forward page="home/Default.jsp" >        <jsp:param name="source" value="entry"/> </jsp:forward>

<jsp:forward> Forwards a client request to an HTML file, JSP file, or servlet for processing. Syntax <jsp:forward page="{relativeURL | <%= expression %>}" /> <jsp:forward page="{relativeURL | <%= expression %>}" > <jsp:param name="parameterName" value="{parameterValue | <%= expression %>}" />+ </jsp:forward>

As you see in the below figure, response of second servlet is sent to the client. Response of the first servlet is not displayed to the user

Example : jsp forward index.jsp display.jsp

As you can see in the above figure, response of second servlet is included in the response of the first servlet that is being sent to the client.

Example of RequestDispatcher interface

Example.jsp Process.jsp index1.jsp

Conclusion JavaServer Pages (JSP) lets you separate the dynamic part of your pages from the static HTML. 1. One can simply write the regular HTML in the normal manner, using whatever Web-page-building tools you normally use. One can enclose then the code for the dynamic parts in special tags, most of which start with "<%" and end with "%>"