简体   繁体   English

检查表单元素是否已提交并处理提交的表单

[英]Check if form element is submitted and handling submitted form

here is my code: 这是我的代码:

<form method="POST" action="">
<div class="col-lg-4 pull-right">
            <div class="form-group">
                <label for="txtUserId">ID:</label>
                <input type="text" name="txtUserId" id="txtUserId" class="form-control input-lg" />
            </div>
            <div class="form-group">
                <label for="txtUserName">NAME:</label>
                <input type="text" name="txtUserName" id="txtUserName" class="form-control input-lg" />
            </div>
            <div class="form-group">
                <label for="txtUserLastName">LastName:</label>
                <input type="text" name="txtUserLastName" id="txtUserLastName" class="form-control input-lg" />
            </div>
        </div>
</form>

First question: Can we handle post request at .jsp file? 第一个问题:我们可以处理.jsp文件中的发布请求吗? or we must get it from java class only? 还是必须仅从Java类获取它?

Second question: how can check which button is clicked? 第二个问题:如何检查单击了哪个按钮?

like: if(isset($_POST['btnSAVE']) in php. 如: if(isset($_POST['btnSAVE'])在php中。

best regards. 最好的祝福。

  1. can we handle post request at .jsp file 我们可以处理.jsp文件中的发布请求吗
    • Yes. 是。 Read values sent in request from previous JSP using "request.getParameter" 使用“ request.getParameter”读取先前JSP在请求中发送的值
  2. how can check which button is clicked 如何检查单击了哪个按钮
 <FORM NAME="form1" METHOD="POST" Action="SomePage.jsp"> <INPUT TYPE="SUBMIT" NAME="submit" VALUE="Button 1"> <INPUT TYPE="SUBMIT" NAME="submit" VALUE="Button 2"> <INPUT TYPE="SUBMIT" NAME="submit" VALUE="Button 3"> </FORM> 
String submit = request.getParameter("submit");

submit will contain value of button clicked. 提交将包含单击的按钮的值。

However, I will recommend to use servlet for form post processing. 但是,我建议使用servlet进行表单后处理。

Yes it can be done. 是的,可以做到。

There are two JSP pages that are required, first one is to display the form and second one is the to process the form submission. 需要两个JSP页面,第一个是显示表单,第二个是处理表单提交。 We can use same JSP to process the form as well, but then we would need to verify if the page was requested by form submission. 我们也可以使用相同的JSP来处理表单,但是随后我们需要验证表单提交是否请求了该页面。

Suppose the JSP for the form content is 假设表单内容的JSP为

<form method="POST" action="/processForm.jsp">
 ...

Then processForm.jsp 然后processForm.jsp

<%@ page import="java.io.*,java.util.*, javax.servlet.*"%>
<%@ page import="javax.servlet.http.*"%>
<%
   System.out.println("HTTP method:" + request.getMethod());

   if("POST".equals(request.getMethod()) {
       System.out.println(request.getParameter("txtUserId"));
       System.out.println(request.getParameter("txtUserName"));
       System.out.println(request.getParameter("txtUserLastName"));

       //Do something with the parameters
   }
%>

Answer to the second question is to set the value of the button input to something that signifies a click before form submission, then value of this button input in the JSP can be accessed as below 回答第二个问题是在提交表单之前将按钮输入的值设置为表示单击的值,然后可以按以下方式访问JSP中此按钮输入的值

String clicked = request.getParameter("btnSAVE");

Or we can include a hidden input. 或者我们可以包含一个隐藏的输入。 On click of the btnSAVE value of this input is set before the form submission and the same is received by processForm.jsp 在单击btnSAVE值时,将在表单提交之前设置此输入的值, processForm.jsp会接收到processForm.jsp

Since JSP's dynamic content (scriplet) is processed on the server, we can do things like reading a file or connecting to database in there. 由于JSP的动态内容(脚本片​​段)是在服务器上处理的,因此我们可以执行诸如读取文件或连接其中的数据库之类的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM