1 / 30

JSP implicit objects & directive elements

JSP implicit objects & directive elements. Lecture – 20. JSP Journey. directive elements ………………………. scripting elements JSP comments………………………………. declarations ………………………………… expressions …………………………..…….. scriptlets……………………………….......... action elements special JSP tags ………………………….….

ian-reeves
Télécharger la présentation

JSP implicit objects & directive elements

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. JSP implicit objects&directive elements Lecture – 20

  2. JSP Journey • directive elements ………………………. • scripting elements • JSP comments………………………………. • declarations ………………………………… • expressions …………………………..…….. • scriptlets……………………………….......... • action elements • special JSP tags ………………………….…. implicit objects

  3. Implicit Objects

  4. 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 …

  5. 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

  6. Example of Implicit Objects web.jsp If page = = web index.jsp controller .jsp If page = = java java.jsp

  7. Example Code Use of implicit objects

  8. 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>

  9. 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>

  10. 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>

  11. 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>

  12. JSP Journey • directive elements ………………………. • scripting elements • JSP comments………………………………. • declarations ………………………………… • expressions …………………………..…….. • scriptlets……………………………….......... • action elements • special JSP tags ………………………….…. implicit objects

  13. JSP Directives

  14. 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

  15. JSP Directives cont. • Format • <%@ directive {attribute=“val”}* %> • JSP directives • page: <%@page {attribute=“val”}* %> • include: <%@include {attribute=“val”}* %> • taglib: <%@taglib {attribute=“val”}* %>

  16. JSP page Directive

  17. 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

  18. 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”

  19. 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” %>

  20. JSP include Directive

  21. 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)

  22. 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.

  23. Example Code use of include directive

  24. 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>

  25. 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>

  26. includeindex.jsp <%@page contentType="text/html"%> <html> <head> <title>JSP Page</title> </head> <body> <%@ include file="header.jsp" %>

  27. 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>

  28. 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.*” />

  29. JSP Life Cycle Methods

  30. JSP Life Cycle Methods jspInit() Request Response _jspService() jspDestroy()

More Related