在Java中获取表单提交的数据通常需要通过Servlet来处理。以下是一种常见的方式:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class FormServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
String email = request.getParameter("email");
// 处理获取到的数据
System.out.println("Name: " + name);
System.out.println("Email: " + email);
}
}
<servlet>
<servlet-name>FormServlet</servlet-name>
<servlet-class>com.example.FormServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FormServlet</servlet-name>
<url-pattern>/submitForm</url-pattern>
</servlet-mapping>
<form action="/submitForm" method="post">
<label for="name">Name:</label>
<input type="text" id="name" name="name">
<label for="email">Email:</label>
<input type="email" id="email" name="email">
<input type="submit" value="Submit">
</form>
当用户填写表单数据并提交后,Servlet会接收到表单提交的数据,并通过getParameter()方法来获取数据。然后可以对数据进行相应的处理和操作。