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...");
}
}
}
Aucun commentaire:
Enregistrer un commentaire