It’s time to pull our fingers out of the holster and start coding. Lets plunge straight in and try to figure out what happens later:
- Start by right clicking the Quiz project and choose to create a new Servlet.
- Go through the wizard and fill in the following fields (some of them have these values, by default):
- Project: Quiz
- Folder: \Quiz\src
- Java Package: quiz
- Class name: ShowNextQuestion
- Superclass: javax.servlet.http.HttpServlet
- Name: ShowQuestion
- URL Mappings: /ShowQuestion
- Locate the doGet method and add the following code inside of it:
PrintWriter out = response.getWriter();
out.println(“<html><head><title>Question</title></head>”);
out.println(“<body><form action=\”/Quiz/ShowQuestion\”>”);
out.println(“<h1>Question 1</h1><p /><p>”);
out.println(“What is a duck?</p>”);
out.println(“<input type=\”radio\” value=\”1\” name=\”Answer\”>A bird<br>”);
out.println(“<input type=\”radio\” value=\”2\” name=\”Answer\”>A boat<br>”);
out.println(“<input type=\”radio\” value=\”3\” name=\”Answer\”>A tiny, yellow java programmer floating in a tub<br>”);
out.println(“<input type=\”submit\” value=\”Ok\”");
out.println(“</form></body></html>”);
Save the file and run it on the server. (You can right-click on the file and choose run, or just stand in the editor and press the run button)
If you copied the code meticulously (or figured out how copy and paste works), you will see a web page with the question on it. Selecting a radio button and pressing OK will bring you back to the same page again, with the selected answer in the url.
So what happens here? Well, the browser tries to open “http://localhost:8080/Quiz/ShowQuestion”. When we run the servlet, our application is automatically installed on the tomcat server on the path /Quiz/. (In a real environment we would choose the “base” path to our application when we installed it.) So the first part (http://localhost:8080/Quiz/) will bring the server to our application. For the next step, we need to take a peek in our web.xml. (It’s in WebContent/WEB-INF, remember?). There are two entries that together maps the path “/ShowQuestion” to our servlet (quiz.ShowQuestion, remember the package name). First look at the servlet mapping:
<servlet-mapping>
<servlet-name>ShowQuestion</servlet-name>
<url-pattern>/ShowQuestion</url-pattern>
</servlet-mapping>
This entry says “If there is an incoming URL ending on /ShowQuestion, then execute the servlet named ShowQuestion”. Next, we look at the servlet tag:
<servlet>
<description></description>
<display-name>ShowQuestion</display-name>
<servlet-name>ShowQuestion</servlet-name>
<servlet-class>quiz.ShowQuestion</servlet-class>
</servlet>
This one says “There is a servlet named ShowQuestion, and the code is in quiz.ShowQuestion.” The connection between these two entries is the servlet-name tag.
That brings into the servlet code. Depending on which method is used to call the servlet, different methods will be executed. Just opening a page uses GET, resulting in the doGet method being executed. If POST was used, the doPost method would have been called instead. Both these methods have two parameters: an HttpServletRequest and an HttpServletResponse. The Request represents the incoming data, this includes data that is sent to the servlet and which url was used to call it. The Response represents whatever we want to send back to the browser. In this example, we only sent back HTML, but we coule have set different status codes or cookies.
Let’s finish this lesson with using our new knowledge to check if the correct answer was submitted. First, change the line in doGet which prints the form tag to:
out.println(“<form action=\”/Quiz/ShowQuestion\” method=\”POST\”>”);
This will post the information back to the servlet instead of sending it in the url. It also means that the doPost method will be called instead. Remember that the request parameter represents the incoming data? That’s where we go digging for the answer. The field on the html form is called Answer, so add the following code to the doPost method:
String answer = request.getParameter("Answer");
String sarcasticReaction = null;
if (answer.equals("1")) {
sarcasticReaction = "Oh, it's a bird? I never would have guessed";
} else if (answer.equals("2")) {
sarcasticReaction = "A duck-boat, huh? I wished for that when I was 5, too";
} else if (answer.equals("3")) {
sarcasticReaction = "I didn't ask what <i>You</i> were.";
}
PrintWriter out = response.getWriter();
out.println("<html><head><title>Question</title></head>");
out.println("<body>");
out.println("<h1>Answer</h1><p /><p>");
out.println(sarcasticReaction);
out.println("</body></html>");
Note especially the first part where we get the answer from the request and checks it.
Try to run it again. The first time, GET is used, making our doGet to get executed. When the user pressed the Ok button, the browser submitts the answer with POST, triggering our doPost method.
It seems like a whole lot of steps from the moment the user presses ok to the point where the users sees the answer in the browser. But it’s a real Speedy King. Even with more complex applications, these steps are executed in a jiffy.
Next time, I’ll tell you why most of the lines of code written here are wrong and should be removed.
Posted by Albin Theander
Posted by Albin Theander
Posted by Albin Theander