代码之家  ›  专栏  ›  技术社区  ›  Badr

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

  •  0
  • Badr  · 技术社区  · 16 年前

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

    我不知道背后有什么问题

    好的,这是细节

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

    <%@ 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 回复  |  直到 16 年前
        1
  •  1
  •   user444904    16 年前

    除了巴卢斯克指出的那些问题,

    根据所描述的症状” 当servlet重定向页面时,jsp可以显示其中的记录,但是当我从任何其他html页面访问此页面时,它无法显示记录 “和代码” 如果(请求.getAttribute(“dbrec”)!=空) ".

    我对这个问题的主要怀疑是 请求.getAttribute(“dbrec”)

    对于那些从其他html页面直接访问的用户,该值可能为null。我怀疑在重定向的情况下,可能有一些代码请求.setAttribute(“dbrec”,…)

        2
  •  2
  •   BalusC    16 年前

    ResultSet DBProcess 作为JSP的实例变量,使用 <%! %> scriptlet声明,因此它在所有HTTP请求之间共享。我不太清楚 数据库进程 ,但这对我来说无疑是个坏主意 (也适用于 Connection Statement 顺便说一下)。怎么了 数据库进程 在代码的剩余部分被使用也是相当可怕的,例如。 DBProcess.statement.executeQuery() 声明 真正地