22
JSP (Java JSP (Java Server Pages) Server Pages) JSP is a technology based on JSP is a technology based on the Java language that enables the Java language that enables the development of dynamic web the development of dynamic web sites. sites. JSP was developed by Sun JSP was developed by Sun Microsystems to allow server Microsystems to allow server side programming. side programming. JSP files are HTML files with JSP files are HTML files with special Tags containing Java special Tags containing Java source code that provide the source code that provide the

JSP (Java Server Pages)

Embed Size (px)

DESCRIPTION

JSP (Java Server Pages). JSP is a technology based on the Java language that enables the development of dynamic web sites. JSP was developed by Sun Microsystems to allow server side programming. - PowerPoint PPT Presentation

Citation preview

Page 1: JSP (Java Server Pages)

JSP (Java Server JSP (Java Server Pages)Pages) JSP is a technology based on the Java JSP is a technology based on the Java

language that enables the language that enables the development of dynamic web sites.development of dynamic web sites.

JSP was developed by Sun JSP was developed by Sun Microsystems to allow server side Microsystems to allow server side programming.programming.

JSP files are HTML files with special JSP files are HTML files with special Tags containing Java source code Tags containing Java source code that provide the dynamic content. that provide the dynamic content.

Page 2: JSP (Java Server Pages)

JSP (Java Server JSP (Java Server Pages)Pages) JSP pages use scriptlets to JSP pages use scriptlets to

encapsulate the java code that encapsulate the java code that generates content for the page.generates content for the page.

JSP page has extension .jspJSP page has extension .jsp

Page 3: JSP (Java Server Pages)

Why use JSP ?Why use JSP ? A separation of presentation and A separation of presentation and

implementation.implementation.

web designers work only on the web designers work only on the presentation.presentation.

Java developers concentrate on Java developers concentrate on implementing the application.implementing the application.

Page 4: JSP (Java Server Pages)

Why use JSP ?Why use JSP ?

JSP can run on various web server JSP can run on various web server including Apache & Netscape.including Apache & Netscape.

JSP pages uses components like JSP pages uses components like enterprise java beans, java beans, enterprise java beans, java beans, which are reusable. This gives JSP which are reusable. This gives JSP reusability capabilities. This will keep reusability capabilities. This will keep pages simple & run faster.pages simple & run faster.

Page 5: JSP (Java Server Pages)

JSP Architecture JSP Architecture Steps required for a JSP request:Steps required for a JSP request:

1. The user goes to a web site 1. The user goes to a web site made using JSP. The user goes to made using JSP. The user goes to a JSP page (ending with .jsp). a JSP page (ending with .jsp). The web browser makes the The web browser makes the request via the Internet. request via the Internet.

2. The JSP request gets sent to the 2. The JSP request gets sent to the Web server.Web server.

Page 6: JSP (Java Server Pages)

JSP ArchitectureJSP Architecture3. The Web server recognises that the 3. The Web server recognises that the

file required is special (.jsp), file required is special (.jsp), therefore passes the JSP file to the therefore passes the JSP file to the JSP Servlet Engine.JSP Servlet Engine.

4. If the JSP file has been called the first 4. If the JSP file has been called the first time,the JSP file is parsed,otherwise time,the JSP file is parsed,otherwise go to step 7. go to step 7.

Page 7: JSP (Java Server Pages)

JSP ArchitectureJSP Architecture5. The next step is to generate a 5. The next step is to generate a

special Servlet from the JSP file. special Servlet from the JSP file.

6. The Servlet source code is compiled 6. The Servlet source code is compiled into a class. into a class.

7. The Servlet is instantiated,calling the 7. The Servlet is instantiated,calling the init and service methods. init and service methods.

Page 8: JSP (Java Server Pages)

JSP ArchitectureJSP Architecture

8. HTML from the Servlet output is sent 8. HTML from the Servlet output is sent via the Internet. via the Internet.

9. HTML results are displayed on the 9. HTML results are displayed on the user's web browser.user's web browser.

Page 9: JSP (Java Server Pages)

                                                                                                                      

Page 10: JSP (Java Server Pages)

JSP tags JSP tags There are five main tags: There are five main tags:

Declaration tag Declaration tag

Expression tag Expression tag

Directive tag Directive tag

Scriplet tag Scriplet tag

Action tag Action tag

Page 11: JSP (Java Server Pages)

Declaration tag ( <Declaration tag ( <%!  %> )%!  %> ) This tag allows the developer to declare This tag allows the developer to declare

variables or methods.variables or methods.

Before the declaration you must have <%!Before the declaration you must have <%!

At the end of the declaration, the At the end of the declaration, the developer must have %>developer must have %>

Code placed in this tag must end with a Code placed in this tag must end with a semicolon ;semicolon ;  

Page 12: JSP (Java Server Pages)

Declaration tag ( <Declaration tag ( <%!  %> )%!  %> )For Example,For Example,

  <%!<%!          int count= 0 ;          int count= 0 ;          String Account ;          String Account ;

%>%>

Page 13: JSP (Java Server Pages)

Expression tag ( <%=   Expression tag ( <%=   %>) %>) This tag allows the developer to This tag allows the developer to

embed any Java expression and is embed any Java expression and is short for out.println(). short for out.println().

    A semicolon ( ; ) does not appear at A semicolon ( ; ) does not appear at

the end of the code inside the tag. the end of the code inside the tag.

Page 14: JSP (Java Server Pages)

Expression tag ( <%=   Expression tag ( <%=   %>)%>) For example, to show the current For example, to show the current

date and time.date and time.

Date : <%= new java.util.Date() %> Date : <%= new java.util.Date() %>

Page 15: JSP (Java Server Pages)

Directive tag ( <%@ Directive tag ( <%@ directive ... %>) directive ... %>) Directives are instructions for JSP engine Directives are instructions for JSP engine

that are processed when JSP page is that are processed when JSP page is compiled into a servlet.compiled into a servlet.

There are three main types of directives:There are three main types of directives:  

1)     page - is used to provide information 1)     page - is used to provide information about about

page.page.2)     Include - is used to include a file in 2)     Include - is used to include a file in jsp page.jsp page.3)     Tag library - A tag lib is a collection 3)     Tag library - A tag lib is a collection of custom tags that can be used by the jsp of custom tags that can be used by the jsp pages. pages.

Page 16: JSP (Java Server Pages)

Directive tag ( <%@ Directive tag ( <%@ directive ... %>) directive ... %>) <%@ page langauge=“java” <%@ page langauge=“java”

import=“java.sql.* ” %>import=“java.sql.* ” %> <%@ include file = “abc.jsp" %><%@ include file = “abc.jsp" %> <%@ taglib <%@ taglib

uri=“http://www.jspcentral.com/tags” uri=“http://www.jspcentral.com/tags” prefix="tld" %> prefix="tld" %>

<tld:loop> <tld:loop>

</tld:loop></tld:loop>

Page 17: JSP (Java Server Pages)

Scriptlet tag ( <% ... Scriptlet tag ( <% ... %> ) %> ) Between <% and %> tags, any valid Java Between <% and %> tags, any valid Java

code is called a Scriptlet. code is called a Scriptlet.

    For example, to print a variable.   For example, to print a variable.   

<% <%                 String username = “John”; String username = “John”;

                  out.println( username ) ; out.println( username ) ;     %> %>

Page 18: JSP (Java Server Pages)

Action TagAction Tag These are specific tags that affect These are specific tags that affect

the runtime behaviour of JSP.the runtime behaviour of JSP.

<jsp:include> - this action allows a <jsp:include> - this action allows a static or dynamic resource to be static or dynamic resource to be included in the JSP at request time.included in the JSP at request time.

<jsp:include page=“header.jsp”/><jsp:include page=“header.jsp”/>

Page 19: JSP (Java Server Pages)

Action TagAction Tag

<jsp:forward> - This action forwards <jsp:forward> - This action forwards a client request to an HTML file, .jsp a client request to an HTML file, .jsp file.file.

<jsp:forward page=“one.jsp”/><jsp:forward page=“one.jsp”/>

Page 20: JSP (Java Server Pages)

First JSP pageFirst JSP page<html><html>

<head><head><title>My first JSP page<title>My first JSP page</title></title></head></head><body><body><h3>Today is :<h3>Today is :

<%= new java.util.Date() %><%= new java.util.Date() %></h3></h3>

</body></body></html></html>

Page 21: JSP (Java Server Pages)

OutputOutput Today is : Tue Sep 09 12:43:33 2008Today is : Tue Sep 09 12:43:33 2008

Page 22: JSP (Java Server Pages)

JSP compared to JSP compared to Servlets Servlets Servlet is a java class, that is both HTML Servlet is a java class, that is both HTML

tags & code exists in a class.tags & code exists in a class.

So when changes are made to code, So when changes are made to code, modification & recompilation of servlet modification & recompilation of servlet source file is required. source file is required.

JSP page doesnot need to be recompiled if JSP page doesnot need to be recompiled if there are some changes made in the there are some changes made in the servlet.servlet.

JSP separates the look from the content.JSP separates the look from the content.