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) |
|
| Java Language Quick Reference (Word doc) |
|
This workshop should take no longer than 60 minutes.
The objective of this JSP is to display the text "Hello World" in increasing font sizes in the browser.
<html> <head> <title>Hello World JSP</title> </head> <body> </body> </html>
<BODY> <% %> </BODY>
for(int i=1; i <= 7; i++)
{
}
{
%>
<p><font size="">Hello World</font></p>
<%
}
http://localhost:8080/mywork/HelloWorld.jsp
i inside the quotes ("") by using a JSP
expression as follows,<font size="<%=i%>">
If you couldn't get your solution to work then click the icon to display the source code.
This part takes a look at the Servlet file that the JSP Container converts the JSP to.
java.util or java.text.
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.
int variable declaration in the for loop.
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.
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.
The next section looks at how to define procedures within a JavaServer Page.