Java Report Parameters Example

This example demonstrates the basic steps required to programmatically set parameter fields for the report in a Java viewer. The report used in this example is created in the Crystal Reports Designer and has two parameter fields: Region and Country Code. The default values of the parameter fields and the new values that the code will set them to are as follows:

  1. Import the packages required for this example.
    <%@ page import=
          "com.crystaldecisions.report.web.viewer.*,
           com.crystaldecisions.sdk.occa.report.data.*"  %>
    
  2. Create and initialize the objects needed to set report parameters.
    Fields fields = new Fields();
    
    ParameterField pfield1 = new ParameterField();
    ParameterField pfield2 = new ParameterField();
    
    Values vals1 = new Values();
    Values vals2 = new Values();
    
    ParameterFieldDiscreteValue pfieldDV1 = new ParameterFieldDiscreteValue();
    ParameterFieldDiscreteValue pfieldDV2 = new ParameterFieldDiscreteValue();
    
  3. Set the parameter name and value for both parameter fields.
    pfield1.setName("Region");
    pfieldDV1.setValue("Japan");
    pfieldDV1.setDescription("The region is Japan");
    
    Integer CountryCode = new Integer("81");
    pfield2.setName("Country Code");
    pfieldDV2.setValue(CountryCode);
    pfieldDV2.setDescription("The country code is 81");
    
  4. Add the parameter field values to the array of values.
    vals1.add(pfieldDV1);
    vals2.add(pfieldDV2);
    
  5. Set the current array of values for both parameter fields.
    pfield1.setCurrentValues(vals1);
    pfield2.setCurrentValues(vals2);
    
  6. Add the parameter fields to the array of fields.
    fields.add(pfield1);
    fields.add(pfield2);
    
  7. Set the parameter fields for the viewer and disable user prompting.
    viewer.setParameterFields(fields);
    viewer.setEnableParameterPrompt(false);
    
  8. Refresh the viewer to apply new parameters.
    viewer.refresh();
    
  9. Have the viewer render the page.
    viewer.processHttpRequest(request, response, getServletConfig().getServletContext(), null);
  10. Destroy the viewer.
    viewer.dispose();