-2
String isno1=request.getParameter("isbn");
        String bktitle2=request.getParameter("booktitle");
        String authr3=(String) request.getParameter("author");
        System.out.println(isno1+bktitle2+authr3);
        Enumeration paramaterNames = request.getParameterNames();  

when i am taking the parameters in servlet then i am gettin my values as 'null'

what wrong am i doing.

this is the way i am setting the parameters...

from a jsp page using script tag

 <script type="text/javascript">
            function getHTTPObject()
            {
                var httpobject;
                if(!httpobject && typeof(XMLHttpRequest) != 'undefined')
                    {
                    try{
                        httpobject=new XMLHttpRequest();
                    }
                    catch(e){
                        xmlhttp=false;
                    }
                    }
                return httpobject; 
            }


            var httpob=getHTTPObject();


            function handleHttpResponse(){
                if(httpob.readyState==4){
                    //alert("sd");
                    var result=httpob.responseText;
                    alert(result);
                    /* document.write("hi your book is submitted !!!!!"); */
                }
            }
            function auth(){
                var params="isbn="+document.mayurform.isbn.value+"&booktitle="+document.mayurform.booktitle.value+"&author="+document.mayurform.author.value;
                alert("params sending"+params);
                httpob.open("POST","addbook",true);

                httpob.setRequestHeader("Content-type","application/x-www-form-urlencoded");
                httpob.setRequestHeader("Content-length",params.length);
                httpob.setRequestHeader("Connection","close");
                /* httpob.setRequestHeader("Content-type","application/x-www-from-urlencoded");
                httpob.setRequestHeader("Content-length",params.length);
                httpob.setRequestHeader("Connection","close"); */
                httpob.onreadystatechange=handleHttpResponse;
                httpob.send();
            }


        </script>

and this my form.....

<form style="margin: 100px;"   name="mayurform">
    <table align="center">
        <tr>
        <td align="center">ISBN NO.</td>
        <td><input align="middle" type="text" size="20" name="id" id="isbn">
        </tr>
        <tr>
        <td align="center">Book-Title</td>
        <td><input align="middle" type="text" size="20" name="pwd" id="booktitle">
        </td>
        </tr>
        <tr>
        <td align="center">Author</td>
        <td><input align="middle" type="text" size="20" name="pwd" id="author">
        </tr>
        <tr>
        <td><input align="middle" type="button" size="20" name="Add-Book" onclick="auth()">
        </tr>
    </table>

</form>
4

2 回答 2

1

You are fetching parameters with ID's you should give Names.

for example

String isno1=request.getParameter("isbn"); //here is isbn is id

you should write

<input align="middle" type="text" size="20" name="id" id="isbn">



String isno1=request.getParameter("id");-----------^

and also

 <td><input align="middle" type="text" size="20" name="pwd" id="booktitle">


    <td><input align="middle" type="text" size="20" name="pwd" id="author">

both inputs have the same **name** please check it

http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-Form-Data.html

于 2013-05-04T11:47:10.637 回答
0

Use like below ajax send() to send data

function auth(){
                var params="isbn="+document.mayurform.isbn.value+"&booktitle="+document.mayurform.booktitle.value+"&author="+document.mayurform.author.value;
                alert("params sending"+params);
                .......
                ...
                httpob.send(params);//change is here 
            }

call httpob.send(),passing in the parameters that will be sent (without the "?" prefix).

于 2013-05-06T11:39:52.190 回答