AJAX请求中的URL参数 - 在servlet中不可用

时间:2015-01-25 13:44:00

标签: javascript ajax jsp servlets

我正在尝试使用ajax调用将参数从jsp传递到servlet。但是我没有通过参数。当我在servlet上打印它们时,它们只是null。我没有看到我在哪里犯错误。需要帮助

JSP:

<body>

<%
    System.out.println("In Add-Edit Questions.jsp");
    String Title=(String)request.getAttribute("Title");
    System.out.println("Title:"+Title);

    String Author=(String)request.getAttribute("Author");
    System.out.println("Author:"+Author);

    String currentq=(String)request.getAttribute("currentq").toString();
    System.out.println("currentq:"+currentq);
    if(currentq!=null){int curq=Integer.parseInt(currentq);}

    int numq=0;
    String noq=(String)request.getAttribute("noq").toString();
    System.out.println("noq:"+noq);
    if(noq!=null){numq=Integer.parseInt(noq);}

%>
    <script type="text/javascript">
        var T;
        var A;

        function populatereq()
        {
            T=document.getElementById("Title").value;
            A=document.getElementById("Author").value;
        }

        function loadquestion(clicked)
        {
            populatereq();
            var xmlhttp;
            if (window.XMLHttpRequest)
            {
              xmlhttp=new XMLHttpRequest();
            }
            else
            {
              xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange=function()
            {
              if (xmlhttp.readyState==4 && xmlhttp.status==200)
              {

                //document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
                //Populate fields;
                var resT=xmlhttp.responseText;
                alert(resT);
                console.log(resT);
                var array=resT.split("`");
                document.getElementById("question").value=array[2];
                document.getElementById("nq").value=array[3];
                choice();
                document.getElementById("desc").value=array[Number(array[3])+5];
                document.getElementById("currentq").value=array[Number(array[3])+6];
                for(k=1;k<Number(array[3]);k++)
                {
                    document.getElementById("Choice"+k).value=array[3+k];
                }
                document.getElementById("A"+4+Number(array[3])).selected=true;
              }
            }
            alert("Title="+T+"&Author="+A+"&loadq="+clicked);
            xmlhttp.open("POST","Add_Edit_Questions",true);
            xmlhttp.send("Title="+T+"&Author="+A+"&loadq="+clicked);
        }

    </script>


    <div class="left-panel">
       <b>Questions</b><br>
       <form name="leftform" action="Add_Edit_Questions" method="get" id="add_edit_question_left">

       <%for(int i=1;i<=numq;i++)
         {%>

                <br>
            <a href="Add_Edit_Questions?Title=<%=Title %>&Author=<%=Author%>&loadq=<%=i %>"><button ><%="Question "+i %></button></a>
                <br>
        <%}%>               
        </form>
        <a href="Add_Edit_Questions?Title=<%=Title %>&Author=<%=Author%>&loadq=new"><button>ADD</button></a>
    </div>

    <script type="text/javascript">
    var number;
    var i;
        function choice()
        {
            var container=document.getElementById("choices");
            while (container.hasChildNodes())
            {
             container.removeChild(container.lastChild);
            }
            number=document.getElementById("nq").value;

            for (i=0;i<number;i++)
            {
                // Append a node with a random text
                container.appendChild(document.createTextNode("Choice " + (i+1) +" : "));
                // Create an <input> element, set its type and name attributes
                var input = document.createElement("input");
                input.type = "text";
                input.name = "Choice" + (i+1);
                input.id = "Choice" + (i+1);
                input.required=true;
                container.appendChild(input);
                // Append a line break 
                container.appendChild(document.createElement("br"));

            }
            answerselect();
        }

        function answerselect()
        {

            var answerbox=document.getElementById("answer");
            while (answerbox.hasChildNodes())
            {
                answerbox.removeChild(answerbox.lastChild);
            }
            answerbox.appendChild(document.createTextNode("Choose Answer :  "));
            var select = document.createElement("select");
            select.name = "Answer";
            for (i=0;i<number;i++)
            {
                var option = document.createElement("option");
                option.id="A"+(i+1);
                option.value=(i+1);
                option.text = (i+1);
                select.add(option,null);
            }
            answerbox.appendChild(select);
            answerbox.appendChild(document.createElement("br"));
            document.getElementById("descr").style.visibility="visible";
        }


    </script>
    <div class="right-panel">
        <b>Add/Edit Question</b><br>

        <form name="myform" action="Add_Edit_Questions" method="get" id="add_edit_question">

            <label>Question : </label><textarea rows="2" cols="50" name="question" form="add_edit_question"></textarea>
            <!-- input type="text" name="question" id="question" placeholder="Enter your question here..." -->
            <br>
            <label>Select number of options : </label>
            <select id="nq" onchange="choice()" name="nq">
                <%for(int i=1;i<=10;i++){ %>
                    <option id=<%="O"+i %> value="<%=i %>"><%=i %>  </option>
                <%} %>  
            </select><br>
            <div id="choices"></div>

            <div id="answer"></div>

            <div id="descr" style="visibility : hidden">
                <label id="desclabel">Add Description : </label>
                <textarea rows="5" cols="50" id="desc" name="desc" form="add_edit_question"></textarea>
            </div>
            <input type="hidden" name="attribute" id="atri"><!-- To tell whether to download QP or edit QP. -->
            <input type="hidden" name="Title" id="Title" value=<%=Title %> >
            <input type="hidden" name="Author" id="Author" value="<%=Author %>" >
            <input type="hidden" name="currentq" id="currentq" value="<%=currentq %>">
            <input type="submit" value="ADD TO QP" onclick="document.getElementById('atri').value='add2qp'">
            <input type="submit" value="CREATE QP" onclick="document.getElementById('atri').value='download'">

        </form>
    </div>

</body>

问题仅出在AJAX上。

SERVLET:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
{
    System.out.println("In Add_Edit .java");
    String Title=request.getParameter("Title");
    System.out.println("title="+Title);//This is printing 'null' when ajax call is made
    String Author=request.getParameter("Author");
    System.out.println("author="+Author);//This is printing 'null' when ajax call is made
    String loadq=request.getParameter("loadq");
    System.out.println("loadq="+loadq);//This is printing 'null' when ajax call is made

    /*
        Here I will process request and write back required response text.
    */



    }

}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
    doGet(request,response);
}

}

0 个答案:

没有答案