The basic idea is to mix Java code with HTML.
Again, CGI data is sent to a server program. The server program reads the input, does the calculation, and generates output.
To do JSP on your own machine, you'll need to download tomcat. If you do this, you'll set the set the value of the variable JAVA_HOME to be the name of the directory where the JDK is located (on the cislinux machine, it's /home/devtools/jdk1.3.1_09).
When you want to start the server, issue the command startup (on linux, it's startup.sh). You'll find this command in /jakarta-tomcat-4.1.29/jakarta-tomcat-4.1.29/bin on the cislinux machine, and in a corresponding location on your own machine.
To stop the server, issue the command shutdown (on linux, it's shutdown.sh).
I'm using the cislinux machine. The tomcat server has already been started (it will run until the system shuts down). If the server stops running, let me know right away.
Note - you will not do this command, only I will (at least on the cislinux machine).
I'm loading my examples (jsp and html files) into /jakarta-tomcat-4.1.29/jakarta-tomcat-4.1.29/webapps/examples/jsp/avitabij
I can make a directory for you there also (if you want to try this out).
You can move files between your computer and this directory using the WinSCP program (which is a free download available at http://www.sofotex.com/download/software/790.html ). The program is also on lab computers.
When you start the program, enter 65.167.152.98 (or 192.168.100.98 if you are on campus) as the hostname, 22 as the port number, and also enter the user name (your last name) and your password (which I'll give you individually). Then navigate to your directory inside /jakarta-tomcat-4.1.29/jakarta-tomcat-4.1.29/webapps/examples/jsp.
The link to my examples is http://65.167.152.98:8080/examples/jsp/avitabij/
http://192.168.100.98:8080/examples/jsp/avitabij/
For your work, substitute your username.
<html>
<head><title>JSP Example
1</title></head>
<body>
<h1>Who are you?</h1>
<form action="ex1.jsp"
method=POST>
<input type="text" name="name">
<input type="submit" value="Hit Me">
</form>
</body>
</html>
ex1.jsp
<%-- Example 1 --%>
<html>
<head><title>JSP Example 1
Results</title></head>
<body>
<h1>Hello
<%
out.println(" " + request.getParameter("name"));
%>
</h1>
</body>
</html>
<html>
<head><title>JSP Example
2</title></head>
<body>
<h1>Who are you?</h1>
<form action="ex2.jsp"
method=POST>
<input type="text" name="name">
<p>
<select name=color>
<option
value=red> Red
<option value=blue> Blue
<option value=green> Green
</select>
<p>
<input type="submit" value="Hit Me">
</form>
</body>
</html>
ex2.jsp
<%-- Example 2 --%>
<% String name =
request.getParameter("name");
String color =
request.getParameter("color");
%>
<html>
<head><title>JSP Example 2
Results</title></head>
<body bgcolor=
<%= color %>
>
<h1>Hello
<%= name %>
. Your favorite color is
<%= color %>
.
</h1>
</body>
</html>
<html>
<head><title>JSP Example
3</title></head>
<body>
<h1>Who are you?</h1>
<form action="ex3.jsp"
method=POST>
<input type="text" name="name">
<p>
<select name=color>
<option
value=red> Red
<option value=blue> Blue
<option value=green> Green
</select>
<p>
<input type="submit" value="Hit Me">
</form>
</body>
</html>
ex3.jsp
<%-- Example 3 --%>
<html>
<head><title>JSP Example 3
Results</title></head>
<body text=
<%= request.getParameter("color") %>
>
<h1>Hello
<%= request.getParameter("name")
%>
. Your favorite color is
<%=
request.getParameter("color") %>
.
</h1>
</body>
</html>
<html>
<head><title>JSP Example
4</title></head>
<body>
<h1>Who are you?</h1>
<form action="ex4.jsp"
method=POST>
<input type="text" name="name">
<p>
<select name=color>
<option
value=red> Red
<option value=blue> Blue
<option value=green> Green
</select>
<p>
<input type="submit" value="Hit Me">
</form>
</body>
</html>
ex4.jsp
<%-- Example 4 --%>
<%! int redCount=0;
int greenCount=0;
int
blueCount=0;
%>
<% String name =
request.getParameter("name");
String color =
request.getParameter("color");
if
(color.equals("red"))
redCount++;
else if (color.equals("green"))
greenCount++;
else
blueCount++;
%>
<html>
<head><title>JSP Example 4
Results</title></head>
<body text=
<%= color %>
>
<h1>Hello
<%= name %>
. Your favorite color is
<%= color %>
.
<P>
Votes so far:
<P>
<TABLE
BORDER=1>
<TR><TH>Color</TH><TH>Count</TH></TR>
<TR><TD>Red</TD><TD><%= redCount
%></TD></TR>
<TR><TD>Green</TD><TD><%= greenCount
%></TD></TR>
<TR><TD>Blue</TD><TD><%= blueCount
%></TD></TR>
</TABLE>
</h1>
</body>
</html>
<html>
<head><title>JSP Example
5</title></head>
<body>
<h1>Who are you?</h1>
<form action="ex5.jsp"
method=POST>
<input type="text" name="name">
<p>
<select name=color>
<option
value=red> Red
<option value=blue> Blue
<option value=green> Green
</select>
<p>
<input type="submit" value="Hit Me">
</form>
</body>
</html>
ex5.jsp
<%-- Example 5 --%>
<%@page import="java.util.*"
%>
<%! int[] count= new int[3];
String[] colors = {"Red", "Green", "Blue"};
Vector colorVector = new Vector();
Vector nameVector = new Vector();
%>
<% int i;
String name =
request.getParameter("name");
String color =
request.getParameter("color");
if
(color.equals("red"))
count[0]++;
else if (color.equals("green"))
count[1]++;
else
count[2]++;
colorVector.addElement(color);
nameVector.addElement(name);
%>
<html>
<head><title>JSP Example 5
Results</title></head>
<body text=
<%= color %>
>
<h1>Hello
<%= name %>
. Your favorite color is
<%= color %>
.
</h1>
Votes so far:
<P>
<TABLE
BORDER=1>
<TR><TH>Color</TH><TH>Count</TH></TR>
<% for (i = 0; i <= 2; i++)
out.println("<TR><TD>" +
colors[i] + "</TD><TD>" + count[i] +
"</TD></TR>");
%>
</TABLE>
<P>
Record of all votes made (since server
started):
<P>
<table border=1>
<TR><TH>Color</TH><TH>Count</TH></TR>
<% for (i = 0; i < colorVector.size(); i++)
out.println("<TR><TD>" +
(String)colorVector.elementAt(i)
+ "</TD><TD>" + (String)nameVector.elementAt(i)
+ "</TD></TR>");
%>
</table>
</body>
</html>
<html>
<head><title>JSP Example
6</title></head>
<body>
<h1>Who are you?</h1>
<form action="ex6.jsp"
method=POST>
<input type="text" name="name">
<p>
<select name=color>
<option
value=red> Red
<option value=blue> Blue
<option value=green> Green
</select>
<p>
<input type="submit" value="Hit Me">
</form>
</body>
</html>
ex6.jsp
<%-- Example 6 --%>
<%@page
import="java.util.*" %>
<%@page import="java.io.*" %>
<%
int[] count= new int[3];
String[] colors = {"Red", "Green",
"Blue"};
Vector colorVector = new Vector();
Vector
nameVector = new Vector();
try
{
BufferedReader in = new BufferedReader(new
FileReader("/jakarta-tomcat-4.1.29/jakarta-tomcat-4.1.29/webapps/examples/jsp/avitabij/data.txt"));
String s;
while (true)
{
s = in.readLine();
if (s == null) break;
int colon =
s.indexOf(':');
String color =
s.substring(0,colon);
String name =
s.substring(colon+1);
if
(color.equals("red"))
count[0]++;
else if
(color.equals("green"))
count[1]++;
else
count[2]++;
colorVector.addElement(color);
nameVector.addElement(name);
}
in.close();
}
catch (Exception e) // if there is an error, we execute this code.
{
out.println("error reading file");
}
int
i;
String name = request.getParameter("name");
String color =
request.getParameter("color");
if (color.equals("red"))
count[0]++;
else if (color.equals("green"))
count[1]++;
else
count[2]++;
colorVector.addElement(color);
nameVector.addElement(name);
%>
<html>
<head><title>JSP
Example 5 Results</title></head>
<body text=
<%= color
%>
>
<h1>Hello
<%= name %>
. Your favorite color
is
<%= color %>
.
</h1>
Votes so
far:
<P>
<TABLE
BORDER=1>
<TR><TH>Color</TH><TH>Count</TH></TR>
<%
for (i = 0; i <= 2; i++)
out.println("<TR><TD>" + colors[i] + "</TD><TD>" +
count[i] +
"</TD></TR>");
%>
</TABLE>
<P>
Record of
all votes made (since server started):
<P>
<table
border=1>
<TR><TH>Color</TH><TH>Count</TH></TR>
<%
for (i = 0; i < colorVector.size(); i++)
out.println("<TR><TD>" + (String)colorVector.elementAt(i)
+ "</TD><TD>" +
(String)nameVector.elementAt(i)
+
"</TD></TR>");
%>
</table>
<%
try {
PrintWriter file = new PrintWriter(new BufferedWriter(new
FileWriter(
"/jakarta-tomcat-4.1.29/jakarta-tomcat-
4.1.29/webapps/examples/jsp/avitabij/data.txt")));
for (i = 0; i <
colorVector.size(); i++)
file.println((String)colorVector.elementAt(i)
+ ":"
+ (String)nameVector.elementAt(i));
file.close();
}
catch
(Exception ex) {
}
%>
<p>
And here is what the file looks
like:
<P>
<%
try
{
BufferedReader in = new
BufferedReader(new
FileReader("/jakarta-tomcat-4.1.29/jakarta-tomcat-4.1.29/webapps/examples/jsp/avitabij/data.txt"));
String s;
while (true)
{
s = in.readLine();
if (s == null) break;
out.println(s + "\n<BR>");
}
in.close();
}
catch (Exception e) // if there is an error, we
execute this code.
{
out.println("error reading file");
}
%>
</body>
</html>