Jump to content
  • All you want to know about Scriptlets!!



    First question which comes to mind after coming across thisword Scriptlet is that does it have any relationships with scripts, if yes thenwhat scripts, the answer  to thisquestion is No it does not have any relation with scripts.    Scriptlets basically act as an interfacebetween the Jasper Reports and Java. It provides much of flexibility to thereporting as now we can introduce user defined functions.

    Scriptlet is nothing but a java class which is extended witheither
    1. net.sf.jasperreports.engine. JRAbstractScriptlet
    2. net.sf.jasperreports.engine.JRDefaultScriptlet.

    Now we need to identify which class we need toextend based upon the usage, JRAbstractSriptlet contains anumber of abstract methods that must be overridden in every implementation, developermust implement all the abstract methods whereas JRDefaultScriptlet contains default empty implementations of everymethod in JRAbstractScriptlet, a developer is only required to implement thosemethods he/she needs for their project.


    Now following listed are the methods that will be called bythe report engine at the appropriate time, during report filling phase.

    public void beforeReportInit()

    Called before report initialization.

    public void afterReportInit()

    Called after report initialization.

    public void beforePageInit()

    Called before each page is initialized.

    public void afterPageInit()

    Called after each page is initialized.

    public void beforeColumnInit()

    Called before each column is initialized.

    public void afterColumnInit()

    Called after each column is initialized.

    public void beforeGroupInit(String groupName)

    Called before the group specified in the parameter is initialized.

    public void afterGroupInit(String groupName)

    Called after the group specified in the parameter is initialized.

    public void beforeDetailEval()

    Called before each record in the detail section of the report is evaluated.

    public void afterDetailEval()

    Called after each record in the detail section of the report is evaluated.




    Except these we can also use User defined custom functions,I have already posted about them in my previous post here is the link-

    Few important facts which need to be kept in mind usingscriptlets are-
    1. When we do a System.out.println in any of thefunction then it does not directly print in the report, many a times peoplehave some misconception but to remind System.out.println  is a console command and prints the value inthe Report console, that is called the Report output window.
    2.  These functions are fired whenthe Jasper Print object is created, and it is created when a JRXML runs.


    Now after creating our scriptlet classes and packaging  it into a jar file we are ready to use it inour Jasper Report created either in iReport or JasperSoft Studio.

    Follow the following steps to implement scriptlets in iReport
    1.  Add the Scriptlet jar file in the classpath byclicking on the Tools(Menu Bar) àOptions àClasspath Tab  and add the Jar file here.
    2.  Locate the Report Inspector Window
    3.  There is a node called Scriptlet, expand thenode and we could see REPORT
    4.  You can create a another scriptlet label byright clicking the scriptlet label and then select Add
    5. Now locate the new element called Scriptlet1under the node SCRIPTLET. (Check Fig-1)
    6.  Navigate to the properties of the Scriptlet1there could be seen Name, Scriptlet Class and Description.
    7. Now youcan enter the desired name for the scriptlet.
    8.  Scriptlet Class is the fully qualified path of the scriptlet class in the JAR File.
    9.  Description is for the describing the role ofthe scriptlet ( Check Fig-2)
    Follow the following steps to implement scriptlets in JasperSoft Studio
    1.  Add the Scriptlet jar file in the classpath byclicking on the Project(Menu Bar) àProperties, then select Java Build Path and external JAR File.
    2.  Locate the Outline window
    3.  There is a node called Scriptlet, expand thenode and we could see REPORT_SCRIPTLET
    4.  You can create a another scriptlet label byright clicking the scriptlet label and then select Create Scriptlet (As in Fig 1)
    5. Now locate the new element called Scriptlet_1under the node SCRIPTLETS.
    6.  Navigate to the properties of the Scriptlet_1there could be seen Name, Class and Description.
    7. Now you can enter the desired name for thescriptlet.
    8.  Class is the fully qualified path of thescriptlet class in the JAR File.
    9.  Description is for the describing the role ofthe scriptlet.  (As in Fig 2)




    Now until now we have success fully linked the scriptlet JARwith the iReport/JasperSoft Studio, now we need to understand that how can itbe used in the report in order to get the desired outputs from the scriptlet orbasically from the JAVA end.

    To use the scriptlet functions in either iReport/ JasperSoftStudio following are the steps:
    1.  When we successfully create a new scriptletunder the scriptlet node, and define the properties of the scriptlet wecould  see a new parameter formed underthe Parameter node.
    2.  Locate the scriptlet parameter, you can find theparameter as by default it is named as ( {name of the scriptletdefined}_SCRIPTLET )
    3. Then wherever you want to use any function ofthe scriptlet, you just need to use as:
      ( {name of the scriptlet defined}_SCRIPTLET ).function Name


    That is all we need to implement to use scriptlet in JasperReports, hope that the above description and methodology helps to graspimportant points.

    Here is a sample using the sample database, you can try the above customization too.

                                                ======SAMPLE========


    Hope this clears your doubt for this topic. Still if you faceany issues or problems then you can add a comment below.



    Happy Coding!!

    User Feedback

    Recommended Comments

    There are no comments to display.



    Guest
    This is now closed for further comments

×
×
  • Create New...