Lab Notes for Fri Apr 10 /* */ import java.applet.*; import java.awt.*; public class Unicorn extends Applet { int i = 0; public void paint(Graphics g) { g.drawOval(30, 30, 50, 50); i = i + 1; System.out.println("paint has been called " + i + " times."); } } Analogy: A servlet is like an applet. The equivalent of paint is doGet. (paint gets a Graphics argument, doGet gets an HttpServletRequest and an HttpServletResponse. Somebody has defined these types already...) appletviewer is like the servletRunner which is now deep into Tomcat's context manager). import javax.servlet.*; import javax.servlet.http.*; // for HttpServlet import java.io.*; // for PrintWriter public class Unicorn extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println(" How are you? "); } } So this is my servlet. I put it into a context's WEB-INF/classes folder. So I create a chat context in $CATALINA_HOME/webapps. The structure of the context is: WEB-INF/classes /lib I place my Unicorn.java in $CATALINA_HOME/webapps/chat/WEB-INF/classes/Unicorn.java I compile and I get this: -bash-3.2$ javac Unicorn.java Unicorn.java:8: unreported exception java.io.IOException; must be caught or declared to be thrown PrintWriter out = response.getWriter(); ^ 1 error -bash-3.2$ pico Unicorn.java Which means I need to change the servlet to this: import javax.servlet.*; import javax.servlet.http.*; // for HttpServlet import java.io.*; // for PrintWriter public class Unicorn extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println(" How are you? "); } } So I throw the exception that getWriter() might actually raise. I also prepare myself for other types of exceptions, and I have: import javax.servlet.*; // for ServletException import javax.servlet.http.*; // for HttpServlet import java.io.*; // for PrintWriter public class Unicorn extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println(" How are you? "); } } Compile and deploy it, then call it. For this you need to describe to Tomcat in the context where the servlet is how Tomcat should find and load/activate the sevrlet when the call comes from the user. This is done by preparing two entries in the web.xml file of the WEB-INF folder of the context where you have the servlet. For us chat/WEB-INF/web.xml: masonv1.3 Unicorn masonv1.3 /servlet/Mason Once I create and save this file I can access the servlet: http://silo.cs.indiana.edu:47067/chat/servlet/Mason I get an error, I think about it and fix it: -bash-3.2$ clear -bash-3.2$ javac -bash-3.2$ which javac /usr/bin/javac -bash-3.2$ javac -version javac 1.6.0_07 -bash-3.2$ pwd /u/dgerman/apache-tomcat-5.5.17/webapps/chat/WEB-INF -bash-3.2$ cd classes/ -bash-3.2$ ls -l total 16 -rw-r--r-- 1 dgerman faculty 676 Apr 10 11:31 Unicorn.class -rw-r--r-- 1 dgerman faculty 520 Apr 10 11:31 Unicorn.java -bash-3.2$ $JAVA_HOME/bin/javac Unicorn.java -bash-3.2$ Remember you need to reload the context using Tomcat manager. Now I write a new, more complex servlet: import javax.servlet.*; // for ServletException import javax.servlet.http.*; // for HttpServlet import java.io.*; // for PrintWriter public class Unicorn extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String message = request.getParameter("message"), n1 = request.getParameter("n1"), n2 = request.getParameter("n2"), correct = request.getParameter("correct"), total = request.getParameter("total"); if (message == null) { message = "Welcome."; n1 = (int)(Math.random() * 100 - 50) + ""; n2 = (int)(Math.random() * 100 - 50) + ""; correct = "0"; total = "0"; message += " Your current score: " + correct + " / " + total; } else { if (Integer.parseInt(n1) + Integer.parseInt(n2) == Integer.parseInt(request.getParameter("answer"))) { message = "Very good."; correct = (Integer.parseInt(correct) + 1) + ""; } else { message = "No, that's not it."; } total = (Integer.parseInt(total) + 1) + ""; n1 = (int)(Math.random() * 100 - 50) + ""; n2 = (int)(Math.random() * 100 - 50) + ""; message += " Your current score: " + correct + " / " + total; } out.println( "
" + message + "

" + "What is " + n1 + " + " + n2 + "? Answer:

" + "Press to move on. " + "" + "" + "" + "" + "" + "

" ); } } We transform this into $CATALINA_HOME/webapps/chat/unicorn.jsp <% String message = request.getParameter("message"), n1 = request.getParameter("n1"), n2 = request.getParameter("n2"), correct = request.getParameter("correct"), total = request.getParameter("total"); if (message == null) { message = "Welcome."; n1 = (int)(Math.random() * 100 - 50) + ""; n2 = (int)(Math.random() * 100 - 50) + ""; correct = "0"; total = "0"; message += " Your current score: " + correct + " / " + total; } else { if (Integer.parseInt(n1) + Integer.parseInt(n2) == Integer.parseInt(request.getParameter("answer"))) { message = "Very good."; correct = (Integer.parseInt(correct) + 1) + ""; } else { message = "No, that's not it."; } total = (Integer.parseInt(total) + 1) + ""; n1 = (int)(Math.random() * 100 - 50) + ""; n2 = (int)(Math.random() * 100 - 50) + ""; message += " Your current score: " + correct + " / " + total; } %>
<%=message%>

What is <%=n1%> + <%=n2%>? Answer:

Press to move on.

As you can tell, the conversion is immediate. After we call it and it runs we notice that a servlet has been created and compiled automatically for us, implementing our .jsp (see the code we wrote embedded inside): -bash-3.2$ more /u/dgerman/apache-tomcat-5.5.17/work/Catalina/localhost/chat/org/apache/jsp/unicorn_jsp.java package org.apache.jsp; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.jsp.*; public final class unicorn_jsp extends org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent { private static java.util.List _jspx_dependants; public Object getDependants() { return _jspx_dependants; } public void _jspService(HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, ServletException { JspFactory _jspxFactory = null; PageContext pageContext = null; HttpSession session = null; ServletContext application = null; ServletConfig config = null; JspWriter out = null; Object page = this; JspWriter _jspx_out = null; PageContext _jspx_page_context = null; try { _jspxFactory = JspFactory.getDefaultFactory(); response.setContentType("text/html"); pageContext = _jspxFactory.getPageContext(this, request, response, null, true, 8192, true); _jspx_page_context = pageContext; application = pageContext.getServletContext(); config = pageContext.getServletConfig(); session = pageContext.getSession(); out = pageContext.getOut(); _jspx_out = out; String message = request.getParameter("message"), n1 = request.getParameter("n1"), n2 = request.getParameter("n2"), correct = request.getParameter("correct"), total = request.getParameter("total"); if (message == null) { message = "Welcome."; n1 = (int)(Math.random() * 100 - 50) + ""; n2 = (int)(Math.random() * 100 - 50) + ""; correct = "0"; total = "0"; message += " Your current score: " + correct + " / " + total; } else { if (Integer.parseInt(n1) + Integer.parseInt(n2) == Integer.parseInt(request.getParameter("answer"))) { message = "Very good."; correct = (Integer.parseInt(correct) + 1) + ""; } else { message = "No, that's not it."; } total = (Integer.parseInt(total) + 1) + ""; n1 = (int)(Math.random() * 100 - 50) + ""; n2 = (int)(Math.random() * 100 - 50) + ""; message += " Your current score: " + correct + " / " + total; } out.write("\n"); out.write("\n"); out.write("
\n"); out.write(" "); out.print(message); out.write("

\n"); out.write(" What is "); out.print(n1); out.write(' '); out.write('+'); out.write(' '); out.print(n2); out.write("? Answer:

\n"); out.write(" Press to move on. \n"); out.write(" \n"); out.write(" \n"); out.write(" \n"); out.write(" \n"); out.write(" \n"); out.write("

\n"); out.write("\n"); out.write("\n"); out.write("\n"); } catch (Throwable t) { if (!(t instanceof SkipPageException)){ out = _jspx_out; if (out != null && out.getBufferSize() != 0) out.clearBuffer(); if (_jspx_page_context != null) _jspx_page_context.handlePageException(t); } } finally { if (_jspxFactory != null) _jspxFactory.releasePageContext(_jspx_page_context); } } } -bash-3.2$