I have decided to write up .txt files for this way things will go faster.
I am still following the notes I have produced already, such as:
http://www.cs.indiana.edu/classes/a348/fall2008/whatsnew/1113.html (Homework Four)
http://www.cs.indiana.edu/classes/a348/fall2008/whatsnew/1114.html (Homework Five)
http://www.cs.indiana.edu/classes/a348/fall2008/whatsnew/1116.html (Homework Two)
Today, in these notes you're reading, we're going to take care of Homework Seven.
The steps should be very clear by now. Refer to any of the links above, in order.
We start with the basic template:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class One extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("Hi there, how are you?");
}
}
Place this in the classes folder (inside WEB-INF) in the context of your choice.
Remember that a context needs to also have a lib folder in WEB-INF to function.
The file name is One.java and we need to compile it with:
$JAVA_HOME/bin/javac One.java
Why do you need to compile with this particular command? (Let me know.)
Once the file is compiled you need to create a web.xml file to deploy the servlet.
Here's the file I used:
OneOneOne/servlet/One
Questions: 1. How do you call the servlet?
2. What if you want to call it using a different name?
My server is accessible from
http://silo.cs.indiana.edu:13108/1119/servlet/One
That means I used what you see above and my context's name is 1119.
So now my server works and it simply writes something up. Let's move on.
We don't have to import cgi or anything like that (or we're doing it anyway).
Also the object q is already given: it's request.
So we now need to define and initialize the state, input variables.
String
message = request.getParameter("message"),
questions = request.getParameter("questions"),
key = request.getParameter("key"),
correct = request.getParameter("correct"),
total = request.getParameter("total"),
answer = request.getParameter("answer");
The last one is the only input, the others form the state.
The output is (as always) in an HTML form:
out.println(
"
"
);
Note that all inputs (retrieving state or genuine user input) might be null.
If they're not null they're of type String. Java won't let you forget that.
So we need this at the heart of our program:
if (message != null) {
} else {
message = "Welcome to the game, are you ready?";
}
The message (initialized, above) is (the most representative) part of the state.
It needs to be reported, saved.
message +
"Press to move on." +
"" +
You know where this goes.
Now would be a good time to initialize the entire state:
correct = "0";
total = "0";
questions = "Italy," +
"Australia," +
"United States," +
"South Africa," +
"Spain," +
"China," +
"Russia," +
"Brazil";
key = ""; // there is no key in the beginning
Let's also save it:
"" +
"" +
"" +
"" +
Now let's define two constants:
url = "http://www.cs.indiana.edu/classes/a202-dger/fall2005/notes/flagquiz/images",
names[] = {"Australia", "United States", "Russia", "Spain", "Italy", "South Africa", "Brazil", "China"},
You know where to define them! Here's how they can be useful right away:
questions = "";
for (int i = 0; i < names.length; i++) {
questions += "," + names[i];
}
questions = questions.substring(1);
And while we're at it we can take care of the randomization just as well:
for (int i = 0; i < 100; i++) {
int a = (int)(Math.random() * names.length);
int b = (int)(Math.random() * names.length);
String temp = names[a];
names[a] = names[b];
names[b] = temp;
}
Do be careful where you put this.
Anyway, time has come now to do some grading:
if (key != null) {
if (answer.equals(key)) {
correct = "" + Integer.parseInt(correct) + 1;
} else {
}
total = "" + Integer.parseInt(total) + 1;
} else {
}
What if the user enters something other than a legitimate integer?
Like " 23" or " " or "" or "23ab" or "something else"?
We'll talk about that in class.
What next?
key = questions.substring(0, questions.indexOf(','));
questions = questions.substring(questions.indexOf(',') + 1);
Is equivalent to:
names = questions.split(",")
(key, questions) = (names[0], ",".join(names[1:]))
Explain why.
Now the rest is just too easy.
Finsh the servlet. Remember to reload the context when you re-compile.
Switching to sessions:
a) You need to get a hold of the session, like so:
HttpSession session = request.getSession();
b) You need to retrieve state from, and save it in, the session:
correct = (String) session.getAttribute("correct")
and
session.setAttribute("correct", correct);
Note the politically correct names, also the inevitable casting.
This should take care of both servlets.
Next: convert the servlets to Java Server Pages (JSP).
Very easy, but also a bit annoying, because of all the double quotes we need to remove.
Place the .jsp files in the root of the context.
This is the lab assignment for today: get Homework Seven done, post it, with protected source code.
Next up: Homework Five, hopefully before the lab.
--