Home  >  Article  >  Java  >  Writing a form submission program using Java

Writing a form submission program using Java

PHPz
PHPzOriginal
2023-08-07 09:27:191710browse

Use Java to write a form submission program

In modern Internet applications, form submission is a basic and important function. Users submit data to the server by filling out forms, and then the server processes and stores the data submitted by the user. In this article, we will write a simple form submission program using Java to let you understand how to use Java to process form data.

First, we need to create a simple HTML form to receive user input. Please save the following code as a "form.html" file.

<!DOCTYPE html>
<html>
<head>
    <title>表单提交示例</title>
</head>
<body>
    <h1>表单提交示例</h1>
    <form action="/submit" method="post">
        <label for="name">姓名:</label>
        <input type="text" id="name" name="name" required><br><br>
        <label for="email">邮箱:</label>
        <input type="email" id="email" name="email" required><br><br>
        <label for="message">留言:</label>
        <textarea id="message" name="message" rows="4" cols="50" required></textarea><br><br>
        <input type="submit" value="提交">
    </form>
</body>
</html>

The above code creates a simple form containing several input fields and a submit button. The action attribute of the form specifies the URL address when submitting data, and we will process the URL address in subsequent Java code.

Next, we need to write a server program in Java to handle form submission. Please save the following code as "FormServlet.java" file.

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 FormServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取表单提交的数据
        String name = request.getParameter("name");
        String email = request.getParameter("email");
        String message = request.getParameter("message");
        
        // 在控制台打印表单数据
        System.out.println("姓名: " + name);
        System.out.println("邮箱: " + email);
        System.out.println("留言: " + message);
        
        // 进行其他处理操作,例如将数据保存到数据库
        
        // 跳转到一个结果页面
        response.sendRedirect("/result.html");
    }
}

The above code uses Java's servlet technology to handle form submission. In the doPost method, we obtain the value of each field in the form through the request.getParameter method and print it to the console. You can perform other operations on this data as required, such as storing it in a database.

Finally, we also need to create a results page to display a prompt message indicating successful submission. Please save the following code as a "result.html" file.

<!DOCTYPE html>
<html>
<head>
    <title>提交成功</title>
</head>
<body>
    <h1>提交成功</h1>
    <p>您的表单已成功提交!感谢您的反馈。</p>
</body>
</html>

Now, we have created a simple form submitter. After the user fills out the form and clicks the submit button, the form data will be submitted to the server-side "FormServlet" program for processing, and then jumps to the "result.html" page to display a successful submission message.

You can save the above code in a Java web project and run the project using a suitable server (such as Tomcat). Then, visit http://localhost:cb0e4711f79077fa4e7e0b4e52a8fd16/form.html in the browser to open the form page for testing.

Hope this article can help you understand how to use Java to write a form submission program. Happy coding!

The above is the detailed content of Writing a form submission program using Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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