java写一个接收前端表单数据写入数据库加上前端的代码
时间: 2024-05-19 08:14:28 浏览: 97
以下是一个简单的Java Servlet示例,可以接收前端表单数据,将其写入MySQL数据库,并返回一个确认页面给前端。
Java Servlet代码:
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/AddDataServlet")
public class AddDataServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取前端表单数据
String name = request.getParameter("name");
String age = request.getParameter("age");
String gender = request.getParameter("gender");
//将数据写入MySQL数据库
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password");
PreparedStatement ps = conn.prepareStatement("INSERT INTO user (name, age, gender) VALUES (?, ?, ?)");
ps.setString(1, name);
ps.setString(2, age);
ps.setString(3, gender);
ps.executeUpdate();
ps.close();
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
//返回确认页面给前端
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>确认页面</title></head>");
out.println("<body>");
out.println("<h1>添加成功!</h1>");
out.println("<p>姓名:" + name + "</p>");
out.println("<p>年龄:" + age + "</p>");
out.println("<p>性别:" + gender + "</p>");
out.println("</body>");
out.println("</html>");
}
}
前端代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加用户</title>
</head>
<body>
<h1>添加用户</h1>
<form action="AddDataServlet" method="post">
<label>姓名:</label><input type="text" name="name"><br>
<label>年龄:</label><input type="number" name="age"><br>
<label>性别:</label>
<input type="radio" name="gender" value="男">男
<input type="radio" name="gender" value="女">女<br>
<input type="submit" value="提交">
</form>
</body>
</html>
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)