How to Make a Servlet Thread Safe

Java's interface allows you to create intelligent and interactive code for controlling input and response. If you have multiple Java applications and servlets, you can fix synchronization and memory issues with a thread safe code added to your servlet.

Instructions

    • 1

      Open your Web page's HTML code and navigate to the servlet that you wish to protect.

    • 2

      Write the following declaration anywhere within the servlet's code:

      <%@ page isThreadSafe="false" %>

    • 3

      Add in a scriptlet to the servlet to synchronize access to share objects within your JSP page with the following code:

      <%
      synchronized (application) {
      SharedObject foo = (SharedObject)
      application.getAttribute("sharedObject");
      foo.update(someValue);
      application.setAttribute("sharedObject",foo);
      }
      %>

    • 4

      Save the changes to your HTML and test your servlet's response to ensure the code works.

Related Searches

References

Related Ads

Featured
View Mobile Site