Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439

This laboratory exercise involves modifying your Hello World servletto 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:


A Counter servlet

The goal of this exercise is to create a servlet that maintains a counterof the number of times it has been invoked since it was last reloadedby the web server. It will use a class variable (i.e. static) tokeep count of the number of invocations. Each time the servlet isinvoked, it will print out the value of the counter.

There are 2 main ways to do this lab.

Copying an existing Servlet 

Begin by copying your Hello World servlet code, and putting it intoa file called CounterServlet.java. You may keep it inthe same project  (WAR file) as before, or put it in a separate WAR file (if youwould like more practice at the process of creating WAR files).

Make the following modifications to your Hello World servlet sourcecode:

  1. You can cut and paste the HelloWorldServlet from the src folder. Rename the class to CounterServlet

  2. Declare a class variable to hold the value of your counter.

      private static int numRequests = 0;
  3. Declare a synchronized method to update the value of thenumRequests variable.

      private synchronized void incNumRequests() {    numRequests++;  }
  4. Modify the code inside your existing doGet() methodto make it call the incNumRequests method you havejust 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 valueof 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 instancesof your doGet() method could be executing simultaneously.

Also recall the meaning of a class variable (i.e. declared asstatic) in Java. A static variable is one that is shared between all instances of a given class. So if you have10 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 runningat the same time both try and update the value of numRequestsat the same time? In theory, this could lead to a semantic error inthe program because two threads are trying to update the same sharedresource simultaneously. [Although strictly speaking in this casethere is not much danger because all we are doing is incrementinga single value.]

The general principle that you should follow is that if you ever writea Java servlet where multiple threads will be accessing a single,non-shareable resource, you must programmatically ensure that atmost one thread can be accessing the resource at any one time.Java provides this facility through the use of itssynchronized keyword.

If you have studied operating systems, note that this is similarin concept to monitors and semaphores used to ensure at most oneprocess 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.

  1. 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.

  2. 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 likethe following (the filename on the end of the URL will dependupon the name you chose for the <url-pattern>