I run the following code on my machine and sample.jsp runs on my tomcat server. sample.jsp has code running in an infinite loop.
public static void main(String[] args) throws Exception { URL obj = new URL("http://localhost:8080/bank/sample.jsp"); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); con.setRequestMethod("GET"); if (con.getResponseCode() == HttpURLConnection.HTTP_OK) { // success ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); try(BufferedInputStream stream = new BufferedInputStream(con.getInputStream()) ) { byte[] buffer = new byte[1024]; int bytesRead = 0; while ( (bytesRead = stream.read(buffer)) > 0 && byteArrayOutputStream.size() <= 1024 * 100) { byteArrayOutputStream.write(buffer, 0, bytesRead); } } if(byteArrayOutputStream.size() > 1024 * 100){ stream.close(); con.disconnect(); throw new Exception("content too big. hence terminating..."); } } } Following is the code in my jsp :
<%@page import = "java.io.File" %> <%@page import = "java.io.FileWriter" %> <% for(long i=0;i < 10;) { out.println("HelloHelloHelloHelloHelloHello"); } %> I close the connection as well as the stream that I opened when the output stream exceeds a particular size. This doesn't stop the process started on my tomcat server. But I want to stop the server side program when connection is closed from client side. I donot get IOException either and the thread seems to be running indefinitely. Can anyone help me on how to stop the thread.