Skip to content

JSP Scripting


Invoking Java Code With JSP Scripting Elements – Chapter 11

Concepts

  • Static vs. dynamic text
  • Dynamic code and good JSP design
  • The importance of packages for JSP helper/utility classes
  • JSP expressions
  • JSP scriptlets
  • JSP declarations
  • Generated servlet code
  • Predefined variables
  • Servlets vs. JSP pages for similar tasks

Creating Template Text

  • The static HTML text in an JSP file is known as template text.
  • This will be the majority of the file.
  • Any tool can be used for this.
  • Usually a web designer will do this.
  • HTML Comments
    • <!— HTML Comment —>
    • These comments are passed to the browser and are visible when the user views the source.
    • Should not be used for any important programming comments!
  • JSP Comments
    • <%-- JSP Comment --%>
    • These comments are NOT passed to the browser. At translation time they are stripped from the code and don’t even appear in the generated servlet file.
    • These should be used almost all of the time when putting comments in JSP files.

JSP Stategies

  • Call Java code directly
  • Call Java code indirectly
  • Use Javabeans
  • Use the MVC architecture
  • Use the JSP expression language
  • Use JSP 2.0 and JSTL
  • Use custom tags

Types of JSP Scripting Elements

  • Expressions

    <%= Java Expression %>
    
  • Scriptlets

    <% Java Code %>
    
  • Declarations

    <%! Field/Method Declaration %>
    
  • Page Directives

    <%@ directive attribute="value" %>
    

Limiting the Amount of Java Code in JSP Pages

  • This is the goal! The less java in JSP the better.
  • Java IDEs and text editors are much better at coding Java then HTML editors.
  • HTML editors like Dreamweaver are much better at coding HTML than java editors.
  • Compiling and testing java in a regular .java file is much better.
  • Debugging java outside of a JSP page is much easier.
  • When we write java classes we can divide the work up and have teams.
  • Regular java classes can be reused!

The Importance of Using Packages

  • Put all java classes in packages. This is really required by both the industry and your teacher!
  • Packaged code is more maintainable.
  • Packaged code is more portable.
  • Prevents naming conflicts.

Using JSP Expressions

  • A JSP expression is used to insert values directly into the output.
  • Example:

    • Current time: <%= new java.util.Date() %>
  • Expressions are frequently used with the predefined variables.

Predefined JSP Variables

  • request, the HttpServletRequest
  • response, the HttpServletResponse
  • session, the HttpSession
  • out, the JSPWriter for outputting to the browser
  • application, the ServletContext.
  • These are created for you by the JSP translator.

JSP Expression Example

Using JSP Scriptlets

  • A JSP scriptlet is used to run arbitrary java code on the JSP page.
  • Example:
    <%
        String greeting = "Well hi there!";
        out.println(greeting);
    %>
    

JSP Scriptlet Example

Using JSP Declarations

  • A JSP declaration lets you define method or fields that get inserted into the main body of the servlet class. This code is placed outside of the service method.
  • Example:
    <%! private String greeting = "Hello again!"; %>
    

JSP Declaration Example

JSP Directives

  • A JSP directive lets you set attributes of the page.
    • Syntax: <%@ directive attributes=... %>
  • There are three types of directives.
    • page
    • taglib
    • include
  • The page directive allows us to specify things that would be above a class definition in a Java class. A common use is adding import statements.

    <%@ page import="java.io.*, java.util.*" %>
    
  • The taglib directive specifies a library of custom tags that this page will have access to. We'll be using this in project 4.

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    
  • The include directive allows to specify a resource to insert into this page. The insertion is done at translation time. This enables things like a highly-performant templating system for HTML page generation.

    <%@ include file="template-example-footer.jsp"%>
    

The jspInit() Method

  • If you declare a method name jspInit() it will be called once when the JSP page is first loaded and before the request is passed to the page.
  • Example:
    <%!
    
        public void jspInit() {
            //initialization code here
        }
    
    %>
    

jspInit() Method Example