310 likes | 439 Views
This lecture focuses on JSP (JavaServer Pages) implicit objects and directive elements crucial for dynamic web content generation. It covers the main implicit objects like request, response, session, and application that can be used in JSP scriptlets and expressions without prior definition. Additionally, it elaborates on JSP directives for configuring page settings, importing classes, and handling session-related attributes, alongside examples demonstrating their practical applications. This session equips developers with fundamental JSP concepts for efficient web application development.
E N D
JSP implicit objects&directive elements Lecture – 20
JSP Journey • directive elements ………………………. • scripting elements • JSP comments………………………………. • declarations ………………………………… • expressions …………………………..…….. • scriptlets……………………………….......... • action elements • special JSP tags ………………………….…. implicit objects
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 …
Implicit Objectscont. • 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
Example of Implicit Objects web.jsp If page = = web index.jsp controller .jsp If page = = java java.jsp
Example Code Use of implicit objects
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>
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>
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>
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>
JSP Journey • directive elements ………………………. • scripting elements • JSP comments………………………………. • declarations ………………………………… • expressions …………………………..…….. • scriptlets……………………………….......... • action elements • special JSP tags ………………………….…. implicit objects
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
JSP Directives cont. • Format • <%@ directive {attribute=“val”}* %> • JSP directives • page: <%@page {attribute=“val”}* %> • include: <%@include {attribute=“val”}* %> • taglib: <%@taglib {attribute=“val”}* %>
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
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”
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” %>
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)
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.
Example Code use of include directive
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>
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>
includeindex.jsp <%@page contentType="text/html"%> <html> <head> <title>JSP Page</title> </head> <body> <%@ include file="header.jsp" %>
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>
XML Syntax for Directives • <jsp:directive.directiveType attribute=“value” /> • For example, the XML equivalent of <%@ page import = “java.util.*” %> Is <jsp:directive.pageimport=“java.util.*” />
JSP Life Cycle Methods jspInit() Request Response _jspService() jspDestroy()