Advanced Internet Programming (Java EE) - Servlets - Lab exercise - Counter Servlet 31242/32549/ Java EEAdvanced Internet Programming INFO: What's New FAQ Software Machines Oracle Assignments MODULES: 00 Admin 01 Intro 02 Architecture 03 Servlets 04 JSP 05 JDBC 06 RMI 06 JNDI 07 EJB 08 XML 08 Web Services 09 Security 10 Transactions 11 Legacy 12 Review 99 Design LINKS: START page Faculty of IT UTS Module: Servlets Lab exercise - Counter Servlet This laboratory exercise involves modifying your Hello World servlet to make it keep count of the number of times it has been invoked. Level of Difficulty: 2 (moderately easy) Estimated time: 15 minutes Pre-requisites: Completed the Hello World servlet lab exercise A Counter servlet The goal of this exercise is to create a servlet that maintains a counter of the number of times it has been invoked since it was last reloaded by the web server. It will use a class variable (i.e. static) to keep count of the number of invocations. Each time the servlet is invoked, it will print out the value of the counter. There are 2 main ways to do this lab. Method 1 is to create a new Servlet (as per the Hello World Servlet lab) and name this CounterServlet, with the URL mapping of /Counter. You then add the required code to perform the desired task. This is fairly straight forward. Method 2 is to copy the existing Servlet and to add it into the deployment descriptor. This is a touch more complicated, so we will do this in this lab to show you how to copy an existing servlet class and insert it into the web project. Copying an existing Servlet Begin by copying your Hello World servlet code, and putting it into a file called CounterServlet.java. You may keep it in the same project (WAR file) as before, or put it in a separate WAR file (if you would like more practice at the process of creating WAR files). Make the following modifications to your Hello World servlet source code: You can cut and paste the HelloWorldServlet from the src folder. Rename the class to CounterServlet Declare a class variable to hold the value of your counter.
private static int numRequests = 0;
Declare a synchronized method to update the value of the numRequests variable.
private synchronized void incNumRequests() {
numRequests++;
}
Modify the code inside your existing doGet() method to make it call the incNumRequests method you have just created, and print out the current value of the counter.
incNumRequests();
out.println("Number of requests since reload: "
+ numRequests);
Servlets and threading The question you should be asking yourself right now is: why do I need to create an extra method just to increment the value of numRequests? Why can't I just have my servlet increment the value directly? The answer lies in the way servlets handle threading. By default, every servlet you create is multi-threaded. That means that at any given instant in time, multiple instances of your doGet() method could be executing simultaneously. Also recall the meaning of a class variable (i.e. declared as static) in Java. A static variable is one that is shared between all instances of a given class. So if you have 10 instances of your CounterServlet executing, all 10 instances will be sharing a single copy of the numRequests variable. What happens if two different instances of your servlet running at the same time both try and update the value of numRequests at the same time? In theory, this could lead to a semantic error in the program because two threads are trying to update the same shared resource simultaneously. [Although strictly speaking in this case there is not much danger because all we are doing is incrementing a single value.] The general principle that you should follow is that if you ever write a Java servlet where multiple threads will be accessing a single, non-shareable resource, you must programmatically ensure that at most one thread can be accessing the resource at any one time. Java provides this facility through the use of its synchronized keyword. If you have studied operating systems, note that this is similar in concept to monitors and semaphores used to ensure at most one process is executing a critical section at any given time. Running your servlet Follow the same steps you used when creating your Hello World servlet to edit and compile the code. Eclipse automatically re-deploys the updated lab so you can now unit-test the application. Note that each time you reload the browser with the refresh button, the displayed counter should update. If not, you can choose Run As -> Run on Server You can make the deployment automatically updated by selecting the "Automatic Publish" option when you defined the Weblogic server instance. You can do this by double clicking the Weblogic server in the Server tab at the bottom of the Eclipse workspace. This produces a panel of server details such as general information, startup & deployment options and in the top right hand corner, publishing options. You can now optionally redeploy your WAR file by using the Export .. as WAR file menu and by copying the war file to your WebLogic autodeploy directory. cp HelloWorldApp.war ~/weblogic/autodeploy
Test your new servlet, in your web browser, by viewing a URL like the following (the filename on the end of the URL will depend upon the name you chose for the http://localhost:7001/HelloWorldApp/Counter
© 2008 University of Technology, Sydney. All Rights Reserved...d. Redistribution without permission prohibited.