如何从 html 按钮获取标识符
How to take identificator from html button
我在 index.html
中有那个表格
<form action="Servlet">
<p>Type text:<br><textarea cols="100" rows="10" name=encipher></textarea></p>
<input type="submit" value="Encipher" name=encipherSubmit id="a"/>
<input type="submit" value="Decipher" name=decipherSubmit id="b"/>
</form>
而且我不知道如何从按钮中获取一些标识符。我需要在按下第一个按钮时执行加密方法,在按下第二个按钮时执行解密方法。
对于 Servlet.java 中的文本区域,我有代码:
String encipher = req.getParameter("encipher");
以及如何从按钮中获取参数?
好吧,你不知道你点击了哪个按钮,因为按钮只是提交表单。实际上 servlet 可以处理任何 header 或您通过 HTTP 发送的数据,但按钮名称不会发送。
或者,您可以为每个按钮编写一个 javascript onclick 事件处理程序,然后以编程方式发送带有附加参数的提交 - 单击按钮。
每个输入都使用您为其指定的名称发送。您的提交按钮有名称。因此,如果您单击 "Encipher" 按钮,您将有一个名为 encipherSubmit
的参数,其值为 Encipher
。如果您单击 "Decipher" 按钮,您将有一个名为 decipherSubmit
的参数,其值为 Decipher
.
就好像这些是文本字段一样,但好处是只会发送您实际用于提交的按钮。
所以你可以这样做:
String encipherButton = req.getParameter("encipherSubmit");
String decipherButton = req.getParameter("decipherSubmit");
if ( encipherButton != null && encipherButton.equals("Encipher") ) {
// Do encipher operation
} else if ( decipherButton != null && decipherButton.equals("Decipher") ) {
// Do decipher operation
} else {
// Form was submitted without using the buttons.
// Decide what you want to do in this case.
}
其实大多数情况下,只要检查encipherButton != null
和decipherButton != null
就够了。
我在 index.html
中有那个表格<form action="Servlet">
<p>Type text:<br><textarea cols="100" rows="10" name=encipher></textarea></p>
<input type="submit" value="Encipher" name=encipherSubmit id="a"/>
<input type="submit" value="Decipher" name=decipherSubmit id="b"/>
</form>
而且我不知道如何从按钮中获取一些标识符。我需要在按下第一个按钮时执行加密方法,在按下第二个按钮时执行解密方法。 对于 Servlet.java 中的文本区域,我有代码:
String encipher = req.getParameter("encipher");
以及如何从按钮中获取参数?
好吧,你不知道你点击了哪个按钮,因为按钮只是提交表单。实际上 servlet 可以处理任何 header 或您通过 HTTP 发送的数据,但按钮名称不会发送。
或者,您可以为每个按钮编写一个 javascript onclick 事件处理程序,然后以编程方式发送带有附加参数的提交 - 单击按钮。
每个输入都使用您为其指定的名称发送。您的提交按钮有名称。因此,如果您单击 "Encipher" 按钮,您将有一个名为 encipherSubmit
的参数,其值为 Encipher
。如果您单击 "Decipher" 按钮,您将有一个名为 decipherSubmit
的参数,其值为 Decipher
.
就好像这些是文本字段一样,但好处是只会发送您实际用于提交的按钮。
所以你可以这样做:
String encipherButton = req.getParameter("encipherSubmit");
String decipherButton = req.getParameter("decipherSubmit");
if ( encipherButton != null && encipherButton.equals("Encipher") ) {
// Do encipher operation
} else if ( decipherButton != null && decipherButton.equals("Decipher") ) {
// Do decipher operation
} else {
// Form was submitted without using the buttons.
// Decide what you want to do in this case.
}
其实大多数情况下,只要检查encipherButton != null
和decipherButton != null
就够了。