title: Servlet multiple request handle at same time on tomcat
body: <p>I have written a servlet program which is calling a java class to generate the random strings. I have also written an HTML file which has 1000 IFRAME pointing to the servlet address. If multiple requests are going to a servlet then each request will be processed in a separate thread. So in this case, 1000 threads are being created(1000 request). The problem is that it is taking to much of time to processed and not able to handle if requests are more than 1000 and so on. It becomes slower if I do the complex calculation at the back-end. What changes need to be done at the servlet level(multithreading) or at tomcat level(if possible) for the fast response. Any suggestion?</p>
  
  <h2>Servlet Code</h2>
  
  <pre><code>@WebServlet("/test")
  public class MyServlet extends HttpServlet {
              private static final long serialVersionUID = 1L;
  private static PrintWriter out=null;
  private UserOperation operation=null;
  private static int counter=0;
  public MyServlet() {
      super();
      operation=new UserOperation();
  }
              protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                              counter++;
                              out=response.getWriter();
                              String output=operation.getResult();
                              System.out.println(counter+":"+output);
                              out.print(output);
                              return;
              }
  
              protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                              doGet(request, response);
              }
  }
  </code></pre>
  
  <h2>UserOperation.java</h2>
  
  <pre><code>import java.util.Random;
  
  public class UserOperation {
              private Random rand=null;
              private int max=9;
              private int min=0;
              private static final String [] RESULT= {"ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","NINE","TEN"};
              public UserOperation() {
                              rand=new Random();
              }
              public String getResult() {
                             int randNum=rand.nextInt((max-min)+1)+min;
                              return RESULT[randNum];
              }
  }
  </code></pre>
  
  <h2>HTML File</h2>
  
  <p>1000 times IFRAME call. below line has been copy paste 1000 times in the HTML file.</p>
  
  <pre><code>&lt;IFRAME src="http://localhost:8080/MultipleRequest/test" width="30px" height="30px"&gt;&lt;/IFRAME&gt;
  </code></pre>