JSP

  1. Unlike Servlets, Java Server Pages (JSPs) may be placed anywhere as long as they are accessed by a browser that is JSP aware. They are compiled on-the-fly into servlets when accessed.

  2. Run the simple JSP example 20.2 (called BGColor.jsp) from the CWP book.

    It illustrates JSP expressions (<%= expression %> and scriptlets (<% code %>).

    1. Download the example anywhere in your file system. In my case, I
      1. created a CoreWebProgramming directory under %TOMCAT-HOME%/webapps (although it need not be within the TOMCAT-HOME directory);
      2. created a cwp directory under the CoreWebProgramming directory;
      3. downloaded BGColor.jsp to %TOMCAT-HOME%/webapps/CoreWebProgramming/cwp
    2. Update the %TOMCAT-HOME%/conf/server.xml file so that you can access BGColor.jsp as an html file. To do that, I made the following entry in the %TOMCAT-HOME%/conf/server.xml file:
              <Context path="/CoreWebProgramming"
                       docBase="webapps/CoreWebProgramming"
                       crossContext="false"
                       debug="0"
                       reloadable="true" >
              </Context>
    3. Start tomcat.
    4. Use a browser to access the BGColor.jsp page at http://localhost:8080/CoreWebProgramming/cwp/BGColor.jsp
    5. Follow the instructions to change the background color, e.g., access this page with: http://localhost:8080/CoreWebProgramming/cwp/BGColor.jsp?bgColor=orange or http://localhost:8080/CoreWebProgramming/cwp/BGColor.jsp?bgColor=ffaa33.
  3. Look at the (JBuilder) translation of this JSP at http://abbott.calstatela.edu/Courses/CS320b/bgColor/work/localhost_8080/_0002fJsp_00031_0002ejspJsp1_jsp_0.java. Note that this implies that scriptlets are all really part of the body of what amounts to a doGet() method, with the HTML corresponding to out.println() statements.


  4. Introduce a syntax error (both HTML and Java) into the JSP and see what happens.
  5. Modify this JSP page to embed html code within the if statement. Here are the original and the revised if statements.

    Original
    <%
    if (hasExplicitColor) {
      out.println("You supplied an explicit background color of " +
                  bgColor + ".");
    } else {
      out.println("Using default background color of WHITE. " +
                  "Supply ?bgColor=, where <color> is either " +
                  "a standard color or an RRGGBB value, " +
                  "if your browser supports X11 color names.");
    }
    %>
    
    

    Revised
    <%
    if (hasExplicitColor) {%>
      You supplied an explicit background color of <%= bgColor %>.
    <%} else {%>
      Using default background color of WHITE.
        Supply ?bgColor=<color>, where <color> is either
        a standard color or an RRGGBB value,
        if your browser supports X11 color names.
    <%}
    %>
    
    Note that in the revised version, there is a JSP expression (<%= bgColor %>) embedded within the HTML, which is embedded within a scriptlet.

  6. Declarations in JSP Pages. If multiple methods want to share a variable, e.g., an object-level variable, use a JSP Declaration (<%! standard java declaration %>. See page 976 in the book (or example 20.3).

    Note that a number of variables are predefined. (See page 977 in the book.) These include request, response, session, etc.

  7. Page directives. To import a class, use a JSP page directive, for example, (<%@ page import="java.io.*" %>). See example 20.4, p. 980. For an example of an import and a declaration that includes a declaration of a method, see ImportAttribute.jsp.