如何在每个请求上重新加载jsp页面?

时间:2010-09-02 10:53:20

标签: java jsp servlets jdbc

我有一个带有jdbc连接的jsp页面,并且在第一次加载时它准确地显示了数据,但之后它显示了空表我认为第二次从内存而不是从服务器加载

我不知道背后的问题是什么

这里有详细信息

我有一个servlet,用于维护登录用户的会话,然后在创建会话后,servlet将用户重定向到jsp页面的视图页面,并在DB中显示现有记录

当servlet重定向页面时,jsp可以显示记录,但是当我从任何其他html页面访问此页面时,它无法显示记录,这里是要查看的jsp页面的代码。

<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.math.BigDecimal" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="java.util.logging.Level" %>
<%@ page import="java.util.logging.Logger" %>
<%@ page import="iEHR.cDBProcessor" %>

<HTML>
<HEAD>
<TITLE>View Patient</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1251">
<script type="text/javascript" language="javascript" src="datepicker/main.js"></script>
                    <script type="text/javascript" language="javascript" src="datepicker/prototype-1.js"></script>
            <script type="text/javascript" language="javascript" src="datepicker/prototype-base-extensions.js"></script>
            <script type="text/javascript" language="javascript" src="datepicker/prototype-date-extensions.js"></script>
            <script type="text/javascript" language="javascript" src="datepicker/behaviour.js"></script>
            <script type="text/javascript" language="javascript" src="datepicker/ratingbar.js"></script>
                            <script type="text/javascript" language="javascript" src="datepicker/datepicker.js"></script>
                                        <link rel="stylesheet" href="datepicker/datepicker.css">
                            <script type="text/javascript" language="javascript" src="datepicker/behaviors.js"></script>
<style type="text/css">
<!--
img {
    border: none;
}
.tah10 {
    font-family: "Times New Roman", Times, serif;
    font-size: 14px;
    text-decoration: none;
    color: #000000;
    font-style: italic;
}
.tah11 {
    font-family: Tahoma;
    font-size: 11px;
    text-decoration: none;
    color: #000000;
}
.ver10 {
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 10px;
    text-decoration: none;
    color: #000000;
}
.ver11 {
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 11px;
    text-decoration: none;
    color: #000000;
}
.tah9 {
    font-family: Tahoma;
    font-size: 9px;
    text-decoration: none;
    color: #000000;
}
.ver9 {
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 9px;
    text-decoration: none;
    color: #000000;
}
td {
    vertical-align: top;
    color: 497B99;
    font-size: 12px;
    font-style: normal;
    font-weight: bolder;
}
-->
</style>
<style type="text/css">
<!--
.bgtop {
    background-repeat: repeat-x;
    background-position: top;
}
-->
</style>
<style type="text/css">
<!--
a {
    font-family: Times New Roman, Times, serif;
    font-size: 12px;
    font-weight: bolder;
    color: 467B99;
    text-decoration: none;
}

.datepicker{
    font-family: Times New Roman, Times, serif;
    font-size: 12px;
    font-weight: bolder;
    color: 467B99;
    text-decoration: none;
}
a:hover {
    font-size: 10px;
    font-weight: bold;
    color: FF8400;
}
.style1 {
    color: 467B99;
    font-family: Times New Roman, Times, serif;
    font-size: 12px;
    text-decoration: none;
    font-style: normal;
    font-weight: bolder;
}
.style2 {
    font-family: Times New Roman, Times, serif;
    font-size: 12px;
    font-style: normal;
    font-weight: bolder;
    color: 467B99;
    text-decoration: none;
}
-->
</style>
</HEAD>
<BODY BGCOLOR=#FFFFFF LEFTMARGIN=0 TOPMARGIN=0 MARGINWIDTH=0 MARGINHEIGHT=0>
<!-- ImageReady Slices (0005_red.psd - Slices: 03, 04, 05) -->
<TABLE WIDTH=100% height="100%" BORDER=0 CELLPADDING=0 CELLSPACING=0 bgcolor="#FFFFFF">
    <TR>
        <TD width="12%"> 
<TABLE WIDTH=159 BORDER=0 CELLPADDING=0 CELLSPACING=0>
    <TR>
        <TD COLSPAN=6>
            <IMG SRC="images/logo.gif" WIDTH=159 HEIGHT=128 ALT=""></TD>
    </TR>
    <TR>
        <TD>
            <IMG SRC="images/s1.gif" WIDTH=20 HEIGHT=20 ALT=""></TD>
        <TD>
            <A HREF="#">
                <IMG SRC="images/r1.gif" WIDTH=29 HEIGHT=20 BORDER=0 ALT=""></A></TD>
        <TD>
            <A HREF="#">
                <IMG SRC="images/r2.gif" WIDTH=31 HEIGHT=20 BORDER=0 ALT=""></A></TD>
        <TD>
            <A HREF="#">
                <IMG SRC="images/r3.gif" WIDTH=31 HEIGHT=20 BORDER=0 ALT=""></A></TD>
        <TD>
            <A HREF="#">
                <IMG SRC="images/r4.gif" WIDTH=28 HEIGHT=20 BORDER=0 ALT=""></A></TD>
        <TD>
            <IMG SRC="images/s2.gif" WIDTH=20 HEIGHT=20 ALT=""></TD>
    </TR>
    <TR>
        <TD COLSPAN=6>
            <IMG SRC="images/s3.gif" WIDTH=159 HEIGHT=93 ALT=""></TD>
    </TR>
    <TR>
        <TD COLSPAN=6>
            <IMG SRC="images/h1.jpg" WIDTH=159 HEIGHT=32 ALT=""></TD>
    </TR>
    <TR>
        <TD COLSPAN=6>
            <IMG SRC="images/img1.jpg" WIDTH=159 HEIGHT=76 ALT=""></TD>
    </TR>
    <TR>
        <TD COLSPAN=6>
            <IMG SRC="images/img2.jpg" WIDTH=159 HEIGHT=81 ALT=""></TD>
    </TR>
    <TR>
        <TD COLSPAN=6>
            <IMG SRC="images/img3.jpg" WIDTH=159 HEIGHT=79 ALT=""></TD>
    </TR>
    <TR>
          <TD COLSPAN=6> <IMG SRC="images/but1.jpg" ALT="" WIDTH=159 HEIGHT=42 border="0" usemap="#Map"></TD>
    </TR>
</TABLE>      </TD>
        <TD width="12%"> 
<TABLE WIDTH=167 BORDER=0 CELLPADDING=0 CELLSPACING=0>
    <TR>
        <TD COLSPAN=2>
            <IMG SRC="images/h2.jpg" WIDTH=167 HEIGHT=25 ALT=""></TD>
    </TR>
    <TR>
        <TD COLSPAN=2 background="images/bg1.gif" HEIGHT=94>
<div style="padding:20;padding-top:5;padding-right:10;padding-bottom:0;color:ffffff" class="tah10">
<strong>Noesis</strong><br><br>
 <strong>Inovative EHR Services </strong> <br>
 <br>
</div>      </TD>
    </TR>
    <TR>
        <TD COLSPAN=2>
            <IMG SRC="images/s4.jpg" WIDTH=167 HEIGHT=52 ALT=""></TD>
    </TR>
    <TR>
        <TD COLSPAN=2 background="images/bg2.gif">
<div style="padding-left:0px;padding-top:12px;padding-bottom:2"><a href="AddPatient.jsp"><span style="padding-left:20;padding-top:5">Add Patient </span></a></div>      </TD>
    </TR>
    <TR>
        <TD COLSPAN=2 background="images/bg3.gif" HEIGHT=18>
<div style="padding-left:20;padding-top:5"><a href="AddPastMedicalHistory.jsp">Add Patient History</a> </div>       

    </TR>
    <TR>
        <TD COLSPAN=2 background="images/bg4.gif" HEIGHT=18>
<div style="padding-left:20;padding-top:5"><a href="AddPatientInsurance.jsp">Add Patient Insurance</a></div>        </TD>
    </TR>
    <TR>
        <TD COLSPAN=2 background="images/bg5.gif" HEIGHT=18 >
<div style="padding-left:20;padding-top:5">View Patient Records</div></TD>
    </TR>
    <TR>
        <TD COLSPAN=2 background="images/bg6.gif" HEIGHT=18 >
<div style="padding-left:20;padding-top:5"><a href="#"></a><a href="AboutUs.jsp">About  Us</a></div>        </TD>
    </TR>
    <TR>
        <TD COLSPAN=2 background="images/bg7.gif" HEIGHT=18 >
<div style="padding-left:20;padding-top:5"><a href="#"></a><a href="ContactUs.jsp">Contact Us</a></div>     </TD>
    </TR>
    <TR>
        <TD COLSPAN=2 background="images/bg8.gif" HEIGHT=18 >
<div style="padding-left:20;padding-top:5"><a href="#"></a></div>       </TD>
    </TR>
    <TR>
        <TD COLSPAN=2>
            <IMG SRC="images/h3.gif" WIDTH=167 HEIGHT=65 ALT=""></TD>
    </TR>
    <TR>
        <TD COLSPAN=2 background="images/bg9.gif" HEIGHT=94>
<div style="padding:15;padding-top:3;padding-bottom:3;color:737373" class="tah10"></div>        </TD>
    </TR>
    <TR>
        <TD COLSPAN=2>
            <IMG SRC="images/h4.jpg" WIDTH=167 HEIGHT=41 ALT=""></TD>
    </TR>
    <TR>
        <TD background="images/bg10.jpg" WIDTH=123 HEIGHT=32>
          <div style="padding-left:15;padding-top:1">
            <input name="text" type="text" size="11">
          </div></TD>
          <TD width="44"> <IMG SRC="images/but2.jpg" ALT="" WIDTH=44 HEIGHT=32 border="0" usemap="#Map2"></TD>
    </TR>
    <TR>
        <TD COLSPAN=2>
            <IMG SRC="images/s5.jpg" WIDTH=167 HEIGHT=48 ALT=""></TD>
    </TR>
    <TR>
      <TD COLSPAN=2>&nbsp;</TD>
      </TR>
</TABLE>      </TD>
      <TD width="72%"> 
          <table width=100% height="193" border=0 cellpadding=0 cellspacing=0>
          <tr>
            <td height=172 colspan="4" ><span class="bgtop"><img src="images/f_m.jpg"></span></td>
            <td height=172 colspan="2" width="63%" background="images/bg_tile_1.gif" class="bgtop">&nbsp;</td>
          </tr>

          <tr>
            <td height=19 colspan="4" background="images/bg11.gif" >&nbsp;</td>
          </tr>
        </table>
        <%! private ResultSet rsResult; %>

         <%!   cDBProcessor DBProcess = new cDBProcessor(); %>



        <%
            DBProcess.ConnectTODB();
            if(request.getAttribute("dbrec") != null)
            {
                rsResult = DBProcess.statement.executeQuery("SELECT * FROM patients");
            }//end if
            out.println("<table border=\"0\"><tr><td valign=\"top\" >");
            out.println("<tr><th>Patient ID</th><th>First Name</th><th>Middle Name</th><th>Last Name</th><th>Gender</th><th>Marital Status</th><th>Phone No.</th><th>Address</th>");
            out.println("<th>Date Of Birth</th><th>Last Date Of Exam</th><th>Status</th></tr>");

            //data display on page
            if(rsResult!= null)
            {
                try
                {
                    while (rsResult.next())
                    {
                        BigDecimal bdPatientID = rsResult.getBigDecimal("patient_id");
                        String strFirstname = rsResult.getString("first_name");
                        String strLastname = rsResult.getString("last_name");
                        String strMiddlename = rsResult.getString("middle_name");
                        String strGeneder = rsResult.getString("gender");
                        String strMeritalStatus = rsResult.getString("marital_status");
                        BigDecimal bdPhoneNo = rsResult.getBigDecimal("phone_no");
                        String strAddress = rsResult.getString("address");
                        String strDOB = rsResult.getDate("birth_dt") == null ? "" : rsResult.getDate("birth_dt").toString();
                        String strDOE = rsResult.getDate("dt_of_exam") == null ? "" :rsResult.getDate("dt_of_exam").toString();
                        String strStatus;
                        Byte bPatientStatus= rsResult.getByte("status");

                        if(bPatientStatus == 1)
                        {
                            strStatus = "Active";
                        }//end if
                        else
                        {
                             strStatus = "Inactive";
                        }//end else

                       out.println("<tr><td>"+bdPatientID+"</td><td>"+strFirstname+"</td><td>"+strMiddlename+"</td><td>"+strLastname+"</td>");
                       out.println("<td>"+strGeneder+"</td><td>"+strMeritalStatus+"</td><td>"+bdPhoneNo+"</td><td>"+strAddress+"</td>");
                       out.println("<td>"+strDOB+"</td><td>"+strDOE+"</td><td>"+strStatus+"</td></tr>");

                    }//end while
                    out.println("</table>");
                }//end try
                catch (SQLException ex)
                {
                    out.println("<I>exception</I><br>");

                }//end catch
            }//end if

        DBProcess.CloseDB();
        %>

      </TD>
        <TD width="4%" background="images/bg_tile_1.gif" class="bgtop">&nbsp;</TD>
    </TR>
    <TR>
        <TD height="100%" colspan="4">&nbsp;</TD>
    </TR>
    <TR>
        <TD colspan="3">
<TABLE WIDTH=768 BORDER=0 CELLPADDING=0 CELLSPACING=0>
    <TR>
        <TD width="326">
            <IMG SRC="images/s7.gif" WIDTH=326 HEIGHT=48 ALT=""></TD>
        <TD background="images/bg16.gif" WIDTH=442 HEIGHT=48>
<div style="padding-top:12;color:A8A8A8" class="tah11">
2010 © Copyright iAS. <Br>
All rights Reserved. Read <a href="#" style="color:467B99" class="tah11">Privacy Policy</a>.</div>      </TD>
    </TR>
</TABLE>        </TD>
        <TD width="4%" background="images/bg_tile_2.gif">&nbsp;</TD>
    </TR>
</TABLE>
<!-- End ImageReady Slices -->
<map name="Map">
  <area shape="rect" coords="39,2,133,27" href="#">
</map>
<map name="Map2">
  <area shape="circle" coords="9,12,9" href="#">
</map>
</BODY>
</HTML> 

2 个答案:

答案 0 :(得分:2)

它闻起来像是线程安全/范围问题。您已使用ResultSet scriptlet声明将DBProcess<%! %>声明为JSP的实例变量,因此它已在所有HTTP请求之间共享。我不确定DBProcess,但对于ResultSet(以及顺便提到ConnectionStatement)这肯定是一个坏主意。如何在代码的残余中使用DBProcess也是非常可怕的,例如DBProcess.statement.executeQuery()Statement 真的是公共字段吗?我不知道这个班级的内部是什么样的,但是,这有点像线程安全/范围问题。

此外,这种90年代的HTML风格编写和在JSP中使用scriptlet实际上并不适合“Copyright 2010”应用程序。你在阅读正确的教程/书籍吗?

答案 1 :(得分:1)

除了BalusC指出的那些问题,

基于描述的症状“ 当servlet重定向页面时,jsp可以显示记录,但是当我从任何其他html页面访问此页面时,它无法显示记录 “和代码” if(request.getAttribute(“dbrec”)!= null) “。

我对该问题的主要疑问是使用 request.getAttribute(“dbrec”)

对于那些从其他html页面直接访问,此值可能为null。我怀疑是重定向的情况,可能有一些代码执行request.setAttribute(“dbrec”,....)