/* * @(#)SingleThreadServlet.java 1.3 97/11/14 * * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved. * * This software is the confidential and proprietary information of Sun * Microsystems, Inc. ("Confidential Information"). You shall not * disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into * with Sun. * * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING * THIS SOFTWARE OR ITS DERIVATIVES. */ import java.io.*; import javax.servlet.*; import javax.servlet.http.*; /** * A sample single threaded servlet. * You can check the result of running that servlet in both single threaded * and non single threaded mode by removing the implements statement. *
To run the test, you will need to hit the servlet with multiple * requests at the same time, by targeting two browsers at it for example. * As the two requests are processed simultaneously, the counter * variable (whose access is not synchronized) will get wrong, while * the totalCount which is properly synchronized will always work. *
Expect correct results only in the case of the servlet implementing * the SingleThreadedModel. * @author Anselm Baird-Smith */ public class SingleThreadServlet extends HttpServlet implements SingleThreadModel { static int servletId = 0; static int counterSum = 0; static SingleThreadServlet alls[] = new SingleThreadServlet[100]; int id = -1; int counter = 0; String stringProperty = null; synchronized static int getNextId(SingleThreadServlet servlet) throws ServletException { if ( servletId >= alls.length ) throw new ServletException("Too many servlet instances !"); alls[servletId] = servlet; return servletId++; } synchronized static void incrTotalCounter() { counterSum++; } synchronized static int allCounters() { int total = 0; for (int i = 0 ; i < servletId ; i++) total += alls[i].counter; return total; } public void init(ServletConfig config) throws ServletException { super.init(config); this.id = getNextId(this); } public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException { int currentCounter = counter; res.setContentType("text/html"); res.setHeader("Pragma", "no-cache"); res.setHeader("Cache-Control", "private"); // Render the current thread binding: ServletOutputStream out = res.getOutputStream(); Thread thread = Thread.currentThread(); out.println("
lastId: "+servletId); out.println("
Thread: "+thread); out.println("
id: "+id); out.println("
AllCounter: "+allCounters()+" Total "+counterSum); out.println("
stringProperty="+getStringProperty()); out.println(""); counter = currentCounter+1; incrTotalCounter(); } /** * A bean property. * If considered as a bean, bean property changes done through the admin * are broadcasted to all instances of the servlet. */ public void setStringProperty(String stringProperty) { this.stringProperty = stringProperty; } /** * Set the bean property value. */ public String getStringProperty() { return stringProperty; } }