# Export with OZ Server

## Use RequestDispatcher

1. <mark style="color:blue;">**param.put("connection.inputjson", inputData)**</mark>
2. <mark style="color:blue;">**request.setAttribute("OZViewerExportParam", param)**</mark>
3. <mark style="color:blue;">**getRequestDispatcher("/server")**</mark>
4. <mark style="color:blue;">**request.getAttribute("OZViewerExportResult")**</mark>

{% code title="OZViewerExport.jsp" %}

```java
<%@ page import="java.util.*,java.io.*"%>
<%@ page contentType="text/html;charset=UTF-8" %>
<%request.setCharacterEncoding("UTF-8");%>
<%
    String ozrName	= request.getParameter("ozrName");
    String exportFormat	= request.getParameter("exportFormat");
    String inputData	= request.getParameter("inputData");
    // Export env
    String exportPath = "C:\\Program Files\\Apache Software Foundation\\Tomcat 9.0\\webapps\\exported\\demo\\";
    String exportFile = ozrName + "_OZViewerExport." + exportFormat; // KYC_Form.ozr
    String exportFilePath = exportPath + exportFile;
    String ozrPath = "demo\\" + ozrName + ".ozr";
    // OZ parameters 	
    Hashtable param = new Hashtable();	
    param.put("connection.reportname", ozrPath);
    param.put("connection.inputjson", inputData); // data to bind with ozr
    param.put("export.mode", "silent");
    param.put("export.confirmsave", "flase"); // no confirm = silent
    param.put("export.format", exportFormat);
    param.put("export.path", exportPath);
    param.put("export.filename", exportFile);
    param.put("pdf.fontembedding", "true"); // embed the font used in the OZR into the target pdf
    // request OZ server to bind and export
    request.setAttribute("OZViewerExportParam", param);
    RequestDispatcher dispatcher = pageContext.getServletContext().getRequestDispatcher("/server");
    dispatcher.include(request, response);
try {
    // get result from OZ server 
    Object obj = request.getAttribute("OZViewerExportResult");
    if (obj != null) {
	Hashtable t = (Hashtable) obj;
	byte[] bytes = (byte[]) t.get(exportFilePath);
	if (bytes != null && writefile(bytes, exportFilePath)) { 
	    out.println("OZViewerExportResult:\n" + exportFilePath);
	} 
    } else {
	Throwable t = (Throwable) request.getAttribute("OZViewerExportError");
	if(t != null) {
	    throw t;
	} else {
	    throw new Exception("No result from OZ Server.");
	}
    }
} catch (Throwable e) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    e.printStackTrace(pw);
    response.sendError(500, sw.getBuffer().toString());
} finally {
}
%>
<%!
public boolean writefile(byte[] b, String path)
{
    BufferedOutputStream fout = null;
    boolean result = false;
    try{
	fout = new BufferedOutputStream(new FileOutputStream(path));
	fout.write(b);
	fout.flush();
	fout.close();
	fout = null;
	result = true;
    }catch(Exception e){
	result = false; 
    }finally{
	if(fout!=null) try{fout.close();}catch(Exception e){}
    }
    return result;
}
%>
```

{% endcode %}

Example: <http://oz.ozeform.io/oz/export/export.html>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://edu.ozeform.io/server-developer/binding-and-exporting/export-api-example/export-with-oz-server.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
