• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Dynamic code execution within jsp

Ranch Hand
Posts: 510
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 !
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to the JSP forum.
Thanks tiny ad, for helping me escape the terrible comfort of this chair.
the value of filler advertising in 2021
    Bookmark Topic Watch Topic
  • New Topic