java - HttpServletRequest.getParameter 如何工作?

标签 java servlets

我在 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 中删除++ 吗?

最佳答案

+是 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/

相关文章:

java - java中的日期只有年份

java - 使用 javax.servlet.RequestDispatcher.include() 时如何将参数传递给 .jsp 文件?

java - 不同平台上的ZipOutputStream结果不同

java - 什么是java中的丢失信号?等待线程如何错过调用 notify()?

java - Android 椭圆形对话框

java - 使用 spring 将文件转换为 MultiPartFile

Java Servlet 使用 HTML 表单创建 MySQL DB 并对其进行排序

java - 在 JSP 中显示 Logo

Android Http 从 Servlet 获取字符串数据

java - 为什么使用 AspectJ 的 google 缓存很慢,而 SpringCaching 却更快