我正在对 servlet 进行一些操作并在会话中设置相同的值,当我重定向到某个页面时我使用该值。但是在重定向到页面时,值不会出现,
我知道这是非常基本的问题,但我仍然无法做到。
这是我的小服务程序:
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String path = getServletContext().getRealPath("/accesscarddata/");
doGet(request, response);
readFile(request, response);
prepareExcelEmployeeListToMap(request, response);
classifyEmployeesMapEntries(request, response);
prepareReport(request, response);
generateLeaveReport(request,response);
//request.getRequestDispatcher("upload.jsp").forward(request, response);
response.sendRedirect("upload.jsp");
}
我尝试了这两种方法,但值没有出现在 jsp 中。
编辑:这就是我在jsp中从会话中读取的方式
<%@ page contentType="text/html"%>
<%@ page pageEncoding="UTF-8"%>
<%@ page import="java.util.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%
String leaveReport = "";
if ((session.getAttribute("leaveReport") != null)) {
leaveReport = (String) session.getAttribute("leaveReport");
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Comparing access-card Data with leave manager Data</title>
<link href="css/sling.css" rel="stylesheet" type="text/css" />
<script language="JavaScript" src='script/cal.js'></script>
<!-- Below imports for Blanket purpose-->
<script src='script/blanket.js'></script>
%>
这就是我在加载页面时尝试获取值的方式,第一次显示 javascript 警报,但从 servlet 重定向请求时未显示 alort。
bodyOnLoad();
function bodyOnLoad(){
alert("bodyOnLoad");
var leaveReport = '<%=leaveReport%>';
alert("leaveReport :"+leaveReport);
if(leaveReport!=null && leaveReport!=""){
displayLeaveReport(leaveReport);
}
}
这就是我的 javascript 方法:
function displayLeaveReport(leaveReport){
if(leaveReport == ""){
document.getElementById("leaveReport").style.display = "none";
} else{
document.getElementById("leaveReport").style.display = "";
}
}
JSP:
<tr>
<td width="100%" valign="top">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td class="title_box" align="left">Leave to be availed Employees</td>
</tr>
</table>
<table width="100%" align="center" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="100%" align="left" class="tableBorder" valign="top">
<table width="100%" border="0" cellspacing="0" cellpadding="0" bgcolor="#f9fae3" >
<tr class="lightrow">
<td width="10%" class="title_sub" align="center">S.No</td>
<td width="35%" class="title_sub" align="center">Employee ID</td>
<td width="35%" class="title_sub" align="center">Employee Name</td>
<td width="20%" class="title_sub" align="center">Action</td>
</tr>
</table>
<div id="leaveReport" align="right" style="display : none;height:100px; overflow:auto"></div>
</td>
</tr>
</table>
</td>
</tr>