Homework Seven: Take your Homework Two program and write it in Tomcat.
What that means is that you should provide Java servlets and Java Server
Pages implementations of it. Keep state on the client side also on the
server side.
Claims: If you have a servlet you obtain a JSP from it in 3 minutes. If
you have a JSP or a servlet with client side state you can add sessions
in less than 2 minutes. Because of this you really need just one program
(servlet or JSP) and then you'll be done in less than 10 minutes. Getting
that first program might take between 90 minutes and two days.
We will develop today a servlet. What will it do? Let's take one of the
simplest problems in the document that we used to prepare for the exam.
Let's do The Big Bang but with an eye towards scaling up to the next few
levels and to the extent that we may even complete all those problems.
retrieve state, input (amount)
if state empty
initialize balance and clicks and message
else
balance += amount
clicks += 1
message = ...
store state (balance, clicks, message)
print
What do we need for a servlet?
(a) Tomcat running with access to Tomcat manager.
Let's start by stopping Tomcat. If it's running you stop it by
$CATALINA_HOME/bin/shutdown.sh
Let's be sure we are managers in our own tomcats.
I look through $CATALINA_HOME/conf/tomcat-users.xml and see (artest, br4wl).
(b) Start your Tomcat.
Do it by calling $CATALINA_HOME/bin/startup.sh
(c) Decide on a good name for a context (folder) in $CATALINA_HOME/webapps
For me raupenfahrzeug is a good name. Please create a context.
This means create a folder raupenfahrzeug with the following subfolders:
$CATALINA_HOME/webapps/raupenfahrzeug
$CATALINA_HOME/webapps/raupenfahrzeug/WEB-INF
$CATALINA_HOME/webapps/raupenfahrzeug/WEB-INF/classes
$CATALINA_HOME/webapps/raupenfahrzeug/WEB-INF/lib
$CATALINA_HOME/webapps/raupenfahrzeug/index.html
This last one is the only non-folder write something in it please.
(d) Go in as a manager and verify that the context is accessible.
(e) Go in the classes folder of the new context and put a simple servlet
template in. I will take my servlet from the .pdf first example in ch. 18
so you should please do the same.
I take that template from 18.1 and create a file called Something.java. The
inside of it looks like this:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class Something extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("Howdy!");
}
}
(f) Save and compile this servlet.
-bash-3.2$ javac -version
javac 1.6.0_17
-bash-3.2$ $JAVA_HOME/bin/javac -version
javac 1.5.0_20
-bash-3.2$
Because of this you should compile with $JAVA_HOME/bin/javac
-bash-3.2$ pwd
/u/dgerman/apache-tomcat-5.5.17/webapps/raupenfahrzeug/WEB-INF/classes
-bash-3.2$ ls -l
total 8
-rw-r--r-- 1 dgerman faculty 378 Nov 13 13:28 Something.java
-bash-3.2$ $JAVA_HOME/bin/javac Something.java
-bash-3.2$ ls -l
total 16
-rw-r--r-- 1 dgerman faculty 672 Nov 13 13:39 Something.class
-rw-r--r-- 1 dgerman faculty 378 Nov 13 13:28 Something.java
-bash-3.2$
(g) Deploy the servlet: create a web.xml that describes where it is.
Take the web.xml from p. 144 in ch. 17 and put it in WEB-INF.
Then change it, mine will look like this:
-bash-3.2$ pwd
/u/dgerman/apache-tomcat-5.5.17/webapps/raupenfahrzeug/WEB-INF
-bash-3.2$ ls -l
total 24
drwxr-xr-x 2 dgerman faculty 4096 Nov 13 13:39 classes
drwxr-xr-x 2 dgerman faculty 4096 Nov 13 13:12 lib
-rw-r--r-- 1 dgerman faculty 436 Nov 13 13:47 web.xml
-bash-3.2$ cat web.xml
crazy
Something
crazy
/servlet/beknackt
-bash-3.2$
(h) I can now access the servlet:
http://silo.cs.indiana.edu:8972/raupenfahrzeug/servlet/beknackt
(i) Now start developing the servlet:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class Something extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
String message = request.getParameter("message"),
balance = request.getParameter("balance"),
clicks = request.getParameter("clicks"), // state
amount = request.getParameter("amount"); // input
if (message == null) {
message = "Welcome.";
balance = "0";
clicks = "0";
} else {
}
PrintWriter out = response.getWriter();
out.println(
""
);
}
}
You have to compile again:
-bash-3.2$ /l/jdk1.5/bin/javac Something.java
-bash-3.2$ pwd
/u/dgerman/apache-tomcat-5.5.17/webapps/raupenfahrzeug/WEB-INF/classes
-bash-3.2$
Then you reload the context in manager.
Then you access the servlet and see it in action.
So this is the final servlet:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class Something extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
String message = request.getParameter("message"),
balance = request.getParameter("balance"),
clicks = request.getParameter("clicks"), // state
amount = request.getParameter("amount"); // input
if (message == null) {
message = "Welcome. ";
balance = "0";
clicks = "0";
message += "Balance is " + balance + " and clicks is " + clicks;
} else {
balance = (Integer.parseInt(balance) + Integer.parseInt(amount)) + "";
clicks = Integer.parseInt(clicks) + 1 + "";
message = "Balance is " + balance + " and clicks is " + clicks;
}
PrintWriter out = response.getWriter();
out.println(
""
);
}
}
It has one minor problem which you can fix (a design problem).
But works fine otherwise. So we stop it here.
(j) convert to JSP. While you're at it fix that thing:
Copy the servlet into the context folder (next to index.html) and clean it.
<%
String message = request.getParameter("message"),
balance = request.getParameter("balance"),
clicks = request.getParameter("clicks"), // state
amount = request.getParameter("amount"); // input
if (message == null) {
message = "Welcome. ";
balance = "0";
clicks = "0";
message += "Balance is " + balance + " and clicks is " + clicks;
} else {
balance = (Integer.parseInt(balance) + Integer.parseInt(amount)) + "";
clicks = Integer.parseInt(clicks) + 1 + "";
message = "Balance is " + balance + " and clicks is " + clicks;
}
%>
We fix this to pay attention to which button is pressed.
This way the JSP is a bit different from the servlet.
-bash-3.2$ pwd
/u/dgerman/apache-tomcat-5.5.17/webapps/raupenfahrzeug
-bash-3.2$ ls
index.html something.jsp WEB-INF
-bash-3.2$ cat something.jsp
<%
String message = request.getParameter("message"),
balance = request.getParameter("balance"),
clicks = request.getParameter("clicks"), // state
action = request.getParameter("action"), // input
amount = request.getParameter("amount");
if (message == null) {
message = "Welcome. ";
balance = "0";
clicks = "0";
message += "Balance is " + balance + " and clicks is " + clicks;
} else {
if (action.equals("Up")) {
balance = (Integer.parseInt(balance) + Integer.parseInt(amount)) + "";
} else {
balance = (Integer.parseInt(balance) - Integer.parseInt(amount)) + "";
}
clicks = Integer.parseInt(clicks) + 1 + "";
message = "Balance is " + balance + " and clicks is " + clicks;
}
%>
-bash-3.2$
To run this simply access it.
When we access it the first time a servlet is created (and we don't know):
-bash-3.2$ du -a $CATALINA_HOME/work | grep something
8 /u/dgerman/apache-tomcat-5.5.17/work/Catalina/localhost/raupenfahrzeug/org/apache/jsp/something_jsp.java
8 /u/dgerman/apache-tomcat-5.5.17/work/Catalina/localhost/raupenfahrzeug/org/apache/jsp/something_jsp.class
-bash-3.2$
Look through the .java file and you will see their code, and ours.
Deployment of JSPs does not require web.xml and reloading is automatic.
So that concludes this demonstration/example.
--