How to submit and send values of a HTML form in JS

2020-04-16 02:58发布

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(...)?

2条回答
狗以群分
2楼-- · 2020-04-16 03:03

All form submitted can ONLY be accessed through getParameter(...). getAttribute() is meant for transferring request scoped data across servlet/jsp within the context of a single request on server side.

查看更多
何必那么认真
3楼-- · 2020-04-16 03:05

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.

E.g.

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

With inside doPost() method:

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

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.

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.

See also:

查看更多
登录 后发表回答