gpt4 book ai didi

java - HttpServletRequest.getParameter 如何工作?

转载 作者:行者123 更新时间:2023-12-01 15:53:45 26 4
gpt4 key购买 nike

我在 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 dogetAfter getsessionthe value of s taken by getparameter C++After ASOURCEFILEThis is the title just before xmltransform C++I am hereThis is the xmlsource http://localhost:8086/SemanticWeb/Search?xml=1&s=C++This is the xmlsource http://localhost:8086/SemanticWeb/xsl\data.xslcheckingchecking_1checking_1checking_2http://localhost:8086/SemanticWeb/Search in dogetAfter getsessionthe value of s taken by getparameter C  After ASOURCEFILEthis is xml 1Search for Title in search.javaThis is the title C  Search for title in semantic search.javaPREFIX 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 ?titleThe above query is due to semanticsearch.java file.Done in SemanticSearchBack in Search.javachecking_3Done here.

你能告诉我为什么调用 getParameter 两次时会从 C 中删除++ 吗?

最佳答案

+是 URL 中代表空格的特殊字符。看起来您是手动设置参数,而不是让浏览器设置它,如下所示:

<a href="Search?xml=1&s=C++">Search C++</a>

这样它最终会变成 C其后有两个空格。您需要URL-encode参数。 + 的 URL 编码值是 %21 .

<a href="Search?xml=1&s=C%21%21">Search C++</a>

如果您想以编程方式执行此操作,请使用 URLEncoder#encode() 在 Java (Servlet/EL) 端或 <c:url> 在JSP端。

关于java - HttpServletRequest.getParameter 如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5482480/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com