Print jena result set in html (servlet / jsp)

I use a servlet to manage ontology. I got the result of my SPARQL query, and I want to display (print) the result of JSP (Servlet).

The following code segment can be used to output the result to the console.

com.hp.hpl.jena.query.Query query = QueryFactory.create(queryStr);
QueryExecution qe = QueryExecutionFactory.create(query,model);
com.hp.hpl.jena.query.ResultSet rs = qe.execSelect();
ResultSetFormatter.out(System.out, rs);

Any idea?

+3
source share
2 answers

This code segment is suitable for your servlet, or you can implement this using a separate java class.

com.hp.hpl.jena.query.Query query = QueryFactory.create(queryStr);
QueryExecution qe = QueryExecutionFactory.create(query,model);
com.hp.hpl.jena.query.ResultSet rs = qe.execSelect();

while(rs.hasNext()){

 QuerySolution binding = rs.nextSolution();                     
 System.out.println(binding.get("ind")); 
}

Note:

"ind" is the variable that you specify in the SELECT clause of the SPARQL query.

Thank!

+1
source

Jena, com.hp.hpl.jena.query.ResultSet List<RowObject>, RowObject - , , HTML. List<RowObject> JSP.

List<RowObject> results = getItSomeHow();
request.setAttribute("results", results); // Will be available as ${results} in JSP
request.getRequestDispatcher("page.jsp").forward(request, response);

JSP JSTL c:forEach, List<RowObject>, HTML.

<table>
    <c:forEach items="${results}" var="rowObject">
        <tr>
            <td>${rowObject.someProperty}</td>
            <td>${rowObject.anotherProperty}</td>
            ...
        </tr>
    </c:forEach>
</table>

, , List<RowObject> Jena ResultSet:

List<RowObject> results = new ArrayList<RowObject>();
while (rs.hasNext()) {
    RowObject result = new RowObject();
    QuerySolution binding = result.nextSolution();
    result.setInd(binding.get("ind"));
    result.setSomethingElse(binding.get("something_else"));
    // ...
    results.add(result);
}

:

...
<td>${rowObject.ind}</td>
<td>${rowObject.somethingElse}</td>
...
+2

Source: https://habr.com/ru/post/1748751/


All Articles