Skip to main content
added 308 characters in body
Source Link

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. 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. Can anybody help me on how to stop the process as well.

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.

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. 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. Can anybody help me on how to stop the process as well.

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..."); } } } 

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.

added 2 characters in body; edited tags
Source Link

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. 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 Exceptionconnection is thrown in my java codeclosed from client side. Can anybody help me on how to stop the process as well.

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..."); } } } 

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. 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 Exception is thrown in my java code. Can anybody help me on how to stop the process as well.

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..."); } } } 

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. 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. Can anybody help me on how to stop the process as well.

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..."); } } } 
Source Link

Stopping server thread on client connection termination

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. 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 Exception is thrown in my java code. Can anybody help me on how to stop the process as well.

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..."); } } }