Docsity

JSP Tags and Directives: Understanding JSP Page Structure, Slides of Java Programming

Partial preview of the text

Download JSP Tags and Directives: Understanding JSP Page Structure and more Slides Java Programming in PDF only on Docsity!

Java Server Pages

Lecture#

JSP Tags

Directives These affect the over all structure of the JSPpage that results from translation but produceno output themselves.

Scripting Elements These let us to insert java code into the JSPpage.

Actions These are special tags available to affect theruntime behavior of

the JSP page.

Scripting Elements

Declarations

Scriptlets

Comments

Expression

<%@ Directive %> <%@ page contentType="text/html" %><%@ taglib prefix="c" uri=http://java.sun.com/jstl/core %>

JSP is Easy

JSP is as easy as ...<%-- Calculate the sum of 1 + 2 + 3 dynamically --%>1 + 2 + 3 =

<%-- JSPComment --> <%@ page contentType="text/html" %><%@ taglib prefix="c" uri=http://java.sun.com/jstl/core %>

JSP is Easy

JSP is as easy as ... <%-- Calculate the sum of 1 + 2 + 3 dynamically --%> 1 + 2 + 3 =

Template Text <%@ page contentType="text/html" %><%@ taglib prefix="c" uri=http://java.sun.com/jstl/core %>

**JSP is Easy **

JSP is as easy as ... <%-- Calculate the sum of 1 + 2 + 3 dynamically --%> 1 + 2 + 3 =

** **

<% code %>

Scriptlet: Add a whole block of code toJSP...

passed through to JSP's service method

<%! decl >•

Turned into an instance variable for theservlet.

<%@ page language="java" contentType="text/html" %><%!

int globalCounter = 0; %>

A page with a counter

This page has been visited: <%= ++globalCounter %> times.

<%

int localCounter = 0;

%>This counter never increases its value: <%= ++localCounter %>

<%@ page language="java" contentType="text/html" %><%@ page import="java.util.Date" %> <%!int globalCounter = 0;java.util.Date startDate;public void jspInit( ) {

startDate = new java.util.Date( ); } public void jspDestroy( ) {

ServletContext context = getServletConfig().getServletContext( );

context.log("test.jsp was visited " + globalCounter +" times between " + startDate + " and " + (new Date( ))); } %> A page with a counter

This page has been visited:

<%= ++globalCounter %>

times

since

<%= startDate %>

.

Standard Actions•

JSP Standard action are predefined tags which performsome action based on information that is required at theexact time the JSP page is requested by a browser.

An action can, for instance, access parameters sent withthe request to do a database lookup.

It can also dynamically generate HTML, such as a tablefilled with information retrieved from an external system.

They can be used to include a file at the request time,to find or instantiate a JavaBean, to forward a request toa new page, to generate a browser-specific code, etc.

The JSP standard actions affect the overall runtimebehavior of a JSP page and also the response sent backto the client.

Standard Actions

Makes a JavaBeans component available in apage

Gets a property value from a JavaBeanscomponent and adds it to the response

Set a JavaBeans property value

Includes the response from a servlet or JSPpage during the request processing phase

Implicit Objects in JSP

Implicit Objects in JSP are those objects whichare

created/instantiated

implicitly

by

the

JSP

Engine (part of the underlying Web Container)as

a

convenience

feature

for

the

JSP

page

writers

who

can

use

them

without

having

to

instantiate them.

These implicit objects implement the interfacesdefined in the JSP/Servlet APIs.

There are

nine

implicit objects in JSP.

Implicit Objects in JSP •

request

-^

an

object

of

the

class

implementing

the

HttpServletRequest

interface

which

is

used

for

requesting

a

web

resource

-^

dynamic

or

static.

This

implicit object has a 'request' scope.

response

-^

an object

of the class

implementing the

HttpServletResponse interface which is used to build theresponse to be sent to the client. This implicit object hasa 'page' scope.

pageContext

-^

an

object

of

the

class

which

encapsulates the implementation-dependent features ofthe abstract class PageContext. This object defines thecontext for the JSP page and it provides a single API tomanage various scoped namespaces -