Workshop 2. JavaServer Page syntax

Objective

The objective of this workshop is to understand the mechanics of writing JavaServer Pages. It will look at the more basic syntax of a JavaServer Page and also looks at various aspects of the JSP Container.

To help with your work check the reference guides on this site or download the JSP Syntax Poster and the Java Lanuage Quick Reference doc by right clicking the following icons and selecting "Save Target As...".

JSP Poster (PowerPoint format) Download icon
Java Language Quick Reference (Word doc) Download icon

Time Available

This workshop should take no longer than 60 minutes.

Part 1. The Hello World JSP

The objective of this JSP is to display the text "Hello World" in increasing font sizes in the browser.

Demo

  1. Create a new subfolder of the '[Tomcat Installation]/webapps' folder called "mywork".
  2. In this folder create a new JSP named "HelloWorld.jsp" and add the following HTML.
    <html>
    <head>
    <title>Hello World JSP</title>
    </head>
    
    <body>
    
    </body>
    </html>
    
  3. Edit "HelloWorld.jsp" in TextPad or your preferred file editor.
  4. Between the body tags enter a pair of script tags,
    <BODY>
    <%
    
    %>
    </BODY>
    
  5. Within these tags enter the following for loop,
    for(int i=1; i <= 7; i++)
    {
    
    }
    
  6. Within this loop we first want to close the script block, write some HTML and then re open the script block as follows,
    {
    %>
      <p><font size="">Hello World</font></p>
    <%
    }
    
  7. So we now have a loop that writes out some HTML every time it executes. Save what you have done and to test this file enter the following URL to your browser,
    http://localhost:8080/mywork/HelloWorld.jsp
    
  8. Now the objective of this exercise is to display the text in increasing font sizes. You may have already realised that we need to do something with the size attribute of the <font> tag. What we do is to write the value of the variable i inside the quotes ("") by using a JSP expression as follows,
    <font size="<%=i%>">
    
  9. Save and test your solution. If you still have your browser open at this page then you can just refresh the page.

If you couldn't get your solution to work then click the icon to display the source code. Source code icon

Part 2. The JSPs Servlet File

This part takes a look at the Servlet file that the JSP Container converts the JSP to.

  1. First, locate the folder in which your JSP Container writes the JSPs Servlets. Note, some Application Servers will delete the source Servlet files and just keep the compiled class files. Check your App Servers documentation for how to disable this feature.
  2. Locate the file that is the Servlet for "HelloWorld.jsp". This file will probably contain the text "HelloWorld" somewhere in it's name and will end with the extension ".java". Tomcat places these files below the 'work' folder. Open this file.
  3. List the packages that are imported to the servlet class. These packages must be imported and will always be imported by every JSP Containers. The JSP Container may also import another Java packages such as java.util or java.text.
  4. Locate the point in code where the JSP and HTML is processed. By what method is the HTML added to the output stream ?

Part 3. Debugging JSPs

You may have already generated a couple of errors which is fine and this part looks at the various errors that can occur and the techniques used to debug a JSP.

  1. So lets cause an error. Take out the int variable declaration in the for loop.
  2. What error message did your JSP Container display ? JRun 3.0 gave the following error report. First of all it stated.
    500 Internal Server Error
    

    Which is the standard error message for an error that occurred at the web server. After this it listed the file that caused the error and then,

    javax.servlet.ServletException:
    Compilation error occured:
    Found 4 errors in JSP file:
    

    So we had a compilation error as opposed to an execution error, that is, although the JSP Container rewrote the JSP as a Servlet when it tried to compile the Servlet the JDK compiler raised an error. Next there were several lines similar to,

    HelloWorld.jsp:9: Error:
    No entity named "i" was found
    in this environment.
    

    So on line 9 of "HelloWorld.jsp" the entity "i" is not recognized - probably it has not been declared or it is out of scope.

    After this there is the error report given by the compiler which is similar to the above and also gives line number of where in the Servlet there is a problem. This is useful if an error was raised by an external class or JavaBean as in this case you may not be given a line number for the JSP.

    Finally there is what is know as the stack trace which lists the various classes that were executing when this error occurred. Again this can be useful in identifying the method which raised the error particularly with external JavaBeans and class files.

  3. Make some other errors and see how these are reported.
  4. JSP Containers should also have an error log file. Locate and open this file and find the point where it reports the error we caused above. For Tomcat check the folder 'logs'
  5. Another useful technique is to write short text strings to the JSP Containers default out file. Add the following line at some point within "HelloWorld.jsp" and access the file,
    System.out.println("Some useful statement")
    
    This is very useful for debugging some code containing complex logic, particularly if there is some tricky nested if statement structure. For example,
    boolean debug = true;
    
    if(some complex condition)
    {
      System.out.println("Case A");
      ...
    }
    else
    {
      System.out.println("Case B");
      ...
    }
    

    When you are comfortable with writing JSPs you should consider using log4j which allows for some more advanced logging.

Declarations

The next section looks at how to define procedures within a JavaServer Page.

Back Next