Below is a step-by-step tutorial to a hello world servlet that writes out some HTML to the user who visits it. **Please note that in the following examples, you will need to replace "username" with your actual username** Actually visiting http://students.cs.ndsu.nodak.edu/~username/ will bring you to an example site. Visiting http://students.cs.ndsu.nodak.edu/~username/servlet/HelloWorld will bring you to the servlet that this howto will create. ====== Howto: A Hello World Servlet ====== ===== Making sure the server works ===== ==== Making sure your web page works ==== The first step is to make sure your web page works. Check to make sure you have a working web page by visiting http://students.cs.ndsu.nodak.edu/~username. See instructions [[deptlab:webpage|here]] for help with setting up your web page. ==== Making sure Tomcat works ==== You will also want to make sure that your account is configured to use Tomcat. Take the simple JSP page below and put it in your public_html directory somewhere and browse to it. For example, if I were to place the sample JSP page at /home/helsene/public_html/java.jsp, I could access it as http://students.cs.ndsu.nodak.edu/~helsene/java.jsp Test JSP <%= new java.util.Date().toString() %> So your JSP works now? Great! If this simple test page doesn't work, contact [[robefoer@cs.ndsu.edu|the departmental administrator]] to get your account configured to use Tomcat. If you are enrolled in CSCI 366, your account has already been configured with Tomcat access. ===== Compiling a basic servlet ===== ==== The program ==== This servlet is a short little 'Hello World' We will be creating a java class file that acts as our servlet, but before we compile it, we need to write it first! import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloWorld extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println(""); out.println(""); out.println("Hello World!"); out.println(""); out.println(""); out.println("

Hello World

"); out.println("
"); out.println("This HTML is generated by a servlet, the code for it is located here."); out.println(""); out.println(""); } }
Save this class as HelloWorld.java in any folder of your choosing. ==== Compiling a servlet ==== Compiling is a bit trickier. You will need {{:classes:csci366:servlet-api.zip|the servlet api jar file}} in order to compile. This file is located in /opt/lablibs/servlet-api.jar in the lab, so if you're using the lab to compile, you can just use: javac -cp "./:/opt/lablibs/servlet-api.jar" .java. If you're not in the lab, you'll need to adjust your classpath or import the .jar into Netbeans of Eclipse. Instructions for doing that can be found [[classes:csci366:loading_libraries|here]]. Download servlet-api.jar from the link above and save it somewhere handy. Assuming I saved the servlet-api.jar and the HelloWorld.java files in my home directory (/home/username/) I could compile the HelloWorld.java file using the command: javac HelloWorld.java Now we've got a HelloWorld.class, now what do we do with it? ===== Setting up Tomcat to use our servlet ===== ==== Putting your class in the right folder ==== So now we need to make a folder called //classes//. This folder needs to be in public_html/WEB-INF/. All of your servlet classes go in here, so copy the compiled HelloWorld.class file into public_html/WEB-INF/classes/. At this point you should have the files/folders: /home/username/public_html/ /home/username/public_html/WEB-INF/ /home/username/public_html/WEB-INF/classes/ /home/username/public_html/WEB-INF/classes/HelloWorld.class ==== The web.xml file ==== Tomcat needs to look for servlets in a special WEB-INF folder. You should create this in your public_html folder. The WEB-INF folder needs to contain a web.xml file which tells Tomcat where and how to access the servlet. === Example web.xml === For this howto, we can use this web.xml file: Hello World HelloWorld Hello World /servlet/HelloWorld ^XML tag name^Purpose^ |servlet|This defines the servlet, you can set a name and the classfile to be associated with that name| |servlet-mapping|This maps a URL to a servlet using the servlet's name (that you defined in )| |servlet-name|An arbitrary name to define your servlet (must be consistent throughout the entire web.xml)| |servlet-class|The name of the class file, if your class is called 'HelloWorld.class', you will use 'HelloWorld'| |url-pattern|The path portion of the URL to access your servlet. If you want to access your servlet at 'http://students.cs.ndsu.nodak.edu/~username/servlet/HelloWorld', then this will be '/servlet/HelloWorld'| This file defines a servlet called //Hello World//, tells Tomcat to use the class //HelloWorld// and says that it will be accessed by the URL /servlet/HelloWorld. The full URL in this case will be: http://students.cs.ndsu.nodak.edu/~username/servlet/HelloWorld You can copy this and use this web.xml in your servlet, just change the values to match your servlet. Servlet-name can be any name of your choosing, servlet-class needs to be the package and class name of the class you want to use, and url-pattern defines which URL you want to use to access the servlet. === Where does web.xml go? === This web.xml file needs to be placed at public_html/WEB-INF/web.xml for Tomcat to see it. So make the web.xml file and use the example above The files/folders we should have now look like: /home/username/public_html/ /home/username/public_html/WEB-INF/ /home/username/public_html/WEB-INF/web.xml /home/username/public_html/WEB-INF/classes/ /home/username/public_html/WEB-INF/classes/HelloWorld.class You should check out http://students.cs.ndsu.nodak.edu/~username/servlet/HelloWorld, replacing "username" with your actual username. If this doesn't work, double check the steps above. If everything looks fine and it doesn't work after 5 minutes, then Tomcat did not load your servlet correctly. Send me an [[robefoer@cs.ndsu.edu|email]] and I can investigate.