【问题标题】:How to pass hidden values using form in jsp如何在jsp中使用表单传递隐藏值
【发布时间】:2015-10-03 08:16:19
【问题描述】:

我的代码如下

<form method="post" action="Answer.jsp">
    <input type="submit" value="answer">
    <%String q_id=rs.getString("q_id"); %>
    <input type="hidden" value="<%out.print(q_id);%>"> 
</form> 

我想将q_id 传递到页面Answer.jsp 我得到了q_id 的值,但我不明白如何传递(或使用任何不同的方法)值?

【问题讨论】:

    标签: html forms jsp servlets


    【解决方案1】:

    在您需要的 JSP 表单中

    <form method="post" action="Answer.jsp">
        <input type="hidden" name="q_id" value="<%= rs.getString("q_id") %>">
        <input type="submit" value="Answer">
    </form>
    

    然后你可以在你的Answer.jsp中接收q_id

    <p>Question ID: <%= request.getParameter("q_id") %></p>
    

    或者,使用 JSP EL(推荐)

    <p>Question ID: ${param.q_id}</p>
    

    【讨论】:

      【解决方案2】:

      在您要隐藏值的页面上。

      <form method="post" action="Answer.jsp">
          <input type="hidden" id="q_id" name="q_idName" value="someValue">
      <input type="submit">   
      </form>
      

      然后在'Answer.jsp'页面可以通过调用隐式请求对象的getParameter(String name)方法获取值,如下:

      <% String q_id = request.getParameter("q_idName"); %>
       Value :  <%=q_id %>
      

      【讨论】:

      • 有什么例外?
      • 不,对不起,我忘记刷新页面了......但是在 Answer.jsp ,使用 request.getParameter("q_id");这将显示空值
      猜你喜欢
      • 1970-01-01
      • 2016-05-23
      • 1970-01-01
      • 2011-07-17
      • 2020-05-07
      • 2012-05-14
      • 2013-09-03
      • 1970-01-01
      • 2013-04-20
      相关资源
      最近更新 更多