在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()方法來獲取數據。然后可以對數據進行相應的處理和操作。