【问题标题】:request.getParameter() returns null on submit buttonrequest.getParameter() 在提交按钮上返回 null
【发布时间】:2019-03-01 19:55:34
【问题描述】:

在这里,我试图在单击 js 函数后单击“submitAction”按钮,调用 DoSubmit() 也调用了 servlet 的 doPost() 方法,但在 doPost() 方法中,request.getParameter("submitAction") 返回 null我期望它应该在哪里返回按钮值“验证 OTP”或“重新发送 OTP”,具体取决于单击的按钮。

以下是JSP代码:

    <html>
<body> 
<script>
   function DoSubmit()
        {
            var btn = document.getElementById("submitAction").value;
            alert("in side the DoSubmit method and OTP is  : "+myOTP+ " btn clicked: "+btn);

            $.ajax({
                type: "POST",
                url : 'myservlet',
                data : {
                    myOTP : myOTP
                },
                success : function(myOTP) {
                    $('#ajaxGetUserServletResponse').text(myOTP);
                }
            }); 
            return true;
        }
    </script>
    <form name="submitForm" action="/myservlet">
        <div id="dialog" title="Enter the OTP">
            <p id="message">Enter the One Time Password</p>
            <p id="note"> OTP code generated will be valid for 10 Mins<p>
                <input type="text" style="margin-left:10px"  valign="right" maxlength=4 size=4 id="myotp" title="OTP" name="myOTP">
                <input type="submit" name="submitAction" value="Verify OTP" onclick="DoSubmit()"/>
                <input type="submit" name="submitAction" value="Resend OTP"onclick="DoSubmit()"/>                   
                <button id="back">Back</button>         
            <div id="ajaxGetUserServletResponse" style="display: none;"></div>
        </div>
    </form>  
</body>
</html>

Servlet 类:

@WebServlet("/myservlet") public class GetUserServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String submitAction = request.getParameter("submitAction"); System.out.println(" "+submitAction); } }

【问题讨论】:

  • ` String submitAction = request.getParameter("submitAction");` 错误

标签: jsp servlets


【解决方案1】:

这是错误的:

  var btn = document.getElementById("submitAction").value;

具有“submitAction”类的按钮,没有 id 属性... 如果不传递事件,就不能传递这样的按钮的值。 你可以这样做:

将“this”添加到输入按钮的 onclick 功能中:

<input type="submit" name="submitAction" value="Verify OTP" onclick="DoSubmit(this)"/>

然后在你的函数中你可以得到这样的值:

function DoSubmit(e)
     {
         var btn = e.value;

(确保在函数的括号中添加“e”。)

现在您的示例的第二个问题是您正在使用表单但也发出 ajax 请求。您到底想做什么?

如果要发出 ajax 请求,请删除表单标签。因为每次单击按钮时都会提交表单,并且您不希望这种情况发生,因为它会改变您所在的页面。

您的 html 应如下所示:

<html>
<body>
<script>
function DoSubmit(e)
    {
        var btn = e.value;
        var myOTP = "123123"; //you don't define this anywhere in your example...
        alert("in side the DoSubmit method and OTP is  : "+myOTP+ " btn clicked: "+btn);


        var params = {
            myOTP : myOTP,
            submitAction : btn
        };

        $.post("myservlet", $.param(params), function(response) {
          //here you can handle the response from the servlet
          $('#ajaxGetUserServletResponse').text(myOTP);
        });
    }
</script>

    <div id="dialog" title="Enter the OTP">
        <p id="message">Enter the One Time Password</p>
        <p id="note"> OTP code generated will be valid for 10 Mins<p>
            <input type="text" style="margin-left:10px"  valign="right" maxlength=4 size=4 id="myotp" title="OTP" name="myOTP">
            <input type="submit" name="submitAction" value="Verify OTP" onclick="DoSubmit(this)"/>
            <input type="submit" name="submitAction" value="Resend OTP"onclick="DoSubmit(this)"/>
            <button id="back">Back</button>
        <div id="ajaxGetUserServletResponse" style="display: none;"></div>
    </div>

</body>
</html>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-10
    • 2014-09-21
    • 2021-01-27
    • 2018-11-13
    • 2012-05-04
    相关资源
    最近更新 更多