views:

244

answers:

2

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.execSelect();
ResultSetFormatter.out(System.out, rs);

Any idea?

+1  A: 

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>
...
BalusC
:: It is not possible for create List<RowObject> and got number of errors.Is there any way to doing that except JSTL.stuck here..help me bro!Thank you!
udayalkonline
Errors tell something about the cause of the problem. You should not ignore them, but interpret them and fix the problem accordingly. If you can't interpret errors, ask a question here.
BalusC
:: Hey bro.. I got the answer that in my own way!Thank you very much for your reply and comments!
udayalkonline
You're welcome. Don't forget to mark the answer accepted. Or if it is fundamentally an entirely different approach, you should post your own answer.
BalusC
A: 

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 = result.nextSolution();
out.println(binding.get("ind")); }

Note:

"ind" is a variable that you are refering in SPARQL query SELECT clause.

Thank you!

udayalkonline
This does fundamentally the same as my answer, only you're printing it at the wrong place. The normal practice is that you display the results in the JSP file (the View), not in a servlet (the Controller). You need to create a Model and pass it to the View, as outlined in my answer.
BalusC