18
Chapter 11 Invoking Java Code with JSP Scripting Elements

Chapter 11 Invoking Java Code with JSP Scripting Elements

Embed Size (px)

Citation preview

Chapter 11

Invoking Java Code with JSP Scripting Elements

Contents1. Creating Template Text2. Invoking Java Code from JSP 3. Using JSP Expressions4. Example: JSP Expressions5. Writing Scriptlets6. Scriptlet Example7. Using Scriptlets to Make Parts of the JSP

Page Conditional8. Using Declarations9. Declaration Example10. 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

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/_

4. Example: JSP Expressions

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() %>

5. Writing Scriptlets (Cont.)

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.

8. Using Declarations (Cont.)

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

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!