Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Intro to Java Web Development with Spark Bells and Whistles Using Filters and Request Attributes

Gábor Tóth
Gábor Tóth
11,958 Points

Halt()?

HI! Could someone explain me the halt method we used after redirecting and also tell me why we need it?

2 Answers

Seth Kroger
Seth Kroger
56,413 Points

Spark.halt() is used to stop the rest of the before() and after() filters from being processed. Since we are redirecting somewhere else, there's no further need to process the request and response any further. Although this is the last of 2 before() filters we have now, that may not always be the case. Calling halt() prevents any filters we may add in the future from running necessarily .

You didn't post any code.. So I wont be able to tell you how and why you're using the halt method in your specific situation. But as for what its use is, it is used to forcibly terminate a currently running java virtual machine, and you inherit this method from the Runtime class. The halt method returns nothing and it takes an int 'status' as an argument, a nonzero status code indicates abnormal termination.

Here's a basic example of using the halt method:

public class RuntimePrac {

    public static void main(String[] args) {
        System.out.println("Starting...");

        //halts your process and terminates the currently running JVM.
        Runtime.getRuntime().halt(0);

        //this line of code never runs.
        System.out.println("Still running...");
    }
}

The output is:

Starting...

If you wanna read more about the Runtime class and the halt method, you can find more about it here: https://docs.oracle.com/javase/8/docs/api/java/lang/Runtime.html

Also, if you want me to tell you a little bit more specifics, as to why and how you're using the halt method. You're gonna need to post the code where it's being used.

Derek, The reason Gábor Tóth didn't post any code is because that person was asking a question on the page in which the lesson was presented. It's specifically in relation to Spark Java and not Java runtime. I agree that this may pose a challenge for you responding to a question, but I also don't think the burden is on the question-asker to provide the whole context for the question. From the point of view of the person asking the question, the context seems completely apparent: what's happening in the lesson on which the question is being presented? Luckily the context for the question can be determined by following the breadcrumbs across the top of the page of the question.

  • Gene