Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's some example code. Note that I wouldn't actually do it this way for real: the presentation code and the back-end query handling are all mixed up in one class. Real apps don't do this! The example is faithful to your sample, except that rather than string-bash the query, I use Jena's facility to pre-bind some of the query variables (so <code>DIR1-NAME</code> becomes <code>?dir-1-name</code>). I think this is much cleaner, and gives you more flexibility as to which of the query parameters are passed in from the client.</p> <pre><code>package example; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.*; import com.hp.hpl.jena.query.*; import com.hp.hpl.jena.rdf.model.*; public class ServletExample extends HttpServlet { /***********************************/ /* Constants */ /***********************************/ private static final long serialVersionUID = 1L; public static final String SPARQL_ENDPOINT = "http://data.linkedmdb.org/sparql"; public static final String QUERY = "PREFIX m: &lt;http://data.linkedmdb.org/resource/movie/&gt;\n" + "SELECT DISTINCT ?actorName WHERE {\n" + " ?dir1 m:director_name %dir_name_1%.\n" + " ?dir2 m:director_name %dir_name_2%.\n" + " ?dir1film m:director ?dir1;\n" + " m:actor ?actor.\n" + " ?dir2film m:director ?dir2;\n" + " m:actor ?actor.\n" + " ?actor m:actor_name ?actorName.\n" + "}\n" + ""; private static final String HEADER = "&lt;html&gt;\n" + " &lt;head&gt;\n" + " &lt;title&gt;results&lt;/title&gt;\n" + " &lt;link href=\"simple.css\" type=\"text/css\" rel=\"stylesheet\" /&gt;\n" + " &lt;/head&gt;\n" + " &lt;body&gt;\n" + ""; private static final String FOOTER = "&lt;/body&gt;&lt;/html&gt;"; /** * Respond to HTTP GET request. Will need to be mounted against some URL * pattern in web.xml */ @Override protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException { String dir1 = req.getParameter( "dir1" ); String dir2 = req.getParameter( "dir2" ); if (dir1 == null || dir2 == null || dir1.isEmpty() || dir2.isEmpty()) { noInput( resp ); } else { runQuery( resp, dir1, dir2 ); } } protected void noInput( HttpServletResponse resp ) throws IOException { header( resp ); resp.getWriter().println( "&lt;p&gt;Please select director names as query params &lt;code&gt;dir1&lt;/code&gt; and &lt;code&gt;dir2&lt;/code&gt;&lt;/p&gt;" ); footer( resp ); } protected void footer( HttpServletResponse resp ) throws IOException { resp.getWriter().println( FOOTER ); } protected void header( HttpServletResponse resp ) throws IOException { resp.getWriter().println( HEADER ); } protected void runQuery( HttpServletResponse resp, String dir1, String dir2 ) throws IOException { PrintWriter out = resp.getWriter(); // Set up the query String q = QUERY.replace( "%dir_name_1%", "\"" + dir1 + "\"" ) .replace( "%dir_name_2%", "\"" + dir2 + "\"" ); Query query = QueryFactory.create( q ) ; QueryExecution qexec = QueryExecutionFactory.sparqlService( SPARQL_ENDPOINT, query ); // perform the query ResultSet results = qexec.execSelect(); // generate the output header( resp ); if (!results.hasNext()) { out.println( "&lt;p&gt;No results, sorry.&lt;/p&gt;" ); } else { out.println( "&lt;h1&gt;Results&lt;/h1&gt;" ); while (results.hasNext()) { QuerySolution qs = results.next(); String actorName = qs.getLiteral( "actorName" ).getLexicalForm(); out.println( String.format( "&lt;div&gt;Actor named: %s&lt;/div&gt;", actorName ) ); } } footer( resp ); } } </code></pre> <p>This code works as far as I can tell, but I don't know which queries you're using that should return some results.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload