jsp implicit objects & directive elements
DESCRIPTION
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 PresentationTRANSCRIPT
![Page 1: JSP implicit objects & directive elements](https://reader036.vdocuments.site/reader036/viewer/2022062309/5681384e550346895d9ffa6a/html5/thumbnails/1.jpg)
JSP implicit objects&
directive elements
Lecture – 20
![Page 2: JSP implicit objects & directive elements](https://reader036.vdocuments.site/reader036/viewer/2022062309/5681384e550346895d9ffa6a/html5/thumbnails/2.jpg)
– directive elements ……………………….– scripting elements
• JSP comments……………………………….
• declarations …………………………………
• expressions …………………………..……..
• scriptlets………………………………..........
– action elements• special JSP tags ………………………….….
JSP Journey
implicitobjects
![Page 3: JSP implicit objects & directive elements](https://reader036.vdocuments.site/reader036/viewer/2022062309/5681384e550346895d9ffa6a/html5/thumbnails/3.jpg)
Implicit Objects
![Page 4: JSP implicit objects & directive elements](https://reader036.vdocuments.site/reader036/viewer/2022062309/5681384e550346895d9ffa6a/html5/thumbnails/4.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062309/5681384e550346895d9ffa6a/html5/thumbnails/5.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062309/5681384e550346895d9ffa6a/html5/thumbnails/6.jpg)
Example of Implicit Objects
index.jspcontroller
.jsp
web.jsp
java.jsp
If page = = web
If page = = java
![Page 7: JSP implicit objects & directive elements](https://reader036.vdocuments.site/reader036/viewer/2022062309/5681384e550346895d9ffa6a/html5/thumbnails/7.jpg)
Example Code
Use of implicit objects
![Page 8: JSP implicit objects & directive elements](https://reader036.vdocuments.site/reader036/viewer/2022062309/5681384e550346895d9ffa6a/html5/thumbnails/8.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062309/5681384e550346895d9ffa6a/html5/thumbnails/9.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062309/5681384e550346895d9ffa6a/html5/thumbnails/10.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062309/5681384e550346895d9ffa6a/html5/thumbnails/11.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062309/5681384e550346895d9ffa6a/html5/thumbnails/12.jpg)
– directive elements ……………………….– scripting elements
• JSP comments……………………………….
• declarations …………………………………
• expressions …………………………..……..
• scriptlets………………………………..........
– action elements• special JSP tags ………………………….….
JSP Journey
implicitobjects
![Page 13: JSP implicit objects & directive elements](https://reader036.vdocuments.site/reader036/viewer/2022062309/5681384e550346895d9ffa6a/html5/thumbnails/13.jpg)
JSP Directives
![Page 14: JSP implicit objects & directive elements](https://reader036.vdocuments.site/reader036/viewer/2022062309/5681384e550346895d9ffa6a/html5/thumbnails/14.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062309/5681384e550346895d9ffa6a/html5/thumbnails/15.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062309/5681384e550346895d9ffa6a/html5/thumbnails/16.jpg)
JSP page Directive
![Page 17: JSP implicit objects & directive elements](https://reader036.vdocuments.site/reader036/viewer/2022062309/5681384e550346895d9ffa6a/html5/thumbnails/17.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062309/5681384e550346895d9ffa6a/html5/thumbnails/18.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062309/5681384e550346895d9ffa6a/html5/thumbnails/19.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062309/5681384e550346895d9ffa6a/html5/thumbnails/20.jpg)
JSP include Directive
![Page 21: JSP implicit objects & directive elements](https://reader036.vdocuments.site/reader036/viewer/2022062309/5681384e550346895d9ffa6a/html5/thumbnails/21.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062309/5681384e550346895d9ffa6a/html5/thumbnails/22.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062309/5681384e550346895d9ffa6a/html5/thumbnails/23.jpg)
Example Codeuse of include directive
![Page 24: JSP implicit objects & directive elements](https://reader036.vdocuments.site/reader036/viewer/2022062309/5681384e550346895d9ffa6a/html5/thumbnails/24.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062309/5681384e550346895d9ffa6a/html5/thumbnails/25.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062309/5681384e550346895d9ffa6a/html5/thumbnails/26.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062309/5681384e550346895d9ffa6a/html5/thumbnails/27.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062309/5681384e550346895d9ffa6a/html5/thumbnails/28.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062309/5681384e550346895d9ffa6a/html5/thumbnails/29.jpg)
JSP Life Cycle Methods
![Page 30: JSP implicit objects & directive elements](https://reader036.vdocuments.site/reader036/viewer/2022062309/5681384e550346895d9ffa6a/html5/thumbnails/30.jpg)
JSP Life Cycle Methods
jspInit()
_jspService()
jspDestroy()
Request Response