在Java中獲取表單提交的數(shù)據(jù)通常需要通過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");
// 處理獲取到的數(shù)據(jù)
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>
當(dāng)用戶填寫表單數(shù)據(jù)并提交后,Servlet會接收到表單提交的數(shù)據(jù),并通過getParameter()方法來獲取數(shù)據(jù)。然后可以對數(shù)據(jù)進(jìn)行相應(yīng)的處理和操作。