180 likes | 186 Views
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
E N D
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 • Declaration Example • Using Predefined Variables
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 -->
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 %>
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/_
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() %>
6. Scriptlet Example • JSP page that uses the bgColor request parameter to set the background color of the page
7. Using Scriptlets to Make Parts of the JSP Page Conditional
7. Using Scriptlets to Make Parts of the JSP Page Conditional (Cont.)
8. Using Declarations • Define methods or fields <%! Field or Method Definition %> Define most methods with separate Java classes, not JSP declarations.
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 %>
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
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
Q & A? • Thank you!