JSP Examples
HelloBilly.htm
<HTML>
<H1>Hello World from Billy!</H1>
</HTML>
HelloBilly.jsp
<HTML>
<H1>Hello World from Billy!</H1>
</HTML>
Expression.jsp
<H1>The time and date now is <%= new java.util.Date() %>.</H1>
Hello7Times.jsp
<HTML>
<HEAD<TITLE>Creating Hello World with Incremental Text Size Increase</TITLE</HEAD>
<BODY>
<% for(int i = 1;i <= 7;i++) { %>
<FONT SIZE=<%=i%>Hello World</FONT<BR>
<% } %>
</BODY>
</HTML>
WSAD Generated Code (in D:\WSADwork\.metadata\.plugins\com.ibm.etools.webtools.validation.jsp.core\Jsp2Java\MyProject\WebContent\Hello7Times_jsp_0.java)
…
JRun Generated Code (in D:\Java\JRun3\servers\default\MyApp\WEB-INF\jsp\ jrun__Hello7Times2ejsp10.java)
out.print("<HTML>\r\n<HEAD<TITLE>Creating Hello World with Incremental Text Size Increase</TITLE</HEAD>\r\n<BODY>\r\n");
for(int i = 1;i <= 7;i++) {
out.print("\r\n <FONT SIZE=");
out.print(i);
out.print(">Hello World</FONT<BR>\r\n");
}
out.print("\r\n</BODY>\r\n</HTML>\r\n\r\n");
…
RegisterToDB.htm
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
</head>
<body>
<H1 ALIGN='CENTER'>Email and URL Registration Page</H1>
<FORM ACTION='/MyApp/RegisterEmailURLToDB.jsp' Method = 'Post'>
SSN: <INPUT TYPE='TEXT' NAME='ssn'<br>
Password: <INPUT TYPE='password' NAME='password'<br>
Email: <INPUT TYPE='TEXT' NAME='emailAddress'<br>
URL: <INPUT TYPE='TEXT' NAME='url'<br<br>
<INPUT TYPE='SUBMIT'</FORM>
</body>
</html>
RegisterEmailURLToDB.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
</head>
<body>
<%@ page import="myclasses.*" %>
<%
String ssn = request.getParameter("ssn");
String password = request.getParameter("password");
String emailAddress = request.getParameter("emailAddress");
String URL = request.getParameter("url");
Student aStud = new Student(ssn, password, emailAddress, URL);
int status = StudentDB.addStudent(aStud);
// ...
%>
<h1>Thanks for registering!</h1>
<p>Here is the information that you entered:</p>
<table cellspacing="5" cellpadding="5" border="1">
<tr>
<td align="right">SSN:</td>
<td<%= ssn %</td>
</tr>
<tr>
<td align="right">Password:</td>
<td<%= password %</td>
</tr>
<tr>
<td align="right">Email Address:</td>
<td<%= emailAddress %</td>
</tr>
<tr>
<td align="right">URL:</td>
<td<%= URL %</td>
</tr>
</table>
</body>
</html>
IncludeExamples.jsp
<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<%-- The following is request-time (i.e., runtime) include --%>
<jsp:include page="/includes/header.htm" flush="true" />
<body>
<h1>This is the body</h1>
<%-- The following is compile-time (i.e., translation time) include --%>
<%-- An include directive for a footer --%>
<%@ include file="/includes/footer.jsp" %>
<%-- An include action for a footer (this file can't use any parameters from the request object) --%>
<%--
<jsp:include page="/includes/footer2.jsp" flush="true" />
--%>
</body>
</html>
header.htm
<head>
<title>Includes Examples</title>
<H1>This is from the header file ... </H1>
</head>
footer.jsp
<%@ page import="java.util.Date" %>
<h1>This is from the footer ...</h1>
<p<i>Your request of <%= request.getParameter("stuff") %> on <%= new Date() %>.</p>
Footer2.jsp
<%@ page import="java.util.Date" %>
<p<i>Date: <%= new Date() %</p>
package myclasses;
import java.sql.*;
import java.util.*;
public class StudentDB{
static String URL = "jdbc:odbc:StudentDB";
static Connection conn;
static Statement stmt;
public static synchronized int addStudent(Student s) throws SQLException{
int rowCount = 0;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // load the driver
conn = DriverManager.getConnection(URL,"","");
stmt = conn.createStatement();
String insertString = "INSERT INTO Student values ('"
+ s.getSsn() + "','"
+ s.getPassword() + "','"
+ s.getEmailAddress() + "','"
+ s.getURL() + "')";
System.out.println("insert string =" + insertString);
rowCount = stmt.executeUpdate(insertString);
} catch (Exception e) {
System.err.println(e.getMessage());
}
return rowCount;
}
public static synchronized int updateStudent(Student s) throws SQLException{
// ...
return 0;
}
public static synchronized int deleteStudent(Student s) throws SQLException{
// ...
return 0;
}
// Other methods here ...
}
package myclasses;
public class Student {
private String ssn;
private String password;
private String emailAddress;
private String URL;
public Student(){}
public Student(String ssn, String password, String emailAddress, String URL){
this.ssn = ssn;
this.password = password;
this.emailAddress = emailAddress;
this.URL = URL;
}
// get methods
public String getSsn(){ return ssn; }
public String getPassword(){ return password; }
public String getEmailAddress(){ return emailAddress; }
public String getURL(){ return URL; }
// sets here ...
}
dukebanner.html
<table border="0" width="400" cellspacing="0"
cellpadding="0">
<tr>
<td height="150" width="150"> </td>
<td width="250"> </td>
</tr>
<tr>
<td width="150"> </td>
<td align="right" width="250">
<img src="duke.waving.gif"> </td>
</tr>
</table>
<br>
hellouser.jsp
<%@ page import="hello.NameHandler" %>
<jsp:useBean id="mybean" scope="page"
class="hello.NameHandler" />
<jsp:setProperty name="mybean" property="*" />
<html>
<head<title>Hello, User</title</head>
<body bgcolor="#ffffff" background="background.gif">
<%@ include file="dukebanner.html" %>
<table border="0" width="700">
<tr>
<td width="150"> </td>
<td width="550">
<h1>My name is Duke. What's yours?</h1>
</td>
</tr>
<tr>
<td width="150" </td>
<td width="550">
<form method="get">
<input type="text" name="username" size="25">
<br>
<input type="submit" value="Submit">
<input type="reset" value="Reset">
</td>
</tr>
</form>
</table>
<%
if ( request.getParameter("username") != null ) {
%>
<%@ include file="response.jsp" %>
<%
}
%>
</body>
</html>
response.jsp
<table border="0" width="700">
<tr>
<td width="150"> </td>
<td width="550">
<h1>Hello, <jsp:getProperty name="mybean"
property="username" />!
</h1>
</td>
</tr>
</table>
Namehandler.java
package hello;
public class NameHandler {
private String username;
public NameHandler() {
username = null;
}
public void setUsername( String name ) {
username = name;
}
public String getUsername() {
return username;
}
}