Controlling Java Compiler by Program

I want to dynamically generate java source files and compile them into .class files for users to download. Is there any way to programmatically control the jdk compiler? I know Tomcat is able to compile JSP into class files dynamically. How does it do this?