gpt4 book ai didi

java - 无法获取从jsp到servlet的传递值

转载 作者:行者123 更新时间:2023-12-01 12:48:42 26 4
gpt4 key购买 nike

如果我运行 jsp,在将内容导出到 Excel 时,我不会获取下载的 Excel 文件中的值。它只是空的。这是我尝试过的..

如何将表值传递给 servlet?

Excel.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import ="javax.swing.JOptionPane"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Export to Excel - Demo</title>
</head>
<body>

<table align="left" border="2">
<thead>
<tr bgcolor="lightgreen">
<th>Sr. No.</th>
<th>Text Data</th>
<th>Number Data</th>
</tr>
</thead>
<tbody>
<%
for (int i = 0; i < 10; i++) {
%>
<tr bgcolor="lightblue">
<td align="center"><%=i + 1%></td>
<td align="center">This is text data <%=i%></td>
<td align="center"><%=i * i%></td>
</tr>
<%
}
%>
</tbody>
</table>

<a href="Sample?exportToExcel=YES">Export to Excel</a>

</body>
</html>

示例.java(Servlet)

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 Sample extends HttpServlet {
private static final long serialVersionUID = 1L;

public Sample() {
super();
}


protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

String exportToExcel = request.getParameter("exportToExcel");

if (exportToExcel != null
&& exportToExcel.toString().equalsIgnoreCase("YES")) {
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "inline; filename="
+ "excel.xls");
}
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub

}
}

最佳答案

如果您尝试将表(或网页)导出excel文件,您需要编写下载内容(响应)代码在 SAME .jsp 中,所以在你的情况下。

在您的 Excel.jsp 文件中添加以下内容。

   <%//Use scriptlet
if (exportToExcel != null
&& exportToExcel.toString().equalsIgnoreCase("YES")) {
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "inline; filename="
+ "excel.xls");
}
%>
//In short COPY whole code from Servlet's get method and paste it in Scriplet

所以在这里你显然是在调用你自己的 .jsp 并处理请求,并且由于 exportToExcel 等于 YES 它会给你包含 .jsp 内容的文件。

关于java - 无法获取从jsp到servlet的传递值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24426612/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com