Details
-
Bug
-
Status: Closed
-
Major
-
Resolution: Fixed
-
1.7.3
-
None
-
Windows/Linux running within Apache Tomcat/6.0.26
Description
In groovlet print and println are bound to the response.Writer() or out variable so lone print ... and println ... calls are written th the response output Writer as is calls to out.
However, printf calls in goovlet context is still bound to System.out so calls to printf write to System.out not out.
printf method getting invoked is following:
package org.codehaus.groovy.runtime.DefaultGroovyMethods public static void printf(Object self, String format, Object arg) { if (self instanceof PrintStream) { printf((PrintStream) self, format, arg); else printf(System.out, format, arg); **** this branch is getting invoked }
Object self is Groovlet class itself which is not instance of PrintStream so System.out is getting used.
Work-around is not using "printf" short-cut but explicitly call print/println and String.format() instead.
Test output:
groovy script output: 1. println test 2. this is a test 3. this is a test groovlet response: 1. println test 3. this is a test 4. test out.println
Groovlet test.gsp
try { response.contentType = 'text/plain' groovylet = true } catch (RuntimeException rte) { groovylet = false } // output to groovlet response println "1. println test" //************************************************************************ // in groovyet context this gets output to System.out not out which is bound to response.getWriter() printf "2. this is a %s%n", 'test' // this is the problem case in groovlet mode //************************************************************************ print String.format("3. this is a %s%n", 'test') if (groovylet) { out.println "4. test out.println" }