如何在JSP文件中提交HTML表单的值并将其发送到Servlet? [英] How to submit and send values of a HTML form in JSP file to a Servlet?

查看:410
本文介绍了如何在JSP文件中提交HTML表单的值并将其发送到Servlet?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java的HTTP Servlet中,我想提交一个表单.推荐的方法是什么 输入字段中的值的方式?是否使用隐藏字段 并通过request.getParameter(...)或与request.getAttribute(...)取得它们?

In HTTP servlets for Java I want to submit a form. What is the recommended way of carrying the values from the input fields? Is it using hidden fields and get them through request.getParameter(...) or with request.getAttribute(...)?

推荐答案

所有表单数据都将作为请求参数发送,其中输入字段名称作为参数名称,输入字段值作为参数值.

All the form data will be sent as request parameters with the input field name as parameter name and the input field value as parameter value.

例如

<form action="servletURL" method="post">
    <input type="text" name="foo" />
    <input type="text" name="bar" />
    <input type="submit" />
</form>

具有内部doPost()方法:

String foo = request.getParameter("foo");
String bar = request.getParameter("bar");
// ...

除非您希望传递最终用户不需要输入自己的其他数据,否则无需使用JavaScript将它们传输到其他隐藏字段或无关紧要的内容.

You don't need to use JavaScript to transfer them to other hidden fields or something nonsensicial, unless you want to pass additional data which the enduser don't need to enter itself.

请求属性将反过来使用;用于将结果从servlet传递到JSP文件,然后依次将其显示在所有HTML中.

The request attributes are to be used for the other way round; for passing the results from the servlet to the JSP file which should in turn present them along all the HTML.

  • Our servlets wiki page - contains a Hello World
  • How do servlets work? Instantiation, sessions, shared variables and multithreading
  • doGet and doPost in Servlets

这篇关于如何在JSP文件中提交HTML表单的值并将其发送到Servlet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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