我是LFSurvey,海外问卷调查对接联系公众号:LFSurvey:国外问卷调查难在哪?

以下是一个基于 Java Web 的简单问卷调查代码示例:

1. 创建问卷调查表单页面 `survey.jsp`,在页面中通过表单提交数据到后台:

“`jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>问卷调查表单</title>
</head>
<body>
<h1>问卷调查表单</h1>
<form action="survey" method="post">
<p>您的姓名:<input type="text" name="name"></p>
<p>您的性别:<input type="radio" name="gender" value="male">男
<input type="radio" name="gender" value="female">女</p>
<p>您的年龄:<input type="text" name="age"></p>
<p>您的职业:<input type="text" name="occupation"></p>
<p>您的爱好:<input type="text" name="hobby"></p>
<p><input type="submit" value="提交"></p>
</form>
</body>
</html>
“`

我是LFSurvey,海外问卷调查对接联系公众号:LFSurvey:国外问卷调查难在哪?
2. 创建后台 Servlet `SurveyServlet`,在后台接收前台表单数据,将数据存储到数据库中,并通过重定向跳转到感谢页面:

“`java
import java.io.IOException;
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("/survey")
public class SurveyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String DB_URL = "jdbc:mysql://localhost:3306/survey_db";
private static final String DB_USER = "root";
private static final String DB_PASSWORD = "password";

protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter("name");
String gender = request.getParameter("gender");
String age = request.getParameter("age");

我是LFSurvey,海外问卷调查对接联系公众号:LFSurvey:国外问卷调查难在哪?
String occupation = request.getParameter("occupation");
String hobby = request.getParameter("hobby");

try {
// 连接数据库
Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);

// 插入数据
String sql = "INSERT INTO survey(name, gender, age, occupation, hobby) VALUES (?, ?, ?, ?, ?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, name);
ps.setString(2, gender);
ps.setString(3, age);
ps.setString(4, occupation);
ps.setString(5, hobby);
ps.executeUpdate();

// 关闭数据库连接
ps.close();
conn.close();

// 跳转到感谢页面
response.sendRedirect("thankyou.jsp");
} catch (SQLException e) {
e.printStackTrace();
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
}
}
“`

3. 创建感谢页面 `thankyou.jsp`,在页面中显示感谢信息:

“`jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>感谢您参加本次问卷调查</title>
</head>
<body>
<h1>感谢您参加本次问卷调查</h1>
<p>您的意见和建议对我们非常重要,我们会认真对待并不断改进产品和服务。</p>
</body>
</html>
“`

以上是一个简单的基于 Java Web 的问卷调查示例代码,可以根据实际需求进行修改和扩展。
我是LFSurvey,海外问卷调查对接联系公众号:LFSurvey 我是LFSurvey,海外问卷调查对接联系公众号:LFSurvey 我是LFSurvey,海外问卷调查对接联系公众号:LFSurvey

商务合作

This will close in 0 seconds