Top Banner
JSP implicit objects & directive elements Lecture – 20
30

JSP implicit objects & directive elements

Jan 04, 2016

Download

Documents

ian-reeves

JSP implicit objects & directive elements. Lecture – 20. JSP Journey. directive elements ………………………. scripting elements JSP comments………………………………. declarations ………………………………… expressions …………………………..…….. scriptlets……………………………….......... action elements special JSP tags ………………………….…. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: JSP implicit objects & directive elements

JSP implicit objects&

directive elements

Lecture – 20

Page 2: JSP implicit objects & directive elements

– directive elements ……………………….– scripting elements

• JSP comments……………………………….

• declarations …………………………………

• expressions …………………………..……..

• scriptlets………………………………..........

– action elements• special JSP tags ………………………….….

JSP Journey

implicitobjects

Page 3: JSP implicit objects & directive elements

Implicit Objects

Page 4: JSP implicit objects & directive elements

Implicit Objects

• Objects that can be used in scriplets & expressions without defining them before.– request

– response

– out

– Session

– application (used in the ServletContext)

– config (used in the ServletConfig, represents configuration options e.g init-parameters)

– and more …

Page 5: JSP implicit objects & directive elements

Implicit Objects cont.

– request: javax.servlet.HttpServletRequest

• request object that is the reason for the servlet to run

– response: javax.servlet.HttpServletResponse• Response for the request

– out: javax.servlet.jsp.JspWriter• Output stream writer

Page 6: JSP implicit objects & directive elements

Example of Implicit Objects

index.jspcontroller

.jsp

web.jsp

java.jsp

If page = = web

If page = = java

Page 7: JSP implicit objects & directive elements

Example Code

Use of implicit objects

Page 8: JSP implicit objects & directive elements

index.jsp

<html> <head>

<meta http-equiv="Content-Type" content="text/html">

<title>JSP Page</title>

</head> <body>

<form name="myForm" action="controller.jsp" method="post">

<h3>

<input type="radio" name="page" value="web.jsp" /> Web Site Development

</h3>

<br/><h3>

<input type="radio" name="page" value="java.jsp" /> Beginning Java 2

</h3> <br/>

<input type="submit" value="Submit" />

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

Page 9: JSP implicit objects & directive elements

controller.jsp

<body>

<%

//reading parameters data from index.jsp page

//by using implicit object names

String pageName = request.getParameter("page");

//decide which page is moved by the index.jsp page

if (pageName.equals("web.jsp")){

response.sendRedirect("web.jsp");

}

else if (pageName.equals("java.jsp")){

response.sendRedirect("java.jsp");

}

%> </body>

Page 10: JSP implicit objects & directive elements

Web.jsp

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>JSP Page</title>

</head>

<body>

<% out.println("<h3> Welcome to Web.jsp"); %>

</body>

</html>

Page 11: JSP implicit objects & directive elements

Java.jsp

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>JSP Page</title>

</head>

<body>

<% out.println("<h3> Welcome to Java.jsp"); %>

</body>

</html>

Page 12: JSP implicit objects & directive elements

– directive elements ……………………….– scripting elements

• JSP comments……………………………….

• declarations …………………………………

• expressions …………………………..……..

• scriptlets………………………………..........

– action elements• special JSP tags ………………………….….

JSP Journey

implicitobjects

Page 13: JSP implicit objects & directive elements

JSP Directives

Page 14: JSP implicit objects & directive elements

JSP Directives

• Used to convey special processing information about the page to JSP container

• Enable programmer to:– Specify page settings

– Include content from other resources

– Specify custom-tag libraries

Page 15: JSP implicit objects & directive elements

JSP Directives cont.

• Format– <%@ directive {attribute=“val”}* %>

• JSP directives– page: <%@ page {attribute=“val”}* %> – include: <%@ include {attribute=“val”}* %>– taglib: <%@ taglib {attribute=“val”}* %>

Page 16: JSP implicit objects & directive elements

JSP page Directive

Page 17: JSP implicit objects & directive elements

JSP page Directive

• Give high level information about servlet that will result from JSP page

• Can be used anywhere in the document

• Can control– Which classes are imported– What class the servlet extends– How multithreading is handled– If the servlet participates in session– Which page handles unexpected errors

Page 18: JSP implicit objects & directive elements

JSP page Directive cont.

• Defines attributes of the page– <%@ page {attribute=“val”}* %>

– Some Attributes• language = “java”• extends = “package.class”• import = “package.*,package.class,…”• session = “true | false”

• errorPage = “relativeURL”• isErrorPage = “true | false”

Page 19: JSP implicit objects & directive elements

JSP page Directive cont.

• Some example uses are:

– To import the package like java.util•<%@ page import = “java.util.*” info = “using util package” %>

– To declare this page as an error page• <%@ page isErrorPage = “true” %>

Page 20: JSP implicit objects & directive elements

JSP include Directive

Page 21: JSP implicit objects & directive elements

JSP include Directive

• Lets you include (reuse) navigation bars, tables and other elements in JSP page

• You can include files at– Translation Time (by using include directives)

– Request Time (by using Action elements)

Page 22: JSP implicit objects & directive elements

Including Pages at Translation Timeusing include directive

• Format

<%@include file=“relativeURL”%>

• Purpose

– To include a file in JSP document at the time document is translated into a servlet.

– May contain JSP code that affect the main page such as response header settings etc.

Page 23: JSP implicit objects & directive elements

Example Codeuse of include directive

Page 24: JSP implicit objects & directive elements

header.jsp

<%@page contentType="text/html"%>

<%@page import="java.util.*" %>

<html>

<head>

<title>JSP Page</title>

</head>

<body>

<marquee>

<h3> Enterprise Application Development </h3>

<h3> <%= new Date() %> </h3>

</marquee>

</body>

</html>

Page 25: JSP implicit objects & directive elements

footer.jsp

<%@page contentType="text/html"%>

<%@page import="java.util.*" %>

<html>

<head>

<title>JSP Page</title>

</head>

<body>

<marquee>

<h3> PUCIT </h3>

</marquee>

</body>

</html>

Page 26: JSP implicit objects & directive elements

includeindex.jsp

<%@page contentType="text/html"%>

<html>

<head>

<title>JSP Page</title>

</head>

<body>

<%@ include file="header.jsp" %>

Page 27: JSP implicit objects & directive elements

includeindex.jsp

<table border=1>

<tr> <th> </th> <th> Apples <th> Oranges

<tr> <th> First Quarter <td> 2307 <td> 4706

<tr> <th> Second Quarter <td> 2982 <td> 4005

<tr> <th> Third Quarter <td> 3052 <td> 3798

<tr> <th> Forth Quarter <td> 3055 <td> 5287

</table>

<%@ include file="footer.jsp" %>

</body>

</html>

Page 28: JSP implicit objects & directive elements

XML Syntax for Directives

• <jsp:directive.directiveType attribute=“value” />

• For example, the XML equivalent of

<%@ page import = “java.util.*” %>

Is

<jsp:directive.page import=“java.util.*” />

Page 29: JSP implicit objects & directive elements

JSP Life Cycle Methods

Page 30: JSP implicit objects & directive elements

JSP Life Cycle Methods

jspInit()

_jspService()

jspDestroy()

Request Response