c:/apache-tomcat-7.0.68/webapps/ROOT
<!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>
> startupThen view the JSP page in a Web browser at localhost:
http://localhost:8080/first/first.jsp
| 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" %> |
| Standard Format | New Format |
|---|---|
| <% ... %> | <jsp:scriptlet> ... </jsp:scriptlet> |
| <%= ... %> | <jsp:expression> ... </jsp:expression> |
| <%! ... %> | <jsp:declaration> ... </jsp:declaration> |
<%@page import="java.util.Calendar" %>
<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) %>
c:/apache-tomcat-7.0.68/webapps/examplesCopy this entire examples folder and its contents into the ROOT folder:
c:/apache-tomcat-7.0.68/webapps/ROOT
http://localhost:8080/index
<!DOCTYPE html>
<html lang="en">
<head>
<title>Insurance Claim Form</title>
<link rel="stylesheet"
href="claimform.css" />
</head>
<body>
<h2>Insurance Claim Form</h2>
<form action="claimserverpage.jsp" method="get">
<table>
<tr>
<td><label for="policynumber">
Policy Number:</label></td>
<td><input type="text" id="policynumber"
name="policynumber" />
</tr>
<tr>
<td><label for="claim">
Amount Claimed:</label></td>
<td><input type="text" id="claim"
name="claim" />
</tr>
<tr>
<td></td>
<td><input type="radio" name="type"
id="buildings"
value="buildings" />
<label for="buildings">
Make a buildings insurance claim.
</label></td>
</tr>
<tr>
<td></td>
<td><input type="radio" name="type"
id="contents" value="contents" />
<label for="contents">
Make a contents insurance claim.
</label></td>
</tr>
<tr>
<td><label for="description"
name="description">
Description of claim:
</label></td>
<td><textarea id="description"
name="description"
rows="5" cols="30">
</textarea></td>
</tr>
<tr>
<td></td>
<td><input type="submit"
value="Submit" /></td>
</tr>
</table>
</form>
</body>
</html>
/* CSS file for claimform.htm */
* { font-family: Helvetica; }
body { color: navy;
background-color: #f2f2ff; }
h2 { color: #600000; }
<?xml version="1.0" ?>
<%@page import="java.text.NumberFormat" %>
<%
String policyNumber =
request.getParameter("policynumber");
String claimValue =
request.getParameter("claim");
String claimType =
request.getParameter("type");
String description =
request.getParameter("description");
// Convert claimValue to a double
double valueOfClaim = 0.0;
try {
valueOfClaim =
Double.parseDouble(claimValue);
}
catch(NumberFormatException e) {
e.printStackTrace( );
System.out.println(claimValue +
" is not a valid number.");
}
%>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Insurance Claim Details</title>
<link rel="stylesheet"
href="claimform.css" />
</head>
<body>
<h2>Insurance Claim Details</h2>
<p>Thankyou for submitting
your insurance claim.</p>
<p>Here are the details
you entered.</p>
<p>
Policy number: <%= policyNumber %><br />
<% NumberFormat currencyFormat =
NumberFormat.getCurrencyInstance( );
String valueOfClaimFormatted =
currencyFormat.format(valueOfClaim); %>
Value of Claim: <%= valueOfClaimFormatted %><br />
Type of insurance policy: <%= claimType %><br />
Description of claim: <%= description %>
</p>
</body>
</html>