You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by Herli Joaquim de Menezes <he...@gmail.com> on 2011/09/12 04:44:40 UTC

Rendering SPARQL resultset in a JSP.

Hello, Friends

I don't know how to solve this. I wrote a simple Jena web application in
order to query on a simple LOM RDF file. I am using a MVC model, so that the
control view is composed by servlets. Excuse me for the long post...but here
is the code snippet:


{..............}
String filename = "/WEB-INF/TesteLOM2.rdf";
        ServletContext context = getServletContext();

        InputStream in = context.getResourceAsStream(filename);
        if (in != null) {
            Model model = ModelFactory.createMemModelMaker()
                    .createDefaultModel();
            model.read(in, null);
            // null base URI, since model URIs are absolute
            in.close();

            @SuppressWarnings("unchecked")
            List<String> lista = (List<String>) request.getSession()
                    .getAttribute("sugestao");
            String palavrachave = null;
            for (Iterator<String> iter = lista.iterator(); iter.hasNext();)
{

                palavrachave = iter.next();
                // Creates a query....
                String queryString =

                "PREFIX NS1:<http://www.exemplo.com/#> "
                        + "PREFIX rdf:<
http://www.w3.org/1999/02/22-rdf-syntax-ns#>"
                        + "PREFIX dcterms:<http://purl.org/dc/terms/>"
                        + "PREFIX dc:<http://purl.org/dc/elements/1.1/>"
                        + "PREFIX lom:<
http://ltsc.ieee.org/rdf/lomv1p0/terms/> "
                        + "PREFIX lomvoc:<
http://ltsc.ieee.org/rdf/lomv1p0/vocabulary#>"
                        + "SELECT  ?title ?LearningResourceType ?location  "
                        + "WHERE {"
                        + "        ?keyword lom:keyword  \""
                        + palavrachave
                        + "\""
                        + "  ."
                        + "     ?keyword dc:title        ?title  ."
                        + "        ?keyword lom:location    ?location ."
                        + "        ?keyword lomvoc:LearningResourceType
?LearningResourceType."
                        + "}";

                Query query = QueryFactory.create(queryString);

                // get the results...

                QueryExecution qe = QueryExecutionFactory.create(query,
model);
                ResultSet results = qe.execSelect();
                request.getSession().setAttribute("results", results);

                // Just for debugging....
                ResultSetFormatter.out(System.out, results, query);
                qe.close();

            }
            request.getAttribute("results");
            request.getRequestDispatcher(VIEW).forward(request, response);

VIEW is a JSP to render the results:

{...........}

<form method="post" action="ControlaBuscaDeObjetos">
<fieldset>
<legend> Objetos Selecionados </legend>

<p> some explanation...</p>

<table>
    <c:forEach items="${results}" var="item">
        <tr>
            <td>${item}</td>
            </tr>
    </c:forEach>
</table>

</fieldset>

</form>
++++++++++++++++++++++++
The terminal output is OK:

-------------------------------------------------------------------------
| title                         | LearningResourceType | location       |
=========================================================================
| "Força Resultante"            | "narrativetext"      | "file:///path" |
| "Calculo da Força Resultante" | "Problem Statement"  | "file:///path" |
-------------------------------------------------------------------------
------------------------------------------------------------
| title            | LearningResourceType | location       |
============================================================
| "Momento Linear" | "Problem Statement"  | "file:///path" |
------------------------------------------------------------
-------------------------------------------------------------------------------
| title                               | LearningResourceType |
location       |
===============================================================================
| "Velocidade"                        | "narrativetext"      |
"file:///path" |
| "Velocidade no Movimento Harmônico" | "Problem Statement"  |
"file:///path" |
| "Velocidade "                       | "Problem Statement"  |
"file:///path" |
-------------------------------------------------------------------------------
------------------------------------------------------
| title      | LearningResourceType | location       |
======================================================
| "Derivada" | "narrativetext"      | "file:///path" |
------------------------------------------------------
-------------------------------------------------------------
| title             | LearningResourceType | location       |
=============================================================
| "Função Contínua" | "narrativetext"      | "file:///path" |
-------------------------------------------------------------
-------------------------------------------------------
| title       | LearningResourceType | location       |
=======================================================
| "Sequência" | "narrativetext"      | "file:///path" |
-------------------------------------------------------
++++++++++++++++++++++++++++++++++++++

But I can't get it rendered at JSP. What am I doing wrong?


-- 
Herli Menezes
*Per Astra ad aspera.*

Re: Rendering SPARQL resultset in a JSP.

Posted by Herli Joaquim de Menezes <he...@gmail.com>.
Thank you, Andy.

As you can see, my idea was to pass the ResultSet as a session attribute
(request.getSession().setAttribute("results", results)) to be recovered and
passed to JSP.

I will see ResultFactory and devise a way to pass the ResultSet to JSP.

Thank you, once more.

Herli Menezes

2011/9/12 Andy Seaborne <an...@epimorphics.com>

>
>
> On 12/09/11 03:44, Herli Joaquim de Menezes wrote:
>
>> Hello, Friends
>>
>> I don't know how to solve this. I wrote a simple Jena web application in
>> order to query on a simple LOM RDF file. I am using a MVC model, so that
>> the
>> control view is composed by servlets. Excuse me for the long post...but
>> here
>> is the code snippet:
>>
>>
>> {..............}
>> String filename = "/WEB-INF/TesteLOM2.rdf";
>>         ServletContext context = getServletContext();
>>
>>         InputStream in = context.getResourceAsStream(**filename);
>>         if (in != null) {
>>             Model model = ModelFactory.**createMemModelMaker()
>>                     .createDefaultModel();
>>             model.read(in, null);
>>             // null base URI, since model URIs are absolute
>>             in.close();
>>
>>             @SuppressWarnings("unchecked")
>>             List<String>  lista = (List<String>) request.getSession()
>>                     .getAttribute("sugestao");
>>             String palavrachave = null;
>>             for (Iterator<String>  iter = lista.iterator();
>> iter.hasNext();)
>> {
>>
>>                 palavrachave = iter.next();
>>                 // Creates a query....
>>                 String queryString =
>>
>>                 "PREFIX NS1:<http://www.exemplo.com/#>  "
>>                         + "PREFIX rdf:<
>> http://www.w3.org/1999/02/22-**rdf-syntax-ns#<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
>> >"
>>                         + "PREFIX dcterms:<http://purl.org/dc/**terms/<http://purl.org/dc/terms/>
>> >"
>>                         + "PREFIX dc:<http://purl.org/dc/**elements/1.1/<http://purl.org/dc/elements/1.1/>
>> >"
>>                         + "PREFIX lom:<
>> http://ltsc.ieee.org/rdf/**lomv1p0/terms/<http://ltsc.ieee.org/rdf/lomv1p0/terms/>>
>>  "
>>                         + "PREFIX lomvoc:<
>> http://ltsc.ieee.org/rdf/**lomv1p0/vocabulary#<http://ltsc.ieee.org/rdf/lomv1p0/vocabulary#>
>> >"
>>                         + "SELECT  ?title ?LearningResourceType ?location
>>  "
>>                         + "WHERE {"
>>                         + "        ?keyword lom:keyword  \""
>>                         + palavrachave
>>                         + "\""
>>                         + "  ."
>>                         + "     ?keyword dc:title        ?title  ."
>>                         + "        ?keyword lom:location    ?location ."
>>                         + "        ?keyword lomvoc:LearningResourceType
>> ?LearningResourceType."
>>                         + "}";
>>
>>                 Query query = QueryFactory.create(**queryString);
>>
>>                 // get the results...
>>
>>                 QueryExecution qe = QueryExecutionFactory.create(**query,
>> model);
>>                 ResultSet results = qe.execSelect();
>>                 request.getSession().**setAttribute("results", results);
>>
>>                 // Just for debugging....
>>                 ResultSetFormatter.out(System.**out, results, query);
>>
>
> A ResultSet is an iterator.
>
> ResultSetFormatter.out(System.**out, results, query); reads the iterator
> so at the end, there are no more results. iterator.hasNext is false.
>
> If you want to print it out and pass it to ther code, you need to create a
> copy or rewindable ResultSet (see ResultSetFactory).
>
>
>                  qe.close();
>>
>>             }
>>             request.getAttribute("results"**);
>>
> ??
>
>
>> VIEW is a JSP to render the results:
>>
>> {...........}
>>
>> <form method="post" action="**ControlaBuscaDeObjetos">
>> <fieldset>
>> <legend>  Objetos Selecionados</legend>
>>
>> <p>  some explanation...</p>
>>
>> <table>
>>     <c:forEach items="${results}" var="item">
>>
>
> This iterator has ended.
>
>
>          <tr>
>>             <td>${item}</td>
>>
>
>              </tr>
>>     </c:forEach>
>> </table>
>>
>> </fieldset>
>>
>> </form>
>> ++++++++++++++++++++++++
>> The terminal output is OK:
>>
>
>        Andy
>
>
>
>> ------------------------------**------------------------------**
>> -------------
>> | title                         | LearningResourceType | location       |
>> ==============================**==============================**
>> =============
>> | "Força Resultante"            | "narrativetext"      | "file:///path" |
>> | "Calculo da Força Resultante" | "Problem Statement"  | "file:///path" |
>> ------------------------------**------------------------------**
>> -------------
>> ------------------------------**------------------------------
>> | title            | LearningResourceType | location       |
>> ==============================**==============================
>> | "Momento Linear" | "Problem Statement"  | "file:///path" |
>> ------------------------------**------------------------------
>> ------------------------------**------------------------------**
>> -------------------
>> | title                               | LearningResourceType |
>> location       |
>> ==============================**==============================**
>> ===================
>> | "Velocidade"                        | "narrativetext"      |
>> "file:///path" |
>> | "Velocidade no Movimento Harmônico" | "Problem Statement"  |
>> "file:///path" |
>> | "Velocidade "                       | "Problem Statement"  |
>> "file:///path" |
>> ------------------------------**------------------------------**
>> -------------------
>> ------------------------------**------------------------
>> | title      | LearningResourceType | location       |
>> ==============================**========================
>> | "Derivada" | "narrativetext"      | "file:///path" |
>> ------------------------------**------------------------
>> ------------------------------**------------------------------**-
>> | title             | LearningResourceType | location       |
>> ==============================**==============================**=
>> | "Função Contínua" | "narrativetext"      | "file:///path" |
>> ------------------------------**------------------------------**-
>> ------------------------------**-------------------------
>> | title       | LearningResourceType | location       |
>> ==============================**=========================
>> | "Sequência" | "narrativetext"      | "file:///path" |
>> ------------------------------**-------------------------
>> ++++++++++++++++++++++++++++++**++++++++
>>
>> But I can't get it rendered at JSP. What am I doing wrong?
>>
>>
>>


-- 
Herli Menezes
*Per Astra ad aspera.*

Re: Rendering SPARQL resultset in a JSP.

Posted by Andy Seaborne <an...@epimorphics.com>.

On 12/09/11 03:44, Herli Joaquim de Menezes wrote:
> Hello, Friends
>
> I don't know how to solve this. I wrote a simple Jena web application in
> order to query on a simple LOM RDF file. I am using a MVC model, so that the
> control view is composed by servlets. Excuse me for the long post...but here
> is the code snippet:
>
>
> {..............}
> String filename = "/WEB-INF/TesteLOM2.rdf";
>          ServletContext context = getServletContext();
>
>          InputStream in = context.getResourceAsStream(filename);
>          if (in != null) {
>              Model model = ModelFactory.createMemModelMaker()
>                      .createDefaultModel();
>              model.read(in, null);
>              // null base URI, since model URIs are absolute
>              in.close();
>
>              @SuppressWarnings("unchecked")
>              List<String>  lista = (List<String>) request.getSession()
>                      .getAttribute("sugestao");
>              String palavrachave = null;
>              for (Iterator<String>  iter = lista.iterator(); iter.hasNext();)
> {
>
>                  palavrachave = iter.next();
>                  // Creates a query....
>                  String queryString =
>
>                  "PREFIX NS1:<http://www.exemplo.com/#>  "
>                          + "PREFIX rdf:<
> http://www.w3.org/1999/02/22-rdf-syntax-ns#>"
>                          + "PREFIX dcterms:<http://purl.org/dc/terms/>"
>                          + "PREFIX dc:<http://purl.org/dc/elements/1.1/>"
>                          + "PREFIX lom:<
> http://ltsc.ieee.org/rdf/lomv1p0/terms/>  "
>                          + "PREFIX lomvoc:<
> http://ltsc.ieee.org/rdf/lomv1p0/vocabulary#>"
>                          + "SELECT  ?title ?LearningResourceType ?location  "
>                          + "WHERE {"
>                          + "        ?keyword lom:keyword  \""
>                          + palavrachave
>                          + "\""
>                          + "  ."
>                          + "     ?keyword dc:title        ?title  ."
>                          + "        ?keyword lom:location    ?location ."
>                          + "        ?keyword lomvoc:LearningResourceType
> ?LearningResourceType."
>                          + "}";
>
>                  Query query = QueryFactory.create(queryString);
>
>                  // get the results...
>
>                  QueryExecution qe = QueryExecutionFactory.create(query,
> model);
>                  ResultSet results = qe.execSelect();
>                  request.getSession().setAttribute("results", results);
>
>                  // Just for debugging....
>                  ResultSetFormatter.out(System.out, results, query);

A ResultSet is an iterator.

ResultSetFormatter.out(System.out, results, query); reads the iterator 
so at the end, there are no more results. iterator.hasNext is false.

If you want to print it out and pass it to ther code, you need to create 
a copy or rewindable ResultSet (see ResultSetFactory).

>                  qe.close();
>
>              }
>              request.getAttribute("results");
??
>
> VIEW is a JSP to render the results:
>
> {...........}
>
> <form method="post" action="ControlaBuscaDeObjetos">
> <fieldset>
> <legend>  Objetos Selecionados</legend>
>
> <p>  some explanation...</p>
>
> <table>
>      <c:forEach items="${results}" var="item">

This iterator has ended.

>          <tr>
>              <td>${item}</td>

>              </tr>
>      </c:forEach>
> </table>
>
> </fieldset>
>
> </form>
> ++++++++++++++++++++++++
> The terminal output is OK:

	Andy

>
> -------------------------------------------------------------------------
> | title                         | LearningResourceType | location       |
> =========================================================================
> | "Força Resultante"            | "narrativetext"      | "file:///path" |
> | "Calculo da Força Resultante" | "Problem Statement"  | "file:///path" |
> -------------------------------------------------------------------------
> ------------------------------------------------------------
> | title            | LearningResourceType | location       |
> ============================================================
> | "Momento Linear" | "Problem Statement"  | "file:///path" |
> ------------------------------------------------------------
> -------------------------------------------------------------------------------
> | title                               | LearningResourceType |
> location       |
> ===============================================================================
> | "Velocidade"                        | "narrativetext"      |
> "file:///path" |
> | "Velocidade no Movimento Harmônico" | "Problem Statement"  |
> "file:///path" |
> | "Velocidade "                       | "Problem Statement"  |
> "file:///path" |
> -------------------------------------------------------------------------------
> ------------------------------------------------------
> | title      | LearningResourceType | location       |
> ======================================================
> | "Derivada" | "narrativetext"      | "file:///path" |
> ------------------------------------------------------
> -------------------------------------------------------------
> | title             | LearningResourceType | location       |
> =============================================================
> | "Função Contínua" | "narrativetext"      | "file:///path" |
> -------------------------------------------------------------
> -------------------------------------------------------
> | title       | LearningResourceType | location       |
> =======================================================
> | "Sequência" | "narrativetext"      | "file:///path" |
> -------------------------------------------------------
> ++++++++++++++++++++++++++++++++++++++
>
> But I can't get it rendered at JSP. What am I doing wrong?
>
>