Java HttpServletRequest.getParameter是如何工作的?

Java HttpServletRequest.getParameter是如何工作的?,java,servlets,Java,Servlets,我在search.java中有一段代码,它使用getParameter和xmltransform。 我正在使用search.java根据给定的标题搜索图书数据库。对于所有标准,我都得到了输出。但是当我把C++作为标题的索引时,JSP将标题的值发送给Sqj.java。java使用getParameter方法接收输入。我的程序给我输出,但以C为标题 问题是getParameter被调用了两次。首先,当它被调用时,它得到C++,但是在XMListar之后,当我再次使用GETPARTION时,它得到了C

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

问题是getParameter被调用了两次。首先,当它被调用时,它得到C++,但是在XMListar之后,当我再次使用GETPARTION时,它得到了C。这就是为什么我只得到与标题C.< /P>有关的书籍。 代码如下

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. 结果如下

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. http://localhost:8086/SemanticWeb/ 在doget中搜索 课后 GETMODEL C++的S值 购买后 这是在XMListC++之前的标题 我在这里 这是xmlsourcehttp://localhost:8086/SemanticWeb/Search?xml=1&s=C++ 这是xmlsourcehttp://localhost:8086/SemanticWeb/xsl\data.xsl 检查 检查1 检查1 检查2 http://localhost:8086/SemanticWeb/ 在doget中搜索 课后 getparameter C获取的s值 购买后 这是XML1 在Search.java中搜索标题 这是标题C 在semantic Search.java中搜索标题 前缀kb: 选择*WHERE{x kb:Price?Price。?x kb:Currency?Currency。?x kb:Title?Title。?x kb:Count?Count。可选的{x kb:Description?Description}。过滤正则表达式(?Title,(^ |\\W)C(\\W |$))按标题排序 上述查询是由于semanticsearch.java文件引起的。 在语义搜索中完成 返回Search.java 检查3 在这里完成。
您能告诉我为什么两次调用getParameter时会从C中删除+?

您好,请尝试在参数值中使用引号吗


“1”&s='C++'

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

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

如果您希望以编程方式执行此操作,请在Java(Servlet/EL)端或JSP端使用。

您希望我需要在以下行中添加引号xmlTransform(contextPath+“Search?xml=1&s=“+title,contextPath+(access?”xsl\\data\u admin.xsl):“xsl\\data.xsl”)、response.getOutputStream();xmlTransform(contextPath+“Search?xml='1'&s=”+title+“”,contextPath+(access?“xsl\\data\u admin.xsl”:“xsl\\data.xsl”),response.getOutputStream();我已经改变了行添加报价如上所述。但是它进入了一个无限循环,我没有得到答案,我不小心没有向上滚动,而是点击了向下的投票。很抱歉,变量s填充在index.jsp中,index.jsp使用encodeURIComponent在index.jsp中编码,并传递到上面的Search.java文件。在这里,我们使用GETPARTIONER(),并获得输出文件中显示的值C++。同一个search.java文件还运行xmlTransform,它再次调用getParameter()并将值获取为C。为什么会发生这种情况?我该怎么做才能改变这个?