开发者

Print the jena result set in html(servlet/jsp)

开发者 https://www.devze.com 2023-01-02 06:09 出处:网络
I\'m using servlet for manipulating ontology. I got the result of my SPARQL query and I want to display(print) that result in JSP (Servlet).

I'm using servlet for manipulating ontology. I got the result of my SPARQL query and I want to display(print) that result in JSP (Servlet).

Following code segment can be used to print the result in 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.execS开发者_如何学Pythonelect();
ResultSetFormatter.out(System.out, rs);

Any idea?


I don't do Jena, but basically you would like to iterate over the com.hp.hpl.jena.query.ResultSet and map the information into a List<RowObject> where RowObject is your own model class which represents a single row you'd like to display in a HTML table. After mapping, put the List<RowObject> in the request scope and forward the request to a JSP.

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

Then in JSP, use JSTL c:forEach to iterate over the List<RowObject>, printing a HTML table.

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

Update based on your other answer, here's how you could create a List<RowObject> based on the Jena's 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);
}

And display it as follows:

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


This code segment is going to your Servlet or You can implement that using seperate java class too.

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 a variable that you are refering in SPARQL query SELECT clause.

Thank you!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号