- Create a new folder named first in this ROOT folder. Then create a new
file named first.jsp in ROOT/first. Place this course code in
first.jsp:
<!DOCTYPE html>
<html>
<head>
<title>First Example</title>
</head>
<body>
<% out.println("username: sjost;");
out.println("Running first program in JSP."); %>
<br />
<br />
<% out.println(new java.util.Date( )); %>
</body>
</html>
- Now open a Command Prompt Window in the folder
c:/apache-tomcat-7.0.68/bin and enter
> startup
Then view the JSP page in a Web browser at localhost:
http://localhost:8080/first/first.jsp
- You should see this text displayed in the browser:
username: ssmith; Running first program in JSP.
Sat Mar 05 13:17:36 CST 2016
- Notice that there are two types of statements in the first.jsp file:
- HTML statements, which are used just like an ordinary HTML file
- Java statements, which are placed in <% ... %>
delimiters, which are called scriptlet delimiters.
- Also notice that System.out.println is not used for printing to a Web page.
The builtin object out is used, which is a
JSPWriter object, used for writing content to the response page.
- The Java statements placed within <% ... %>
delimiters should not have any return value or values. This situation is
similar to the <% ... %> Rails delimiters.
- Actually, JSP has five different types of delimiters:
Delimiter Type | Delimiter Symbols | Example |
Scriptlet | <% ... %> |
<% out.println("This is a test"); %> |
Expression | <%= ... %> |
<%= new Date( ) %> |
Declaration | <%! ... %> |
<%! public int n = 5; %> |
Directive | <%@ ... %> |
<%@page import="java.util.Calendar" %> |
- JSP even has a new format that makes the differences between these types
of delimiters explicit:
Standard Format | New Format |
<% ... %> |
<jsp:scriptlet> ... </jsp:scriptlet> |
<%= ... %> |
<jsp:expression> ... </jsp:expression> |
<%! ... %> |
<jsp:declaration> ... </jsp:declaration> |
- Try adding this directive line to the beginning of the First Example
source code:
<%@page import="java.util.Calendar" %>
- Also add these lines of code immediately before the </body> tag:
<br />
<%
Calendar cal = Calendar.getInstance( );
int m = cal.get(Calendar.MONTH);
int d = cal.get(Calendar.DAY_OF_MONTH);
int y = cal.get(Calendar.YEAR);
%>
<%= String.format("%d/%d/%d", m + 1, d, y) %>