如何从 Java servlet 访问 HTML 表单名称?

发布于 2024-12-26 03:53:39 字数 294 浏览 0 评论 0原文

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

喜欢, 表格 1:

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

表格 2:

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

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

I am facing an issue where a single java servlet needs to handle multiple HTML forms. So, I thought of using Form names in the HTML to pass it on to the server.

Like,
Form 1:

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

Form 2:

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

How can I access both these forms from a single servlet?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼泪淡了忧伤 2025-01-02 03:53:39

提交时,表单名称不会随 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");

Form name is not sent with the HTTP request on submit, thus it cannot be used on the server side.

Consider adding hidden field with the same name but different values in both forms:

<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>

And in your servlet:

request.getParameter("type");
初熏 2025-01-02 03:53:39

基础知识:

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

<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>

按“提交”按钮提交相应的表格。

Basics:

When we submit a form all the values of that form will only get
submitted.. So even if you are having two forms and you are submitting
one of them, then only parameters of submitted form will be available
to your controller.

<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>

Pressing Submit button with submit the respective form.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文