Java development form submission and processing function
In web application development, forms are an inevitable part. They can be used not only to collect data entered by the user, but also to pass the data to the server for processing. This article will introduce how to implement form submission and processing functions in Java development, with code examples.
<!DOCTYPE html> <html> <head> <title>表单提交示例</title> </head> <body> <form action="processForm" method="POST"> <label for="name">姓名:</label> <input type="text" id="name" name="name" required><br><br> <label for="age">年龄:</label> <input type="number" id="age" name="age" required><br><br> <input type="submit" value="提交"> </form> </body> </html>
In this example, we use a form element to create a form. The action
attribute of the form specifies the URL to which data is sent when the form is submitted. The method
attribute specifies the type of request. Here we use POST request.
import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class FormProcessorServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name = request.getParameter("name"); int age = Integer.parseInt(request.getParameter("age")); // 在这里可以对表单数据进行处理,比如保存到数据库中 // 将处理结果返回给用户 response.setContentType("text/html"); response.getWriter().println("姓名: " + name + "<br>"); response.getWriter().println("年龄: " + age); } }
In the above code, we obtain the data submitted in the form through the request.getParameter
method. We can then do whatever we need with this data, such as saving to a database.
Finally, we use the response.getWriter()
method to return the processing results to the user. In this example, we output the results as HTML to the response stream.
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee https://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0"> <display-name>FormExample</display-name> <servlet> <servlet-name>FormProcessorServlet</servlet-name> <servlet-class>com.example.FormProcessorServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>FormProcessorServlet</servlet-name> <url-pattern>/processForm</url-pattern> </servlet-mapping> </web-app>
In this example, we map the Servlet class name and URL to "FormProcessorServlet" and "/processForm". In this way, when the user submits the form, the Servlet will be called and process the form data.
Summary
This article introduces the steps to implement form submission and processing functions in Java development. We completed the entire process by creating an HTML form, writing a Servlet to process the form, and configuring the Servlet. I hope that through the sample code in this article, you will have a deeper understanding of the form processing function and be able to use it in actual projects.
The above is the detailed content of Java development form submission and processing functions. For more information, please follow other related articles on the PHP Chinese website!