Search code examples
javanlpsparqlrdfjena

Using a variable declared outside the Query string inside a SPARQL Query- Using JSP and Java Servlet


I have 2 jsp pages. In the index page I am selecting a dropdown value and passing the selected value as a url parameter to the SearchResults page.

http://localhost:8080/SearchResults.jsp?jobCat=InformationTechnology

I am capturing the parameter InformationTechnology passed using below code . The query should show results for the selected value in the form. (I am selecting a job category and the results should show the candidates under selected job category)

String jobCategory = request.getParameter("jobCat");

Below is my Query

String queryString =
"Select ?a ?b"+
" Where { ...."+
" Filter (?a = "+ jobCategory  +")"+// jobCategory is the String variable 
" }";

But I am getting ParseException Error.

com.hp.hpl.jena.query.QueryParseException: Encountered " "in" "In "" at line 
1, column 559.
Was expecting one of:
<IRIref> ...
<PNAME_NS> ...
<PNAME_LN> ...
<VAR1> ...
<VAR2> ...
"exists" ...
"not" ...
"count" ...
"min" ...
"max" ...
"sum" ...
"avg" ...
"sample" ...
"group_concat" ...
"bound" ...
"coalesce" ...
"if" ...
"bnode" ...
"iri" ...
"uri" ...
"str" ...
"strlang" ...
"strdt" ...
"datatype" ...
"lang" ...
"langmatches" ...
"isURI" ...
"isIRI" ...
"isBlank" ...
"isLiteral" ...
"isNumeric" ...
"regex" ...
"sameTerm" ...
"RAND" ...
"ABS" ...
"CEIL" ...
"FLOOR" ...
"ROUND" ...
"CONCAT" ...
"SUBSTR" ...
"STRLEN" ...
"UCASE" ...
"LCASE" ...
"ENCODE_FOR_URI" ...
"CONTAINS" ...
"strSTARTS" ...
"strENDS" ...
"YEAR" ...
"MONTH" ...
"DAY" ...
"HOURS" ...
"MINUTES" ...
"SECONDS" ...
"TIMEZONE" ...
"TZ" ...
"NOW" ...
"MD5" ...
"SHA1" ...
"SHA224" ...
"SHA256" ...
"SHA384" ...
"SHA512" ...
"true" ...
"false" ...
<INTEGER> ...
<DECIMAL> ...
<DOUBLE> ...
<INTEGER_POSITIVE> ...
<DECIMAL_POSITIVE> ...
<DOUBLE_POSITIVE> ...
<INTEGER_NEGATIVE> ...
<DECIMAL_NEGATIVE> ...
<DOUBLE_NEGATIVE> ...
<STRING_LITERAL1> ...
<STRING_LITERAL2> ...
<STRING_LITERAL_LONG1> ...
<STRING_LITERAL_LONG2> ...
"(" ...
"!" ...
"+" ...
"-" ...

Is there an alternative way to use the string variable inside a SPARQL query like in similar scenario. Thanks in advance


Solution

  • I have a solution for this issue. I used regex to obtain the result

    "filter(regex(?a, \"" + jobCategory + "\"))"