Concurrent Processes in a Servlet Object

Concurrent Processes in a Servlet Object thumbnail
A page can only have a single object instance of a servlet.

Applets aren't the only way web developers can use Java code to create websites. Servlets function as individual objects on a JavaServer Pages (JSP) site. However, servlets encounter the same issues as other Java programs when concurrent tasks modify the same variable out of sequence. This is because of the software model of servlets, but there are ways to manage multiple processes within the servlet to prevent these issues.

  1. Servlet Object Model

    • Servlets function as classes that webpages instantiate as objects. When a web developer calls a servlet object within a frame, the servlet code goes to the Java compiler, then the compiled class file goes to the server's Java runtime environment (JRE). When the page loads for the viewer, the JRE will execute that compiled object file within that specific HTML frame. However, the framework within which servlets operate is not sophisticated enough to handle multiple instance objects.

    Servlet Concurrency

    • Web developers can embed the same object in different frames on the same page. The object's code will execute independently in each of those frames. However, it will be the code from the same instance. Unlike different instances of the same class which all have independent sets of variable values, each of those frames will be executing off the same set of variable values. This creates the issue of concurrency among servlet objects.

    Servlet Multitasking

    • Servers handle multitasking in Java servlets by creating multiple threads within a single process, not by creating multiple processes. This can be multi-tasking as part of the servlet's actual task, or due to the web developer embedding the same object in multiple page frames. The JRE will treat each frame as a separate thread. These multiple threads from multiple frames can overlap and corrupt variables when threads modify the same variable at the same time.

    Concurrency Safety

    • There are two standard ways of managing concurrency with Java servlets. The first is Java's standard method of preventing data corruption: lock objects. These allow a thread to literally lock out other threads from accessing a variable until it is done with the variable. The other way is to always explicitly reference variables and values instead of referencing "self," a term for the object itself. The object's exact state will differ between the frames, so code that references specific values will not encounter the same concurrency errors as code that references an object's state.

Related Searches:

References

  • Photo Credit Hemera Technologies/Photos.com/Getty Images

Comments

Related Ads

Featured