File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Dynamic code execution within jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Dynamic code execution within jsp" Watch "Dynamic code execution within jsp" New topic
Author

Dynamic code execution within jsp

Yahya Elyasse
Ranch Hand

Joined: Jul 07, 2005
Posts: 510

Hi guys,
what i'm trying to do is execute some simple java code dynamically...
well i know this involves using ClassLoader and reflection to invoke the main method of the java program. of course this needs to be done from a servlet or jsp page

however someone more knowledgeable than me propose me this :

If the program uses System.out or System.err (as most simple ones do) it's a bit of a pain, because output from the entire web server goes there.

You can override System.out by creating a new PrintStream object and passing it to System.setOut() but that's going to affect everything that happens to be written for the entire webserver.

What I'd do is to create a special OutputStream object which steared "write" attempts to some suitable cache. But it's behaviour has to depend if it's running on the thread that's executing your user program (or another thread created by the user program). (Overriding the OutputStream is simpler than writing a complete PrintStream.)

Then I'd permanently, one time, insert those new writers into System.

Because of multi-threading it's not enough to set the output back again after the program has run.

There's a class called InheritableThreadLocal, which allows an object to have a different value for each thread it runs on. You can stear the write requests using that.

Before running the client program create an appropriate OutputStream (a temporary file or memory buffer) and insert set the InheritableThreadLocal to point to it. After the client had run (or crashed, use finally) close the stream and set the ThreadLocal to null.

The write methods of the Streams you direct out and err to check the ThreadLocal, and if it's not null send the data there, if it is null they send it to the default writers.


Well I need someone to help me translate above into real java code...I would appreciate some java code to get me started on this.
thanking you !
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

Moving to the JSP forum.


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Dynamic code execution within jsp
 
Similar Threads
InheritableThreadLocal caches the value for all threads!
Dynamic code execution within jsp
It's all about Stream...
Is there any newsletter for scjp?
Real-time Printing on other application