1 / 18

Chapter 11

Chapter 11. Invoking Java Code with JSP Scripting Elements. Contents. Creating Template Text Invoking Java Code from JSP Using JSP Expressions Example: JSP Expressions Writing Scriptlets Scriptlet Example Using Scriptlets to Make Parts of the JSP Page Conditional Using Declarations

trevorbrown
Download Presentation

Chapter 11

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. Chapter 11 Invoking Java Code with JSP Scripting Elements

  2. Contents • Creating Template Text • Invoking Java Code from JSP • Using JSP Expressions • Example: JSP Expressions • Writing Scriptlets • Scriptlet Example • Using Scriptlets to Make Parts of the JSP Page Conditional • Using Declarations • Declaration Example • Using Predefined Variables

  3. 1. Creating Template Text • A large percentage of your JSP document consists of static text • HTML looks just like normal HTML • Using tools to build the Web pages • “template text is passed straight through” • You need to put <\% or %\> in the template text • <%-- JSP Comment --%> • <!-- HTML Comment -->

  4. 2. Invoking Java Code from JSP

  5. 2. Invoking Java Code from JSP (Cont.) • Expressions of the form <%= Java Expression %> • Scriptlets of the form <% Java Code %> • Declarations of the form <%! Field/Method Declaration %>

  6. 3. Using JSP Expressions • <%= Java Expression %> • Current time: <%= new java.util.Date() %> • Predefined Variables • request, the HttpServletRequest • response, the HttpServletResponse • session, the HttpSession associated with the request • out, the Writer (a buffered version of type JspWriter) • application, the ServletContext • Tomcat Autogenerated Servlet Source Code: install_dir/work/Standalone/localhost/_

  7. 4. Example: JSP Expressions

  8. 5. Writing Scriptlets • complex than output the value of a simple expression <% Java Code %> • explicitly send output to the resultant page <% String queryData = request.getQueryString(); out.println("Attached GET data: " + queryData); %> <% String queryData = request.getQueryString(); %> Attached GET data: <%= queryData %> Attached GET data: <%= request.getQueryString() %>

  9. 5. Writing Scriptlets (Cont.)

  10. 6. Scriptlet Example • JSP page that uses the bgColor request parameter to set the background color of the page

  11. 7. Using Scriptlets to Make Parts of the JSP Page Conditional

  12. 7. Using Scriptlets to Make Parts of the JSP Page Conditional (Cont.)

  13. 8. Using Declarations • Define methods or fields <%! Field or Method Definition %> Define most methods with separate Java classes, not JSP declarations.

  14. 8. Using Declarations (Cont.)

  15. 9. Declaration Example • prints the number of times the current page has been requested since the server was booted <%! private int accessCount = 0; %> Accesses to page since server reboot: <%= ++accessCount %>

  16. 10. Using Predefined Variables • Variable names the autogeneratedservlet uses • request is the HttpServletRequest associated with the request • response is the HttpServletResponse associated with the response to the client • out is the writer used to send output to the client • Session is the HttpSession object associated with the request • application is the ServletContext as obtained by getServletContext • config is the ServletConfig object for this page

  17. 10. Using Predefined Variables (Cont.) 7. pageContext give a single point of access to many of the page attributes. 8. page is simply a synonym for this and is not very useful

  18. Q & A? • Thank you!

More Related