Java 如何使用该字段的 id 通过 Servlet 读取 JSP 表单中输入字段的值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32374375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to read value of a input field in JSP form through a Servlet using id of that field?
提问by tpsaitwal
My JSP
contains this form
tag:
我的JSP
包含这个form
标签:
<form action="MyServlet" method="post">
Fname:<input type="text" id="fname" placeholder="type first name"/>
<input type="submit" value="ok"/>
</form>
My servlet
is:
我的servlet
是:
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.cad.database.DatabaseClass;
import com.cad.example.service.InputService;
public class Input extends HttpServlet {
private static final long serialVersionUID = 1L;
public Input() {
super();
// TODO Auto-generated constructor stub
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String fname = request.getParameterById("fname");
System.out.println("My name "+fname);
}
}
采纳答案by Anandhakumar Subramaniam
You can use name
field. Thats the proper way and can go with the method request.getParameter()
.
您可以使用name
字段。那是正确的方法,可以使用该方法request.getParameter()
。
回答by Harshit Shrivastava
Also add name="fname"
to
还添加name="fname"
到
<input type="text" name="fname" id="fname" placeholder="type first name"/>
Retrieve it like this
像这样检索它
String fname = request.getParameter("fname");
回答by Harshit Shrivastava
You can use the below updates:
您可以使用以下更新:
In jsp/html, add attribute name
:
在jsp/html中,添加属性name
:
<input type="text" id="fname" name="fname" placeholder="type first name"/>
In servlet:
在 servlet 中:
String fname = request.getParameter("fname");
回答by ChandrasekarG
Add a name attribute to the input element. Now that, your HTML will look like,
向 input 元素添加 name 属性。现在,您的 HTML 将如下所示,
<form action="MyServlet" method="post">
Fname:
<input type="text" name="fname" placeholder="type first name" />
<input type="submit" value="ok" />
</form>
This can be accessed anywhere in your servlet/java code as,
这可以在您的 servlet/java 代码中的任何地方访问,因为,
String fName = request.getParameter("fname");
String fName = request.getParameter("fname");
As far as i know, ID attribute cannot be used to get values in java. JavaScript can be used in cases to get the innerText or innerHTML using an ID attribute.
据我所知,ID 属性不能用于在 java 中获取值。在使用 ID 属性获取 innerText 或 innerHTML 的情况下,可以使用 JavaScript。
回答by Bacteria
You already got your answer that you need to specify name attribute in your input text field
您已经得到了需要在输入文本字段中指定 name 属性的答案
<input type="text" name="fname" id="fname" placeholder="type first name"/>
But why you need to specify this name attribute in input text field that may be still not clear to you.
但是为什么您需要在输入文本字段中指定此名称属性,您可能仍然不清楚。
We Use input text on form elements to submit information. Only input tags with a name attribute are submitted to the server. So if don't specify the name attribute and do request.getParameter("fname");
you don't get the value at server end at all.
我们使用表单元素上的输入文本来提交信息。只有带有 name 属性的输入标签才会提交给服务器。因此,如果不指定 name 属性并且request.getParameter("fname");
您根本不会在服务器端获得该值。