问题描述
我在search.java中有一个使用getParameter和xmltransform的代码。 我正在使用search.java根据给定的标题搜索书籍数据库。 对于所有条件,我都得到输出。 但是当我在index.jsp中将C ++作为标题时,会将标题的值发送到search.java。 search.java使用getParameter方法接收输入。 我的程序给我输出,但以C为标题。
问题是getParameter被调用了两次。 首先,它被称为C ++,但是在xmltransform之后,当我再次使用getParameter时,它也变为C。这就是为什么我只得到与标题C有关的书籍的原因。
代码如下
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
Boolean access = (Boolean) request.getSession().getAttribute("access");
if (access == null)
access = false;
if (contextPath == null)
//contextPath = HOST + getServletContext().getContextPath() + "\\";
//changed the above line to get http://localhost:8085/SemanticWeb
contextPath = HOST + getServletContext().getContextPath() + "/";
System.out.println(contextPath);
System.out.println("Search in doget");
SemanticSearch semsearch = new SemanticSearch(request.getSession());
System.out.println("After getsession");
semsearch.loadData(REALPATH + RDFDATASOURCEFILE);
String xml = request.getParameter("xml");
String title = request.getParameter("s");
/*Writer writer = response.getWriter();
writer.write(request.getParameter("s"));*/
System.out.println("the value of s taken by getparameter " +title);
System.out.println("After ASOURCEFILE");
// Ask just for XML Response
if (xml != null && xml.equals("1") && title != null) {
System.out.println("this is xml "+xml);
//response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=utf-8");
System.out.println("Search for Title in search.java");
System.out.println("This is the title "+title);
semsearch.searchForTitleXML(response.getOutputStream(), null, title);
System.out.println("Back in Search.java");
return;
}
System.out.println("This is the title just before xmltransform "+title);
xmlTransform(contextPath + "Search?xml=1&s=" + title, contextPath
+ (access ? "xsl\\data_admin.xsl" : "xsl\\data.xsl"), response.getOutputStream());
//System.out.println(xmlResponse);
System.out.println("Done in Search.java");
}
public static void xmlTransform(String xml_uri, String xsl_uri,
OutputStream out) {
// JAXP reads Data trough Source-Interface
Source xmlSource = new StreamSource(xml_uri);
Source xsltSource = new StreamSource(xsl_uri);
System.out.println("I am here");
System.out.println("This is the xmlsource "+xml_uri);
System.out.println("This is the xmlsource "+xsl_uri);
// Factory-Pattern supports different XSLT-Processors
TransformerFactory transFact = TransformerFactory.newInstance();
System.out.println("checking");
Transformer trans;
System.out.println("checking_1");
try {
System.out.println("checking_1");
trans = transFact.newTransformer(xsltSource);
System.out.println("checking_2");
trans.transform(xmlSource, new StreamResult(out));
System.out.println("checking_3");
System.out.println("Done here");
} catch (TransformerConfigurationException e) {
//System.out.println("After error in xmltransfrom");
System.out.println("Caught here");
e.printStackTrace();
} catch (TransformerException e) {
System.out.println("Here I am there");
e.printStackTrace();
}
}
}
输出如下
http://localhost:8086/SemanticWeb/ Search in doget After getsession the value of s taken by getparameter C++ After ASOURCEFILE This is the title just before xmltransform C++ I am here This is the xmlsource http://localhost:8086/SemanticWeb/Search?xml=1&s=C++ This is the xmlsource http://localhost:8086/SemanticWeb/xsl\data.xsl checking checking_1 checking_1 checking_2 http://localhost:8086/SemanticWeb/ Search in doget After getsession the value of s taken by getparameter C After ASOURCEFILE this is xml 1 Search for Title in search.java This is the title C Search for title in semantic search.java PREFIX kb: SELECT * WHERE {?x kb:Price ?price. ?x kb:Currency ?currency. ?x kb:Title ?title. ?x kb:Count ?count. OPTIONAL {?x kb:Description ?description}.FILTER regex(?title, "(^|\\W)C (\\W|$)")}ORDER BY ?title The above query is due to semanticsearch.java file. Done in SemanticSearch Back in Search.java checking_3 Done here.
您能告诉我为什么两次调用getParameter会从C中擦除++吗?
1楼
+
是URL中的特殊字符,表示空格。
看起来您是在手动设置参数,而不是让浏览器对其进行设置,如下所示:
<a href="Search?xml=1&s=C++">Search C++</a>
这样,它将最终以带有两个空格的C
结束。
您需要对该参数进行 。
URL编码的+
值为%21
。
<a href="Search?xml=1&s=C%21%21">Search C++</a>
如果要以编程方式执行此操作,请在Java(Servlet / EL)端使用或在JSP端使用 。
2楼
您好,能否请您在参数值中加引号
'1'&s ='C ++'