All you want to know about Scriptlets!!

noreply@blogger.com (Ankur Gupta)
AgileTech - Ankur Gupta )


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

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

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

Now following listed are the methods that will be called by the 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 using scriptlets are-
  1. When we do a System.out.println in any of the function then it does not directly print in the report, many a times people have some misconception but to remind System.out.println  is a console command and prints the value in the Report console, that is called the Report output window.
  2.  These functions are fired when the 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 in our 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 by clicking 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 the node and we could see REPORT
  4.  You can create a another scriptlet label by right clicking the scriptlet label and then select Add
  5. Now locate the new element called Scriptlet1 under the node SCRIPTLET. (Check Fig-1)
  6.  Navigate to the properties of the Scriptlet1 there could be seen Name, Scriptlet Class and Description.
  7. Now you can 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 of the scriptlet ( Check Fig-2)



Follow the following  steps to implement scriptlets in  JasperSoft Studio
  1.  Add the Scriptlet jar file in the classpath by clicking 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 the node and we could see REPORT_SCRIPTLET
  4.  You can create a another scriptlet label by right clicking the scriptlet label and then select Create Scriptlet (As in Fig 1)
  5. Now locate the new element called Scriptlet_1 under the node SCRIPTLETS.
  6.  Navigate to the properties of the Scriptlet_1 there could be seen Name, Class and Description.
  7. Now you can enter the desired name for the scriptlet.
  8.  Class is the fully qualified path of the scriptlet class in the JAR File.
  9.  Description is for the describing the role of the scriptlet.  (As in Fig 2)




Now until now we have success fully linked the scriptlet JAR with the iReport/JasperSoft Studio, now we need to understand that how can it be used in the report in order to get the desired outputs from the scriptlet or basically from the JAVA end.

To use the scriptlet functions in either iReport/ JasperSoft Studio following are the steps:
  1.  When we successfully create a new scriptlet under the scriptlet node, and define the properties of the scriptlet we could  see a new parameter formed under the Parameter node.
  2.  Locate the scriptlet parameter, you can find the parameter as by default it is named as ( {name of the scriptlet defined}_SCRIPTLET )
  3. Then wherever you want to use any function of the 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 Jasper Reports, hope that the above description and methodology helps to grasp important 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 face any issues or problems then you can add a comment below.


Happy Coding!!
Feedback
randomness