Home > Java > javaTutorial > Java development form submission and processing functions

Java development form submission and processing functions

WBOY
Release: 2023-08-07 09:49:27
Original
1540 people have browsed it

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.

  1. HTML Form
    First, we need to create a form in HTML for users to enter data. Here is a simple example:
<!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>
Copy after login

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.

  1. Servlet handles form submission
    Next, we need to create a Servlet to handle form submission. The following is a sample code for a Servlet that processes a form:
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);
    }
}
Copy after login

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.

  1. Configuring Servlet
    In order for the Servlet to handle form submission, we need to configure it in the web.xml file. The following is an example configuration of the web.xml file:
<?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>
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template