javascript:无法将表单值传递给javascript并将其处理为httprequest

时间:2021-09-07 00:26:47

I have a java servlet (class) which populates form data.

我有一个java servlet(类),它填充表单数据。

  //encode string for special characters
  String encodedString = URLEncoder.encode(desc, "UTF-8");

  out_table.append("<form name=\"masinsert"+id+"\" method=\"GET\" action=\"MASInsert2\">\n");

    out_table.append("<input type=hidden id=\"partnumber"+id+"\" name=\"partnumber"+id+"\" value=\""+part+"\">\n");
                    out_table.append("<input type=hidden id=\"itemdescription"+id+"\" name=\"itemdescription"+id+"\" value=\""+encodedString+"\">\n");


  out_table.append("<tr><td colspan=2><input id=\"m"+id+"\" type=\"button\" onclick=\"masinsert('"+ id +"')\" value=\"Add\"></td></tr>");

which loops with a different id as you can see so a form would look like this

你可以看到哪个循环具有不同的id,所以表单看起来像这样

<form name="masinser12" method="GET" action="MASInsert2">
<input type=hidden id="partnumber12" name="partnumber12" value="9999">
<input type=hidden id="itemdescription12" name="itemdescription12" value="1. test description">
<input id="m12" type="button" onclick="masinsert('"+ id +"')" value="Add"> 

<form name="masinser13" method="GET" action="MASInsert2">
<input type=hidden id="partnumber13" name="partnumber13" value="2222">
<input type=hidden id="itemdescription13" name="itemdescription13" value="2. test description">
<input id="m12" type="button" onclick="masinsert('"+ id +"')" value="Add"> 

and they post to this javascript which is in the head of the html of the generated forms

并且他们发布到这个javascript,它位于生成的表单的html的头部

function masinsert(id)
{
 var currentTime=new Date();


 var button = document.getElementById("m"+id);
 button.onclick="";
 button.value="Inserting";

 var partnumber     = document.getElementById("partnumber"+id).value;
 var itemdescription    = document.getElementById("itemdescription"+id).value;


 function handleHttpResponse()
  {
   if (http.readyState == 4) {
    button.value="Item Added";
   }
  }


 var http = getHTTPObject(); // We create the HTTP Object
 var tempUrl = "\MASInsert2";
 tempUrl +=  "?partnumber="+partnumber+"&"+"itemdescription="+itemdescription+"&"+"itemshortdescription="+itemdescription.substring(0,37)
alert(tempUrl);
 http.open("GET", tempUrl, true);
 http.onreadystatechange = handleHttpResponse;
 http.send(null);   
    }

function getHTTPObject(){
 var xmlhttp;
 /*@cc_on
 @if (@_jscript_version >= 5)
   try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
   }catch (e){
    try {
     xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
     }catch (E) {
      xmlhttp = false;
      }
  }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
 }

and the javascript turns around and posts to this java file which inserts the data into the database with the snippet

并且javascript转过来并发布到这个java文件,该文件使用片段将数据插入到数据库中

String partnumber = request.getParameter( "partnumber");
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        String itemdescription = URLDecoder.decode(request.getParameter( "itemdescription"), "UTF-8");
        String itemshortdescription = request.getParameter( "itemshortdescription");
out.println(itemimport(partnumber,itemdescription, itemshortdescription));
} 

{ private String itemimport (String partnumber, String itemdescription, String itemshortdescription){ 
---
---
}

I decided to shorten the number of paramters for readibility. The main thing I'm having trouble with is how the URLDecoding and URLEncoding is transferring over correctly to be inserted into a Java PreparedStatement. I've narrowed it down the problem being somewhere in the javascript because when I created a simple html form with variables populated with the URLEncoded values, it inserts just fine. However when I run it through the javascript, I get a Null error

我决定缩短参数的数量以便于阅读。我遇到的主要问题是URLDecoding和URLEncoding如何正确传输以插入Java PreparedStatement。我已经缩小了javascript中某个地方的问题,因为当我创建一个带有URLEncoded值填充变量的简单html表单时,它插入就好了。但是,当我通过javascript运行它时,我得到一个Null错误

Is there a better way to do this or any ideas why the values are not making it through? Does javascript require some working around with when encoding strings to be Post'ed or Get?

有没有更好的方法来做这个或任何想法为什么价值观没有通过?当编码字符串为Post'ed或Get时,javascript是否需要一些解决方法?

thanks in advance.

提前致谢。

UPDATE: I just did a System.out.println on reach request.getParamter and the script stops at String itemdescription = URLDecoder.decode(request.getParameter( "itemdescription"), "UTF-8"); so it obviously has a problem with the URLDecoder.decode(request.getParameter("itemdescription"),"UTF-8")

更新:我只是在到达request.getParamter上做了一个System.out.println,脚本停在String itemdescription = URLDecoder.decode(request.getParameter(“itemdescription”),“UTF-8”);所以它显然有一个URLDecoder.decode(request.getParameter(“itemdescription”),“UTF-8”)的问题

2 个解决方案

#1


Here are some things to try: In your JavaScript, encode your URL paraemters with encodeURIComponent:

以下是一些尝试:在JavaScript中,使用encodeURIComponent对URL paraemters进行编码:

tempUrl +=  "?partnumber="+encodeURIComponent(partnumber)+"&"+"itemdescription="+encodeURIComponent(itemdescription)+"&"+"itemshortdescription="+encodeURIComponent(itemdescription.substring(0,37))

Next, you may have to set the encoding on the request using setCharacterEncoding

接下来,您可能必须使用setCharacterEncoding在请求上设置编码

Last, I think if you call request.getParameter, it does the URLDecoding for you.

最后,我想如果你调用request.getParameter,它会为你做URLDecoding。

#2


looks like you should be url encoding your request parameter values in your javascript

看起来你应该在你的javascript中对你的请求参数值进行url编码

tempUrl += "?partnumber="+partnumber+"&"+"itemdescription="+escape(itemdescription)+"&"+"itemshortdescription="+escape(itemdescription.substring(0,37))

tempUrl + =“?partnumber =”+ partnumber +“&”+“itemdescription =”+ escape(itemdescription)+“&”+“itemshortdescription =”+ escape(itemdescription.substring(0,37))

#1


Here are some things to try: In your JavaScript, encode your URL paraemters with encodeURIComponent:

以下是一些尝试:在JavaScript中,使用encodeURIComponent对URL paraemters进行编码:

tempUrl +=  "?partnumber="+encodeURIComponent(partnumber)+"&"+"itemdescription="+encodeURIComponent(itemdescription)+"&"+"itemshortdescription="+encodeURIComponent(itemdescription.substring(0,37))

Next, you may have to set the encoding on the request using setCharacterEncoding

接下来,您可能必须使用setCharacterEncoding在请求上设置编码

Last, I think if you call request.getParameter, it does the URLDecoding for you.

最后,我想如果你调用request.getParameter,它会为你做URLDecoding。

#2


looks like you should be url encoding your request parameter values in your javascript

看起来你应该在你的javascript中对你的请求参数值进行url编码

tempUrl += "?partnumber="+partnumber+"&"+"itemdescription="+escape(itemdescription)+"&"+"itemshortdescription="+escape(itemdescription.substring(0,37))

tempUrl + =“?partnumber =”+ partnumber +“&”+“itemdescription =”+ escape(itemdescription)+“&”+“itemshortdescription =”+ escape(itemdescription.substring(0,37))