Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Custom Translation step: Possible?  RSS feed

 
Andrei Hager
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I've done some work with JSPs, but I'm trying to figure out if something I want to do is possible with the technology or not.

Let's assume I define a custom tag, <custom:code>, which is invoked with arbitrary text inside it. (BTW, this forum needs a preview button! :P)

<custom:code><![CDATA[
Hello world!
]]></custom:code>

What I want to happen is I want to control the JSP compilation process... for instance, I want to have "Hello World!" moved into an external temporary file and instantiate a regular class with the relevant filename.

It's not a simple search-replace of JSP tags with Java code in the final servlet, since "Hello World", of whatever length, would still be stored as a literal string.

Is it possible to hook into any common web containers to provide this functionality?

I suppose one workaround is to write and run some sort of XML preprocessor manually and only deploy an altered version... Or to have the resulting servlet check if it's already pushed the string into a file each time...
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Terr",
Welcome to the JavaRanch.

We're a friendly group, but we do require members to have valid display names.

Display names must be two words: your first name, a space, then your last name. Fictitious names are not allowed.

Please edit your profile and correct your display name since accounts with invalid display names get deleted, often without warning

thanks,
Dave
 
Andrei Hager
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Uhm. If you insist...
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66184
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Andrei Hager:
Uhm. If you insist...


We do.

Now as to your question, you'll have to explain what you're wanting to do a little more clearly. I'm really not sure what your aim is.
[ March 06, 2007: Message edited by: Bear Bibeault ]
 
Andrei Hager
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dang... even without any angle brackets in my entire post, the forum complains about invalid characters... And they're all already escaped! I'll pare down the example... Pretend this is a JSP snippet

While I know I can use a tag handler so that the custom:code block becomes something like:

CustomCode.runPHP("long PHP string literal from the codeblock goes here");

I would like to instead control the specifics of the JSP-to-servlet translation so that I can put the PHP code into an entirely seperate file and instead have the final servlet be something like:

File f = new File("examplepage.jsp_code/1.php");
CustomCode.runPHPFile(f);

If I can make this happen at translation time, then it's a once-per-page-per-server-lifetime performance hit and I don't need to tack on some potentially bypassable layer of preprocessing.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66184
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you want to embed PHP code in the JSP?

That seems odd, in this day and age, when the best practice concsensus is to move scripting (Java scriptlets et al) out of the pages.

In any case there really isn't any way to hijack the translation phase -- the custom action sub-system, except for attribute validaton, is an execution-time mechanism.

If you're just trying to import content from PHP resources, why doesn't <c:import> suit your needs? Why the desire to put the PHP in the JSPS, where (in my opinion) it really doesn't belong?
 
Andrei Hager
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:
So you want to embed PHP code in the JSP?


I'm trying to think of ways of making a better version of an existing system which does support limited inline language mixing. The reason import statements won't quite do it is because I'd like let authors write PHP that would grab certain basic variables from a cross-language store.

While allowing direct scripting inside JSP pages isn't best practice, when it comes to other languages (say PHP) it's far easier to let someone do



Rather than tediously wrapping everything into some sort of tag abtraction for dealing with objects:

But anyway, thanks for your answer.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like Bear says, there is no way to hijack the translation phase.
<off-the-wall>
However, suppose you create a custom tag - on the first execution of the page the tag body content gets translated/turned into php, whatever and some sort of object representing the way to execute is stored. All subsequent executions of the tag recognize the body content and look up the object to execute it.
</off-the-wall>
Bill
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!