HttpServletRequest.getParameter如何工作?

时间:2011-03-30 06:02:44

标签: java servlets

我在search.java中有一个使用getParameter和xmltransform的代码。 我使用search.java根据给定的标题搜索书籍数据库。对于所有标准,我得到了输出。但是当我将C ++作为index.jsp中的标题时,它将标题的值发送给search.java。 search.java使用getParameter方法接收输入。我的程序给了我输出但是把C作为标题。

问题是getParameter被调用两次。首先,当它被调用时,它会得到C ++,但是当我再次使用getParameter进行xmltransform后,它会得到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中删除++吗?

2 个答案:

答案 0 :(得分:1)

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

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

这样它最终将成为C,之后有两个空格。您需要URL-encode参数。 +的网址编码值为%21

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

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

答案 1 :(得分:0)

您好,请参考参数值

中的引用

http://localhost:8086/SemanticWeb/Search?xml= '1' &安培; S = 'C ++'