【问题标题】:how can i pass a value from jsp to servlet如何将值从 jsp 传递到 servlet
【发布时间】:2016-06-01 21:33:35
【问题描述】:

这是我的 jsp 程序的一部分。

<%@ page import="db.Conn" %>
<%@ page import="java.sql.*" %>

<%  try{
    String id2=request.getParameter("id");


    System.out.println(id2);



%>      
<input type="hidden" name="id2" id="id2" value="<%=id2%>" >

<%  }catch(Exception e)
    {System.err.println(e);}
%>

<form method="Post" action="./transaction">

    <button type="submit">Pay Now</button>

</form>
</span></div>

但是当我尝试使用以下语句在我的 servlet 中获取此 id2 时:

String id=request.getParameter("id2");

我收到id=null。 如何获得我的价值?

【问题讨论】:

    标签: jsp servlets


    【解决方案1】:

    存储id2input标签元素必须在表单元素内。

     <%@ page import="db.Conn" %>
        <%@ page import="java.sql.*" %>
    
     <form method="Post" action="./transaction">
    
        <%  try{
            String id2=request.getParameter("id");
            System.out.println(id2);
        %>      
        <input type="hidden" name="id2" id="id2" value="<%=id2%>" >
    
        <%  }catch(Exception e)
            {System.err.println(e);}
        %>
    
            <button type="submit">Pay Now</button>
    
        </form>
        </span></div>
    

    http://www.w3schools.com/html/html_forms.asp

    【讨论】:

      【解决方案2】:

      尝试使用 JSTL 核心库。将此添加到您的 JSP 声明中:

      <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
      

      那么你应该可以用这个替换你的 代码块:

      <c:set var="id2" value="${id}"/>
      

      然后,您可以将变量 id2 用于 JSP 中的任何内容,包括通过输入标签将其传递给 servlet。

      您阅读过 JSP 教程吗? http://docs.oracle.com/javaee/5/tutorial/doc/bnagx.html

      【讨论】:

      • 你能帮我写代码吗?我今天需要在我的大学提交。
      • uri:java.sun.com/jsp/jstl/core 无法在 web.xml 或随此应用程序部署的 jar 文件中解析。
      • @PulkitPansari 很高兴知道您关心大学工作,但学习最新的编码技术也很重要。由于 Scriplets 已过时,因此请快速上手 JSTL
      猜你喜欢
      • 2013-11-25
      • 2012-05-03
      • 1970-01-01
      • 2015-03-21
      • 2014-06-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多