我如何从Java servlet访问HTML表单名称? [英] How can I access the HTML Form Name from the Java servlet?

查看:109
本文介绍了我如何从Java servlet访问HTML表单名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临的问题是单个java servlet需要处理多个HTML表单。所以,我想在HTML中使用表单名称将它传递给服务器。



赞,
表单1:

  form method =POSTaction ='Controller'name =edit1

表格2:

  form method =POSTaction ='Controller'如何从单个servlet访问这两个表单?

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

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


解决方案

表单名称在发送HTTP请求时不会发送,因此无法在服务器端使用。



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

 < form method =POSTaction ='Controller'> 
< input type =hiddenname =typevalue =form1/>
<! - ... - >
< / form>

< form method =POSTaction ='Controller'>
< input type =hiddenname =typevalue =form2/>
<! - ... - >
< / form>

而在您的servlet中:

 的request.getParameter( 类型); 


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?

解决方案

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");

这篇关于我如何从Java servlet访问HTML表单名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆