Link to home
Create AccountLog in
Avatar of dkim18
dkim18

asked on

Creating public class that makes simultaneous requests in JSP

hi!

this is continuous question of the following question.
https://www.experts-exchange.com/questions/21338416/JSP-timing-program-and-all-hit-the-URL-at-the-same-time.html

I was trying to create public class in scriplets, but I had compile errors. And calling Test () method twice did not help. Also Test() needed to return something and by making while loop infinite didn't compile either and that is how I changed like this.

Now how do I call this Test() method simultaneously?
+++++++++++++++++
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<%@ page import="java.util.*, java.io.*, java.net.*, java.util.Date" %>
<%@ page isThreadSafe="false"%>
<HTML>
<HEAD>
  <TITLE> Thread Test Case</TITLE>
</HEAD>


<BODY>

<%!
//public class Test(){
private String Test(){
       
       String strURLs="";

          String[] urls = {"http://webdev.apl.jhu.edu/~dkim18/hw5/ColorTestOne.jsp",       "http://webdev.apl.jhu.edu/~dkim18/hw5/ColorTestOne.jsp",
                      "http://webdev.apl.jhu.edu/~dkim18/hw5/ColorTestOne.jsp", "http://webdev.apl.jhu.edu/~dkim18/hw5/ColorTestOne.jsp",
                      "http://webdev.apl.jhu.edu/~dkim18/hw5/ColorTestOne.jsp"};
         
          StringBuffer sb = new StringBuffer();

              int i=0;
          while(i<2){
               for( int jj=0 ; jj < urls.length ; jj++){
             
                    try {
                         
                         URL u = new URL( urls[jj] );
                         HttpURLConnection huc = (HttpURLConnection) u.openConnection();
                         huc.setRequestMethod( "GET" );
                         huc.connect();
                         BufferedReader br = new BufferedReader(new InputStreamReader(huc.getInputStream()));

                         int b = 0;
                         while((b = br.read()) != -1) {
                            sb.append((char)b);
                         }
                         br.close();
                       
                         huc.disconnect() ;                                                                                                
                                     strURLs=strURLs + new Date()+ "\n";              

                    }catch (IOException e){
                         System.out.println( "Unable to open connection: " + e.getMessage() );
                    }finally{}//end try/catch connection
                    try {
                         Thread.sleep( 1000 * 1 );// every 1 second
                    } catch (InterruptedException e) {
                         e.printStackTrace();
                    }finally{}//end try/catch Thread.sleep
               }//end for
                     i++;
          }//end while
 
  return strURLs;
}//end class
%>




<%=Test()%>
<%=Test()%>

</BODY></HTML>
+++++++++++++++
thanks,
Avatar of kupra1
kupra1

use
<% = new Test();%>
<% = new Test();%>
instead of
<% = Test();%>
<% = Test();%>

What other exceptions you are getting?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of bloodredsun
bloodredsun
Flag of Australia image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer