servlets:在servlet中通过ajax获取null参数

时间:2013-05-04 11:45:29

标签: java javascript ajax jsp servlets

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();  

当我在servlet中获取参数时,我将我的值设为'null'

我做错了什么。

这是我设置参数的方式......

使用脚本标记

从jsp页面
 <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>

这是我的形式.....

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

2 个答案:

答案 0 :(得分:1)

您正在获取带有ID的参数,您应该给出姓名。

例如

String isno1 = request.getParameter(“isbn”); //这里是isbn是id

你应该写

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



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

以及

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


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

两个输入具有相同的**name**,请检查

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

答案 1 :(得分:0)

使用以下ajax send()发送数据

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 
            }

调用httpob.send(),传入将要发送的参数(不带“?”前缀)。