Download presentation
Presentation is loading. Please wait.
Published byRoderick Boyd Modified over 9 years ago
1
Chapter 11 Invoking Java Code with JSP Scripting Elements
2
Contents 1.Creating Template Text 2.Invoking Java Code from JSP 3.Using JSP Expressions 4.Example: JSP Expressions 5.Writing Scriptlets 6.Scriptlet Example 7.Using Scriptlets to Make Parts of the JSP Page Conditional 8.Using Declarations 9.Declaration Example 10.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 in the template text
4
2. Invoking Java Code from JSP
5
2. Invoking Java Code from JSP (Cont.) Expressions of the form Scriptlets of the form Declarations of the form
6
3. Using JSP Expressions Current time: 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 explicitly send output to the resultant page <% String queryData = request.getQueryString(); out.println("Attached GET data: " + queryData); %> Attached GET data:
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 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 Accesses to page since server reboot:
16
10. Using Predefined Variables Variable names the autogenerated servlet uses 1. request is the HttpServletRequest associated with the request 2. response is the HttpServletResponse associated with the response to the client 3. out is the writer used to send output to the client 4. Session is the HttpSession object associated with the request 5. application is the ServletContext as obtained by getServletContext 6. 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!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.