request.getParameter 值无法从用户键入的值中获取带空格的数据

request.getParameter value not get data with spaces from value that user key in

我刚刚学习了 jsp 和 Java,而且还是新手。 我的 a.jsp file.The 中有此代码,用户需要在值中输入任何项目,然后单击搜索按钮。

<input type="text" value="" name="itemCode" id="itemCode">;
<input type="button" name="btnitemsearch" value="Search" onclick="searchItem();"> 

然后在函数searchItem()中,

var code1 = document.getElementById("itemCode").value;
var getData = '../select_Item/b.jsp?itemCode=' + code1;

$(document).ready (function(){
     $("#openModalDialog").dialog({
         modal: true,
         autoOpen: false,
         title: "Item Search",
         width: 700,
         height: 400,
         });
     });

     $('#openModalDialog').dialog('open');
      $('#openModalDialog').load(getData);

所以在 b.jsp 文件中,我使用请求获取参数来获取值,但它仅在值没有 space.

<% String itemCode= request.getParameter("itemCode");
   System.out.println(itemCode);%>

例如,如果我输入值铅笔,它当然会打印出来 "pencil"。 但是如果我输入值铅笔 2b 如果只打印出 "pencil" 而没有 space " " 和 2b.

我知道有人问过这个问题,duplicate.But 在大多数问题中,值使用 request.getParameter.It 表示我需要用引号将属性值括起来。

value="<%=request.getParameter("anything")%>"

但我的问题是 user.How 是否必须输入该值才能使其工作?谢谢。

替换:

var getData = '../select_Item/b.jsp?itemCode=' + code1;

来自

var getData = '../select_Item/b.jsp?itemCode=' + encodeURIComponent(code1);

您需要对查询字符串进行编码以形成正确的 URL 字符串。

在这种情况下:pencil 2b 将变为 pencil%202b,并且 space 字符在您的 b.jsp

中访问时被正确保留