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.

Slides:



Advertisements
Similar presentations
Michelle Johnston, Firebird Services Ltd
Advertisements

PHP I.
Java Script Session1 INTRODUCTION.
Java Server Pages (JSP)
JSP1 Java Server Pages (JSP) Introducing JavaServer Pages TM (JSP TM ) JSP scripting elements.
1 CSC 551: Web Programming Spring 2004 client-side programming with JavaScript  scripts vs. programs  JavaScript vs. JScript vs. VBScript  common tasks.
 Copyright Wipro Technologies JSP Ver 1.0 Page 1 Talent Transformation Java Server Pages.
The Web Warrior Guide to Web Design Technologies
Chapter 51 Scripting With JSP Elements JavaServer Pages By Xue Bai.
Links and Comments.
18-Jun-15 JSP Java Server Pages Reference: Tutorial/Servlet-Tutorial-JSP.html.
JSP Java Server Pages Reference:
Web Page Development Identify elements of a Web Page Start Notepad
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,
Three types of scripting elements: 1.Expressions 2.Scriptlets 3.Declarations Scripting elements.
CMPUT 391 – Database Management Systems Department of Computing Science University of Alberta CMPUT 391 Database Management Systems JavaServer Pages (JSP)
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.
Form Handling, Validation and Functions. Form Handling Forms are a graphical user interfaces (GUIs) that enables the interaction between users and servers.
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.
DAT602 Database Application Development Lecture 15 Java Server Pages Part 1.
M. Taimoor Khan * Java Server Pages (JSP) is a server-side programming technology that enables the creation of dynamic,
Reading Data in Web Pages tMyn1 Reading Data in Web Pages A very common application of PHP is to have an HTML form gather information from a website's.
FALL 2005CSI 4118 – UNIVERSITY OF OTTAWA1 Part 4 Web technologies: HTTP, CGI, PHP,Java applets)
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.
JSP Java Server Pages Softsmith Infotech.
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)
 Embeds Java code  In HTML tags  When used well  Simple way to generate dynamic web-pages  When misused (complex embedded Java)  Terribly messy.
JSP Most of the web developers deploying web applications using servlets mixes the presentation logic and business logic. Separation of business logic.
IST 210: PHP BASICS IST 210: Organization of Data IST210 1.
What is Java Script? An extension to HTML. An extension to HTML. Allows authors to incorporate some functionality in their web pages. (without using CGI.
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.
Introduction to JavaServer Pages. 2 JSP and Servlet Limitations of servlet  It’s inaccessible to non-programmers JSP is a complement to servlet  focuses.
CSC 2720 Building Web Applications Server-side Scripting with PHP.
DAT602 Database Application Development Lecture 16 Java Server Pages Part 2.
Writing Enterprise Applications with J2EE (Fourth lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)
Intro to PHP IST2101. Review: HTML & Tags 2IST210.
JSP Fundamentals Elements of a JSP Using Beans with JSP Integrating Servlets and JSP.
Jsp (Java Server Page) Is a server side program.
Leman Akoglu 11/11/ Fall 2009 Recitation Homework 9 Building A Web Application Phase-II School of Computer Science.
FT228/3 Web Development Error processing. Introduction READ Chapter 9 of Java Server Pages from O’reilly 2 nd Edition Need to be able to 1) Diagnose and.
Introduction to JavaScript CS101 Introduction to Computing.
Overview of Form and Javascript fundamentals. Brief matching exercise 1. This is the software that allows a user to access and view HTML documents 2.
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.
OOSSE Week 8 JSP models Format of lecture: Assignment context JSP models JSPs calling other JSPs i.e. breaking up work Parameter passing JSPs with Add.
JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.
COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 3 1COMP9321, 15s2, Week.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
STRUCTURE OF JSP PRESENTED BY: SIDDHARTHA SINGH ( ) SOMYA SHRIVASTAV ( ) SONAM JINDAL ( )
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.
 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,
Java Programming: Advanced Topics 1 Building Web Applications Chapter 13.
DT228/3 Web Development JSP: Actions elements and JSTL.
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.
1 CSC160 Chapter 1: Introduction to JavaScript Chapter 2: Placing JavaScript in an HTML File.
World Wide Web has been created to share the text document across the world. In static web pages the requesting user has no ability to interact with the.
IST 210: PHP Basics IST 210: Organization of Data IST2101.
111 State Management Beginning ASP.NET in C# and VB Chapter 4 Pages
JSP Based on
Java Server Pages.
Java Servlets.
Scripted Page Web App Development (Java Server Pages)
Intro to PHP & Variables
Invoking Java Code from JSP
Java Server Pages.
Tutorial 10: Programming with javascript
Presentation transcript:

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 HTML, but with embedded JSP tags with snippets of Java code inside them. JSP file compiled and loaded. JSP file compiled and loaded. So takes longer time than normal html file. So takes longer time than normal html file. This compilation only happens once, so after the first load, the file doesn't take long to load anymore. This compilation only happens once, so after the first load, the file doesn't take long to load anymore. But everytime you change the JSP file, it will be re- compiled again. But everytime you change the JSP file, it will be re- compiled again.

Hello! Hello! The time is now The time is now Example Example Example Notice that each time you reload the page in the browser, it comes up with the current time. Notice that each time you reload the page in the browser, it comes up with the current time. The character sequences enclose Java expressions, which are evaluated at run time. The character sequences enclose Java expressions, which are evaluated at run time. This is what makes it possible to use JSP to generate dyamic HTML pages that change in response to user actions or vary from user to user. This is what makes it possible to use JSP to generate dyamic HTML pages that change in response to user actions or vary from user to user.

JSP also allows you to write blocks of Java code inside the JSP. JSP also allows you to write blocks of Java code inside the JSP. You do this by placing your Java code between characters (just like expressions, but without the = sign at the start of the sequence.) You do this by placing your Java code between characters (just like expressions, but without the = sign at the start of the sequence.) This block of code is known as a "scriptlet". This block of code is known as a "scriptlet". By itself, a scriptlet doesn't contribute any HTML (though it can, as we will see down below.) By itself, a scriptlet doesn't contribute any HTML (though it can, as we will see down below.) A scriptlet contains Java code that is executed every time the JSP is invoked. A scriptlet contains Java code that is executed every time the JSP is invoked. Here is a modified version of our JSP from previous section, adding in a scriptlet. Here is a modified version of our JSP from previous section, adding in a scriptlet.

<BODY> <% <% // This is a scriptlet. Notice that the "date” //variable we declare here is available // in the embedded expression later on. // in the embedded expression later on. java.util.Date date = new java.util.Date(); %> Hello! The time is now Hello! The time is now </BODY> Example

By itself a scriptlet does not generate HTML. By itself a scriptlet does not generate HTML.

A "request" in server-side processing refers to the transaction between a browser and the server. A "request" in server-side processing refers to the transaction between a browser and the server. When someone clicks or enters a URL, the browser sends a "request" to the server for that URL, and shows the data returned. When someone clicks or enters a URL, the browser sends a "request" to the server for that URL, and shows the data returned. As a part of this "request", various data is available, including the file the browser wants from the server, and if the request is coming from pressing a SUBMIT button, the information the user has entered in the form fields. As a part of this "request", various data is available, including the file the browser wants from the server, and if the request is coming from pressing a SUBMIT button, the information the user has entered in the form fields. The JSP "request" variable is used to obtain information from the request as sent by the browser. The JSP "request" variable is used to obtain information from the request as sent by the browser. For instance, you can find out the name of the client's host (if available, otherwise the IP address will be returned.) then request.getRemoteHost() can be used. For instance, you can find out the name of the client's host (if available, otherwise the IP address will be returned.) then request.getRemoteHost() can be used.

A similar variable is "response". A similar variable is "response". This can be used to affect the response being sent to the browser. This can be used to affect the response being sent to the browser. For instance, you can call response.sendRedirect( anotherUrl ); For instance, you can call response.sendRedirect( anotherUrl ); to send a response to the browser that it should load a different URL. to send a response to the browser that it should load a different URL. This response will actualy go all the way to the browser. This response will actualy go all the way to the browser. The browser will then send a different request, to "anotherUrl". The browser will then send a different request, to "anotherUrl". This is a little different from some other JSP mechanisms we will come across, for including another page or forwarding the browser to another page. This is a little different from some other JSP mechanisms we will come across, for including another page or forwarding the browser to another page.

Mixing Scriptlets and HTML It is simpler to mix scriptlets and HTML. It is simpler to mix scriptlets and HTML. Suppose you have to generate a table in HTML. Suppose you have to generate a table in HTML. We will generate a table containing the numbers from 1 to 10. We will generate a table containing the numbers from 1 to 10. Example Number Number </TR> </TABLE>

Another example of mixing scriptlets and HTML is shown below Another example of mixing scriptlets and HTML is shown below here it is assumed that there is a boolean variable named "hello" available. here it is assumed that there is a boolean variable named "hello" available. If you set it to true, you will see one output, if you set it to false, you will see another output. If you set it to true, you will see one output, if you set it to false, you will see another output. Hello, world Hello, world Goodbye, world Goodbye, world

JSP Directives Why we don't just import java.util.*; It is possible to use "import" statements in JSPs, but the syntax is a little different from normal Java. Why we don't just import java.util.*; It is possible to use "import" statements in JSPs, but the syntax is a little different from normal Java. Try the following Example: Try the following Example:Example <% Date date = new Date(); %> Hello! The time is now The first line in the above example is called a "directive". The first line in the above example is called a "directive".

A JSP "directive" starts with characters. A JSP "directive" starts with characters. This one is a "page directive". This one is a "page directive". The page directive can contain the list of all imported packages. The page directive can contain the list of all imported packages. To import more than one item, separate the package names by commas, e.g. To import more than one item, separate the package names by commas, e.g. Besides the page directives, the other most useful directives are include and taglib. Besides the page directives, the other most useful directives are include and taglib. We will be covering taglib separately. We will be covering taglib separately. The include directive is used to physically include the contents of another file. The include directive is used to physically include the contents of another file. The included file can be HTML or JSP or anything else -- the result is as if the original JSP file actually contained the included text. The included file can be HTML or JSP or anything else -- the result is as if the original JSP file actually contained the included text.

Going to include hello.jsp... Going to include hello.jsp...<BR> Example Example Example

JSP Tags Another important syntax element of JSP are tags. Another important syntax element of JSP are tags. JSP tags do not use <%, but just the < character. JSP tags do not use <%, but just the < character. A JSP tag is somewhat like an HTML tag. A JSP tag is somewhat like an HTML tag. JSP tags can have a "start tag", a "tag body" and an "end tag". JSP tags can have a "start tag", a "tag body" and an "end tag". The start and end tag both use the tag name, enclosed in characters. The start and end tag both use the tag name, enclosed in characters. The end starts with a / character after the < character. The end starts with a / character after the < character. The tag names have an embedded colon character : in them, the part before the colon describes the type of the tag. The tag names have an embedded colon character : in them, the part before the colon describes the type of the tag.

For instance: For instance: body body If the tag does not require a body, the start and end can be conveniently merged together If the tag does not require a body, the start and end can be conveniently merged together Tags can be of two types: loaded from an external tag library, or predefined tags. Tags can be of two types: loaded from an external tag library, or predefined tags. Predefined tags start with jsp: characters. Predefined tags start with jsp: characters. For instance, jsp:include is a predefined tag that is used to include other pages. For instance, jsp:include is a predefined tag that is used to include other pages. We have already seen the include directive. jsp:include is similar. We have already seen the include directive. jsp:include is similar. But instead of loading the text of the included file in the original file, it actually calls the included target at run-time (the way a browser would call the included target. But instead of loading the text of the included file in the original file, it actually calls the included target at run-time (the way a browser would call the included target.

Going to include hello.jsp... Going to include hello.jsp... Change the "jsp:include" to "jsp:forward" and see what is the difference. Change the "jsp:include" to "jsp:forward" and see what is the difference. These two predefined tags are frequently very useful. These two predefined tags are frequently very useful. Example Example Example Example Example Example

JSP Sessions On a typical web site, a visitor might visit several pages and perform several interactions. On a typical web site, a visitor might visit several pages and perform several interactions. If you are programming the site, it is very helpful to be able to associate some data with each visitor. If you are programming the site, it is very helpful to be able to associate some data with each visitor. For this purpose, "session"s can be used in JSP. For this purpose, "session"s can be used in JSP. A session is an object associated with a visitor. A session is an object associated with a visitor. Data can be put in the session and retrieved from it. Data can be put in the session and retrieved from it. A different set of data is kept for each visitor to the site. A different set of data is kept for each visitor to the site. Here is a set of pages that put a user's name in the session, and display it elsewhere. Here is a set of pages that put a user's name in the session, and display it elsewhere.

First we have a form, let us call it GetName.html First we have a form, let us call it GetName.htmlGetName.html What's your name? <P> </FORM> The target of the form is "SaveName.jsp", which saves the user's name in the session. The target of the form is "SaveName.jsp", which saves the user's name in the session. SaveName.jsp Note the variable "session". Note the variable "session". This is another variable that is normally made available in JSPs. This is another variable that is normally made available in JSPs. In directive, you can indicate that you do not need sessions, in which case the "session" variable will not be made available. In directive, you can indicate that you do not need sessions, in which case the "session" variable will not be made available.

<% <% String name = request.getParameter( "username"); session.setAttribute( "theName", name ); %> Continue Continue The SaveName.jsp saves the user's name in the session, and puts a link to another page, NextPage.jsp. The SaveName.jsp saves the user's name in the session, and puts a link to another page, NextPage.jsp. NextPage.jsp shows how to retrieve the saved name. NextPage.jsp shows how to retrieve the saved name. NextPage.jsp Hello, Hello, If you bring up two different browsers (not different windows of the same browser), or run two browsers from two different machines, you can put one name in one browser and another name in another browser, and both names will be kept track of. If you bring up two different browsers (not different windows of the same browser), or run two browsers from two different machines, you can put one name in one browser and another name in another browser, and both names will be kept track of. The session is kept around until a timeout period. The session is kept around until a timeout period. Then it is assumed the user is no longer visiting the site, and the session is discarded. Then it is assumed the user is no longer visiting the site, and the session is discarded.

JSP Errors JSP Translation JSP Translation If you mistype the jsp tags or fail to use correct attribute for the tag, you’ll get this error. If you mistype the jsp tags or fail to use correct attribute for the tag, you’ll get this error. Servlet Compilation Servlet Compilation When java code is mistyped or page directives are omitted. When java code is mistyped or page directives are omitted. Information is written on a log file and return an HTTP 500 error to the client. J2EE also shows compilation error on the page including the line number of error. Information is written on a log file and return an HTTP 500 error to the client. J2EE also shows compilation error on the page including the line number of error.

Servlet runtime exceptions Servlet runtime exceptions Any exception generated by JSP that are not explicilty caught by try/catch block will be reported back to the client as an HTTP 500 error. Any exception generated by JSP that are not explicilty caught by try/catch block will be reported back to the client as an HTTP 500 error. HTML Presenation HTML Presenation Incorrect definition of HTML element. Incorrect definition of HTML element. The first three are detected by JSP container and reported in an implementation-specific way. The first three are detected by JSP container and reported in an implementation-specific way. The J2EE reports the error back to the client using HTML The J2EE reports the error back to the client using HTML The Last error is detected by browser. The Last error is detected by browser.