JSP files must have a "jsp" extension, instead of "html." Also, they must be text files. Use a text editor such as NotePad or TextPad.
JSP code must be enclosed within opening and closing tags. The opening tag is <% and the closing tag is %>.
There are two formats for JSP code: blocks or in-line.
Please review the Coding Standards! Also, brush up on your Java.
Code blocks are used for normal coding of declarations and statements. If you have one line of code, the tags can be placed on the same line as the code:
[][]
<% if (x == 5) %>
[
][
]
For more than one line of code, it’s easier to read if the tags are placed on their own lines, and all code is indented:
[][]
<%
if (x == 5) y = 2;
else y = 3;
%>
[
][
]
In-line code is used to insert a value from a variable, constant, literal or expression into the HTML. Note that the opening tag also includes an equal (=) sign.
[][]
<%= x %>
<%= x + 5 %>
<%= 5 %>
[
][
]
All variables are of type variant, there is no data typing.
Type, or copy-paste, the following into a text editor and save it as "HelloWorld.jsp" in the "C:\Program Files\Apache Tomcat 4.0\webapps\cis237" directory.
[][]
<%
//Code Block segment
String message = "Hello, World!";
%>
<%= message%>
<%= message%>
[
][
]
Make sure that Tomcat is running. Open your browser, type "http:/localhost:8080/cis237/HelloWorld.jsp" in the Address/Location bar, and press enter. You should see "Hello, World!" both in the title bar and the main browser window. It will be a little slow the first time, because the Java code has to be compiled. If you close your browser, the reopen it and return to the page, you will notice a faster response time.
Note: It does not matter which browser you use, because the server executes the code. All the browser ever sees is straight HTML.