【问题标题】:How can I access the HTML Form Name from the Java servlet?如何从 Java servlet 访问 HTML 表单名称?
【发布时间】:2012-01-10 17:43:29
【问题描述】:

我面临一个问题,即单个 java servlet 需要处理多个 HTML 表单。因此,我想到了在 HTML 中使用表单名称将其传递给服务器。

喜欢, 表格一:

form method="POST" action='Controller' name="edit1"

表格2:

form method="POST" action='Controller' name="edit2"

如何从一个 servlet 访问这两种表单?

【问题讨论】:

    标签: html servlets


    【解决方案1】:

    表单名称在提交时没有随 HTTP 请求一起发送,因此不能在服务器端使用。

    考虑在两种形式中添加名称相同但值不同的隐藏字段:

    <form method="POST" action='Controller'>
      <input type="hidden" name="type" value="form1" />
      <!-- ... -->
    </form>
    
    <form method="POST" action='Controller'>
      <input type="hidden" name="type" value="form2" />
      <!-- ... -->
    </form>
    

    在你的 servlet 中:

    request.getParameter("type");
    

    【讨论】:

    • @user757021:很高兴我能帮上忙。如果您仍然发现一些问题,请接受答案或提供更多详细信息。
    【解决方案2】:

    基础知识:

    当我们提交一个表单时,该表单的所有值只会得到 提交..所以即使你有两个表格并且你正在提交 其中之一,则只有提交表单的参数可用 到你的控制器。

    <form method="POST" action='Controller' name="form1">
      <input type="text" name="type" value="form1Text" />
      <input type="text2" name="type" value="form1Text2" />
      <input type="submit"/>
      <!-- ... -->
    </form>
    
    <form method="POST" action='Controller' name="form2">
      <input type="text" name="type" value="form2Text" />
      <input type="text2" name="type" value="form2Text2" />
      <input type="submit"/>
      <!-- ... -->
    </form>
    

    按下提交按钮并提交相应的表单。

    【讨论】:

      猜你喜欢
      • 2010-10-25
      • 2017-04-02
      • 2011-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-24
      • 2016-03-16
      相关资源
      最近更新 更多