views:

468

answers:

2

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

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

<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

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

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

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?

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")

+1  A: 

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

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

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

Kevin Hakanson
A: 

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

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

objects
escape and unescape are deprecated - https://developer.mozilla.org/En/Core_JavaScript_1.5_Guide/Predefined_Functions/Escape_and_unescape_Functions
Kevin Hakanson
thanks for letting me know :)
objects