0

我正在对 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>
4

1 回答 1

0

如果您使用 dwr 之类的 ajax 技术,则可以在 div 标签中设置值,如果不是,则无法在 div 标签中设置值,请在 jsp 中更改代码,如下所示,将显示 vlaues。

if(reportMap.size()>0){
     Iterator<Map.Entry<String, String>> entries = reportMap.entrySet().iterator();
     while (entries.hasNext()) {
        Map.Entry<String, String> entry = entries.next();
        employeeid = entry.getKey();
        employeename = entry.getValue();
        %>
        <tr class='<%=classname %>'>
        <td width="10%" class="title_sub" align="center"><%=sno %></td>
        <td width="35%" class="title_sub" align="center"><%=employeeid %></td>
        <td width="35%" class="title_sub" align="center"><%=employeename %></td>
        <td width="20%" class="title_sub" align="center">Action</td>
        </tr>
        <%
        style++;
        }
        }
于 2012-05-25T10:42:08.043 回答