Results 1 to 7 of 7

Thread: servlet and JS

  1. #1
    saqib15 is offline Member
    Join Date
    Feb 2010
    Posts
    11
    Rep Power
    0

    Default servlet and JS

    Hey Folks, can anyone tell me whats is wrong in my code, i am trying to get values from checkbox whenever they clicked but when i run my code nothing happen, response will be highly appriciated


    1.out.write("<FORM name=\"form1\"method=\"Post\" "+
    2. "onSubmit=\"return onFormSubmit();\"");///ws/ws_ext?servlet=GetUsersList&token="+token+"&users= form\">");
    3. out.write("<script type=\"text/javascript\">");
    4. out.write("function onFormSubmit(){");
    5. out.write("if(document.pressed==\"submit\"){");
    6. out.write("var queryString=\"/ws/ws_ext?servlet=GetUsersList&token="+token+";");
    7. //out.write("var chk=document.getElementByTagName(\"input\");");
    8. out.write("for(int i =0;i<document.form1.elements.length;i++){");
    9. out.write("if(document.form1.elements[i].type==\"checkbox\"){");
    10. out.write("if(document.form1.elements[i].checked==true){");
    11. out.write("queryString+=\"&user=\"+document.elemen ts[i].value;");
    12. out.write("alert(queryString)");
    13. out.write("}");
    14. out.write("}");
    15. out.write("}");
    16. out.write("document.form1.action=queryString;");
    17. out.write("document.form1.submit();");
    18. out.write("}");
    19. out.write("return queryString;");
    20. out.write("}");
    21. out.write("</script>");
    22.
    23. out.write("<tr>");
    24. for(int h =0; h<human.length; h++){
    25. out.write("<td></td><td bgcolor =#669933>"+human[h].getDisplayString()+"</td>");
    26. }
    27. out.write("</tr>");
    28.
    29. for (int i = 0; i < user.length; i++) {
    30. out.write("<tr>");
    31. out.write("<td bgcolor=#cecef6>"+role[i].getName()+"</td>");
    32. for(int h =0; h<human.length; h++){
    33.
    34. out.write("<td>");
    35. for (int j = 0; j < user[i].length; j++) {
    36. out.write("<input type=\"checkbox\" name=\"name"+user[i][j].getName() +"\""+" value=\""+user[i][j].getName()+"\"/>");
    37. out.write(user[i][j].getName());//this data to be with all tabs
    38. }
    39. out.write("</td><td></td>");
    40. }
    41.
    42. out.write("</tr>");
    43. }
    44. out.write("<input type=\"button\" value=\"Next &gt;&gt;\" onClick=\'document.pressed=this.value' value=\"submit\">");
    45. //out.write("</FORM>");
    46. out.write("</table>");
    47. //================================================== ======
    48.
    49. out.write("</FORM>");

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    878
    Rep Power
    11

    Default

    Hi,
    Why u have to frame the HTML like this? u can directly write as html right using jsp....Don't compicate urself...
    Ramya:cool:

  3. #3
    saqib15 is offline Member
    Join Date
    Feb 2010
    Posts
    11
    Rep Power
    0

    Default servlets JS

    yeah i have restriction i can not use JSp thats the main problem with me...

  4. #4
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    878
    Rep Power
    11

    Default

    1.You do one thing...U break as small small methods for simplicity and debugging sake.
    ex: form build separate meth,javascript separate method like this...

    2.Try to use StringBuffer and convert as String later....
    Ramya:cool:

  5. #5
    saqib15 is offline Member
    Join Date
    Feb 2010
    Posts
    11
    Rep Power
    0

    Default

    ::Hey , i have created method saperately can anyone check where is my mistake


    private StringBuffer addUsers (String token) {
    StringBuffer outstring = new StringBuffer("\r\n<script type=\"text/javascript\">");
    outstring.append("function onFormSubmit(){");
    outstring.append( "if(document.pressed==\"submit\"){");
    outstring.append( "var queryString=\"/ws/ws_ext?servlet=GetUsersList&token="+token+";");
    outstring.append( "for(int i =0;i<document.form1.elements.length;i++){");
    outstring.append( "if(document.form1[i].elements[i].type==\"checkbox\"){");
    outstring.append( "if(document.form1[i].elements[i].checked==true){");
    outstring.append( "queryString+=\"&user=\"+document.elements[i].value;");
    outstring.append( "alert(queryString)");
    outstring.append( "}");
    outstring.append( "}");
    outstring.append( "}");
    outstring.append( "document.form1.action=queryString;");
    outstring.append( "document.form1.submit();");
    outstring.append( "}");
    outstring.append( "return queryString;");
    outstring.append( "}");
    outstring.append("</script>");
    return outstring;
    }

  6. #6
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    878
    Rep Power
    11

    Default

    In the 5th line u have missed double quotes.correct like this below.
    outstring.append( "var queryString=\"/ws/ws_ext?servlet=GetUsersList&token="+token+";\"");
    Ramya:cool:

  7. #7
    saqib15 is offline Member
    Join Date
    Feb 2010
    Posts
    11
    Rep Power
    0

    Default

    Thank you Yes thats worked and its working now, really appriciate.

Similar Threads

  1. how can i link html+servlet+xml to form servlet
    By balachandarr in forum Java Servlet
    Replies: 0
    Last Post: 04-15-2009, 04:06 PM
  2. Servlet to Servlet connection through URLConnection
    By bsrjavaforum in forum Networking
    Replies: 0
    Last Post: 04-01-2009, 04:12 PM
  3. Replies: 1
    Last Post: 12-02-2008, 12:23 AM
  4. Replies: 0
    Last Post: 08-30-2008, 11:52 AM
  5. Replies: 1
    Last Post: 08-07-2007, 03:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •