### Invoking the Server API

JReport Server API can be invoked either by JSP or Servlet. This section presents to you the two methods in detail.

#### Invoking using JSP

There are example JSPs which use the Server API in `<intall_root>\public_html\jinfonet`.

WebViewServlet and SendFileServlet are built-in servlets. WebViewServlet responds to the run-report requests, while SendFileServlet sends the requested file to the client end. For Example, our JSP example rsthtml.jsp calls SendFileServlet to send files such as jar file and report set results, and getRptDescPage.jsp calls WebViewServlet to run report sets.

Here is a simple JSP example:

    <%@ page import="java.io.*, java.util.*,jet.cs.util.*" %>
    <%@ page import="jet.server.api.http.*" %>
    <%@ page import="jet.server.api.*" %>
    <%
    try{
    if( !HttpUtil.checkLogin(request, response) )
    return;
    }catch(TooManyUsersException e){
    %>
    Too many users!
    <%
    return;
        }catch(TooManyTimesException e){
    %>
    Too many times to try to login!
    <%
    return;
    }

    if( !HttpUtil.checkPermission(request) ){
    %>
    Access denied!
    <%
    return;
    }
    try{
    HttpRptServer httpRptServer = HttpUtil.getHttpRptServer();

    String user = HttpUtil.getUser(request);
    String cat = "/SampleReports/SampleReports.cat";
    String rptName = "CustomerAnalysis.cls";

    Properties ht = new Properties();
    int rstType = APIConst.HTML;

    // set result type
    ht.put(APIConst.TAG_RESULT_TYPE, String.valueOf(rstType));

    System.out.println("============================ht="+ht);
    // the rst is name of the first HTML result page
    String rst = httpRptServer.runReport(user, cat, rptName, ht);
    if (rst == null) {
    // warning error
    } else {

    // register the owner of the result.
    httpRptServer.getTempResultOwnerManager().registerOwner(user,
    HttpUtil.getTempResultKey(new File(rst).getName()));

    // make the URL to view the first HTML result page.
    // The "/sendfile/" is the path of built-in servlet of SendFileServlet
    // of JReport Enterprise Server. This rstURL will redirect to the SendFileServlet.
    // The SendFileServlet will send the HTML result page to the client.
    String rstURL = request.getScheme() + "://" + request.getServerName() +
    ":" + request.getServerPort() +
    "/servlet/sendfile/result/" + HttpUtil.encodeEsc(new File(rst).getName());
    // redirect to the rstURL
    response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
    response.setHeader("Location", rstURL);
    response.setHeader("Content-Location", rstURL);
    }
    }catch(RptServerException e){
    // output error
    e.printStackTrace();
    }catch(Throwable t){
    // output error
    t.printStackTrace();
    }
    %>

#### Invoking using Servlet

After you have installed JReport Enterprise Server, you will find the illustrative test program ViewHtmlPipelineServlet.java in `<install_root>\help\server\en\samples`.

Here is how ViewHtmlPipelineServlet.java works. First, the mapping.properties file and servlet.properties file in `<install_root>\bin` should be modified before it can be run. `/viewrpt=viewrpt` should be added into mapping.properties. `servlet.viewrpt.code=ViewHtmlPipelineServlet` should be added into the servlet.properties file as below:

    # jrserver servlet
    servlet.jrserver.code=jet.server.servlets.JRServlet
    servlet.jrserver.initArgs=\
    temp_dir=temp,\
    history_dir=history,\
    default_doc=docs\\index.html,\
    debug=on
    servlet.viewrpt.code=ViewHtmlPipelineServlet
    servlet.webview.code=jet.server.servlets.WebViewServlet
    servlet.sendfile.code=jet.server.servlets.SendFileServlet
    servlet.webreporting.code=jet.web.design.Designlet
    servlet.jrdhtml.code=jet.web.dhtml.DHTMLRunReportlet
    servlet.dhtml.code=jet.web.dhtml.DHTMLlet
    servlet.help.code=jet.web.dhtml.JHelplet
    servlet.jspservlet.code=org.apache.jasper.servlet.JspServlet
    servlet.jspservlet.initArgs=keepgenerated=false, development=false, reloading=true,
    scratchdir=C:\JReport\Server\scratchdir

Note: If you want to access the temporary result files generated by running reports with the Server API, you should register as the owner of the temporary results by invoking the method jet.server.api.TempResultOwnerManager.registerOwner(String owner, String tempResult) and then you can view the temporary result files via URL or by clicking the link on the Background Tasks page, or else an Access denied error warns.