很难弄清楚如何在使用表单时让 JSP 文件触发 java 中的方法。该方法将查询发送到数据库,我想从该查询中获取结果集并将其转换为 csv 文件,然后将其发送回用户并由他们的浏览器下载。
jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Form Example</title>
</head>
<body>
<h1>Form Example</h1>
<form action="class/method" method="post">
<input type="submit" name="Download">
</form>
</body>
</html>
Java
public class Class{
public void Method()
String sql="select * from table"
connectionmanger cm=server.getcm();
connection con=null;
preparedstatment statment=null;
resultset rs=null
try{
con=cm.allocateconnection();
statmen=con.preparestatment(sql);
rs=statment.excutequery();
}
catch{
error handling stuff
}
finally{
CSVWriter writer = new CSVWriter(new FileWriter("yourfile.csv"), '\t');
Boolean includeHeaders = true;
java.sql.ResultSet myResultSet = rs
writer.writeAll(myResultSet, includeHeaders);
writer.close();
cm.closestatment(statment);
cm.closeresultset(rs);
cm.delalocateconnection(con);
}
}
您是否使用 servlet 来处理请求?如果是,那么您能否确保您的操作属性指向正确的 servlet 的 doPost 方法(因为表单的方法是 POST)。 我修改了你的代码并纠正了一些事情。请尝试下面的代码并让我知道它是如何进行的。
JSP:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Form Example</title>
</head>
<body>
<h1>Form Example</h1>
<form action="yourServlet/URL" method="post">
<input type="submit" name="Download" value="Download">
</form>
</body>
</html>
SERVLET(JAVA):
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
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;
import com.opencsv.CSVWriter;
@WebServlet("/yourServlet/URL")
public class YourServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String sql = "select * from table";
ConnectionManager cm = ConnectionManager.getInstance();
Connection con = null;
PreparedStatement statement = null;
ResultSet rs = null;
try {
con = cm.allocateConnection();
statement = con.prepareStatement(sql);
rs = statement.executeQuery();
// Set response headers to prompt download
response.setContentType("application/csv");
response.setHeader("Content-Disposition", "attachment; filename=\"yourfile.csv\"");
// Create CSV writer
PrintWriter writer = response.getWriter();
CSVWriter csvWriter = new CSVWriter(writer);
// Write CSV header
csvWriter.writeNext(new String[]{"Column1", "Column2", "Column3"}); // Replace with your actual column names
// Write data rows
while (rs.next()) {
String[] data = {
rs.getString("column1"), // Your actual column names
rs.getString("column2"),
rs.getString("column3")
};
csvWriter.writeNext(data); // Write the CSV data to the response
}
csvWriter.close();
} catch (SQLException e) {
e.printStackTrace();
// Logic for Handling SQL exception
} finally {
//Properly closing resources
cm.closeResultSet(rs);
cm.closeStatement(statement);
cm.deallocateConnection(con);
}
}
}