11.2 Invoking Java Code from JSP

There are a number of different ways to generate dynamic content from JSP, as illustrated in Figure 11-1. Each of these approaches has a legitimate place; the size and complexity of the project is the most important factor in deciding which approach is appropriate. However, be aware that people err on the side of placing too much code directly in the page much more often than they err on the opposite end of the spectrum. Although putting small amounts of Java code directly in JSP pages works fine for simple applications, using long and complicated blocks of Java code in JSP pages yields a result that is hard to maintain, hard to debug, hard to reuse, and hard to divide among different members of the development team. See Section 11.3 (Limiting the Amount of Java Code in JSP Pages) for details. Nevertheless, many pages are quite simple, and the first two approaches of Figure 11-1 (placing explicit Java code directly in the page) work quite well. This chapter discusses those approaches.

Figure 11-1. Strategies for invoking dynamic code from JSP.


Types of JSP Scripting Elements

JSP scripting elements let you insert Java code into the servlet that will be generated from the JSP page. There are three forms:

  1. Expressions of the form <%= Java Expression %> , which are evaluated and inserted into the servlet's output.

  2. Scriptlets of the form <% Java Code %> , which are inserted into the servlet's _jspService method (called by service ).

  3. Declarations of the form <%! Field/Method Declaration %> , which are inserted into the body of the servlet class, outside any existing methods .

Each of these scripting elements is described in more detail in the following sections.

Core Servlets and JavaServer Pages (Vol. 1.Core Technologies)
Core Servlets and Javaserver Pages: Core Technologies, Vol. 1 (2nd Edition)
ISBN: 0130092290
EAN: 2147483647
Year: 2002
Pages: 194

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net