Subsections
- What is a JSP?
- Why use JSPs?
- Directives
- Scriptlets
- Actions
- JSPs are a combination of HTML code and Java code.
- JSP files usually have a filename extension of ".jsp".
- JSP files are compiled into Servlets by the JSP container.
- Provides automatic session management.
- Implicit programming objects are available, including request, response, and others.
- JavaBeans are the component model.
- Compiles a JSP page into a servlet (once, the first time it is called).
- Eexecutes the servlets service() method.
- Sends the resulting text back to the caller.
- Translation time - the time a JSP is compiled into a Servlet.
- Translation time - certain JSP elements are evaluated at translation time.
- Request time - the time a JSP is requested by a user.
- Request time - some JSP elements, such as expressions, are evaluated at request time.
- Use <% ... %> syntax to include Java code directly into the JSP.
- Any valid Java code can be included.
- Use <%= ... %> syntax to include expressions into a JSP.
- An expression is a piece of Java code that evaluates to some thing or some value.
- Example: <%= 2+2 %> evaluates to 4.
- Expressions are evaluated at request time.
- Use <%! ... %> syntax for declarations.
- Use declarations to declare class scope variables and methods.
- Use <%@ ... %> syntax for declarations.
- Use directives to declare information needed by the JSP engine.
- Directives include page, include, and taglib.
- <%@ page contentType="text/html" %>
- Lets the developer specify packages to include.
- Lets the developer specify more advanced page features.
- <%@ taglib uri="uriToTaglib" prefix="someID" %>
- Lets the developer include tag libraries.
- Tag libraries are generally powerful, reusable components for web developers.
- Tag libraries can be created by local developers, are available open source, or can be purchased.
- <%@ include file="filename.jsp" %>
- Lets one JSP include another JSP or HTML document.
- The JSP container reads the included file, and creates on servlet.
- Includes occur at translation time.
- If you change the included file ...
- Works differently than <% jsp:include page="filename.jsp" />
- As a JSP developer, certain implicit objects are made available to you.
- The JSP contaner provides them for you.
- Some objects, such as request and response, are passed as parameters to the servlets service() method.
The table below provides details about each implicit object.
Figure 3.1:
List of implicit JSP objects
|
- If a JSP causes an exception to be thrown, good practices dictate that the exception is handled by code, or that the user be forwarded to an error page.
- Exceptions can be caught in scriptlets.
- Use the <%@ page errorPage="/error.jsp" %> directive to define an error page.
- Identify the error page with the page directive, <%@ page isErrorPage="true" %>.
- The JSP container makes the implicit exception object available to a defined error page.
|
|