response.sendRedirect gives "uses or overrides deprecated API" error when used to redirect to another JSP when the database recordset is empty

Hi,

I am successfully receiving data from a database. However when the database is empty and my JSP tries to recover records from it, I get a

500 Servlet Exception
java.sql.SQLException: Illegal operation on empty result set

error. Fair enough, but I don't want this to display when this happens (bad for people using my website. What I want to happen is that the page redirects to a page saying that "no records were found".

I am trying to use response.sendRedirect to do this. The relevant code is below:

<%      
conn = pool.getConnection();
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
String sqlStatement = "SELECT * from members";
ResultSet Recordset1 = stmt.executeQuery(sqlStatement);
boolean Recordset1_isEmpty = !Recordset1.next();
boolean Recordset1_hasData = !Recordset1_isEmpty;
Object Recordset1_data;
int Recordset1_numRows = 0;
int Repeat1__numRows = 10;
int Repeat1__index = 0;
Recordset1_numRows += Repeat1__numRows;
%>
<!-- BEGIN The following redirects the page if no records are found -->
<%
if (Recordset1_isEmpty) {
Recordset1.close();
conn.close();
response.sendRedirect("no_records.jsp");
} %>

The strange thing is, this code DOES actually do the job BUT my RESIN server kicks out an error log which includes the statement

_profileview__jsp.java uses or overrides a deprecated API.
[19:04:42:531] Note : Recompile with -deprecation for details

(apologies I don't know how to dump text out of my MSDOS window where this log is appearing). Is this log just being silly or am I doing something wrong with my code?

Thankyou for all your help in advance folks.

JamesFrogAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TrekkyLeaperCommented:
This is strange. I'm guessing that you've played around enough to be sure it's that call that is causing the deprecation message. The sendRedirect call certainly seems to be there in the J2EE spec. However, most app servers distribute their own brand of J2EE API. Some (like IBM) even distribute their own version of java. So my question is, what app server are you using?
0
kiranhkCommented:
can you post your full jsp code. the sendRedirect is not the one causing the deprecated api warning.
0
JamesFrogAuthor Commented:
Thankyour for the reply, here is my full code, this is to display profiles in a dating agency:



<%@page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*"%>
<%@ page import='javax.sql.*' %>
<%@ page import='javax.naming.*' %>
<!-- util class is here to do the gregorian calender bit -->
<%@ page import='java.util.*' %>

<!-- BEGIN JAVA TO MAKE A CONNECTION TO THE POOL TO DISPLAY DATA IN THE FORM gg-->
<% DataSource pool;%>
<%      try
            {
                  Context env = (Context) new InitialContext().lookup("java:comp/env");
                  pool = (DataSource) env.lookup("jdbc/dbeurogaydate");
                  if (pool == null) throw new ServletException("`jdbc/dbeurogaydate' is an unknown DataSource");
                }
            catch (NamingException e)
            {
            throw new ServletException(e);
                }
                  %>
                  <!-- INITIALIZE THE CONN CONNECTION -->
                  <%Connection conn = null;%>
      <!-- END JAVA TO MAKE A CONNECTION TO THE POOL -->
      
      
                  <%      
                  conn = pool.getConnection();
                  Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
                  String sqlStatement = "SELECT membername, members_ID, birthday_day_ID, birthday_month_ID, birthday_year, introduction, height, weight, gender, country, city, ethnic_origin FROM members, birthday_year, height, weight, gender, country, city, ethnic_origin WHERE members_ID='2' AND members.birthday_year_ID=birthday_year.birthday_year_ID AND members.height_ID=height.height_ID AND members.weight_ID=weight.weight_ID AND members.gender_ID=gender.gender_ID AND members.country_ID=country.country_ID AND members.city_ID=city.city_ID AND members.ethnic_origin_ID=ethnic_origin.ethnic_origin_ID";
                  ResultSet Recordset1 = stmt.executeQuery(sqlStatement);
                  
                  
            
                  
                  boolean Recordset1_isEmpty = !Recordset1.next();
                  boolean Recordset1_hasData = !Recordset1_isEmpty;
                  Object Recordset1_data;
                  int Recordset1_numRows = 0;
                  int Repeat1__numRows = 10;
                  int Repeat1__index = 0;
                  Recordset1_numRows += Repeat1__numRows;
                  %>
      
            <!-- BEGIN The following redirects the page if no records are found -->
      <%      if (Recordset1_isEmpty) {
                  Recordset1.close();
                  conn.close();
    response.sendRedirect("no_records.jsp");
                  } %>
                  
      
            <!-- END The folclowing redirects the page if no records are found -->
            
                  <!-- THIS CODES WORKS OUT THE AGE OF THE member AND STORES IT AS membersage-->
                  <%
                  // BEGIN The following code calculated the AGE of the member we are viewing -->
                  String monthstring= (String)Recordset1.getString("birthday_month_ID");
                  String yearstring= (String)Recordset1.getString("birthday_year");
                  String daystring= (String)Recordset1.getString("birthday_day_ID");
                  // converts date parts to ints
                int MM = Integer.parseInt(monthstring)-1; // account for GregorianCalendar weirdness
                int DD = Integer.parseInt(daystring  );
                      int YY = Integer.parseInt(yearstring );
                  // Create a calendar object with the date of birth
                     GregorianCalendar c1 = new GregorianCalendar(YY,MM,DD); // initializes GregorianCalender c1
                  c1.set(YY,MM,DD); // sets c1 to inputed date
                  // Create a calendar object with today's date
                  Calendar today = Calendar.getInstance();
                // Get age based on year
                  //  int age = today.get(Calendar.YEAR) - dateOfBirth.get(Calendar.YEAR);
                int membersage = today.get(Calendar.YEAR) - c1.get(Calendar.YEAR);
                  // Add the tentative age to the date of birth to get this year's birthday
                  //  dateOfBirth.add(Calendar.YEAR, age);
                    c1.add(Calendar.YEAR, membersage);
                // If this year's birthday has not happened yet, subtract one from age
                //if (today.before(dateOfBirth)) {
                  if (today.before(c1))
                  {
                    membersage--;
                }
                  %>            
                  <!-- BEGIN THIS CODES WORKS OUT THE AGE OF THE member AND STORES IT AS membersage-->
                  
                  <!-- BEGIN THIS CODE SORTS OUT THE PAGE NAVIGATION -->
                  <%
                  // *** Recordset Stats, Move To Record, and Go To Record: declare stats variables
                  int Recordset1_first = 1;
                  int Recordset1_last  = 1;
                  int Recordset1_total = -1;

                  if (Recordset1_isEmpty) {
                Recordset1_total = Recordset1_first = Recordset1_last = 0;
                  }

                  //set the number of rows displayed on this page
                  if (Recordset1_numRows == 0) {
                    Recordset1_numRows = 1;
                  }
                  %>
                  <%
                  // *** Recordset Stats: if we don't know the record count, manually count them
                  if (Recordset1_total == -1) {
                  // count the total records by iterating through the recordset
                   for (Recordset1_total = 1; Recordset1.next(); Recordset1_total++);
                  // reset the cursor to the beginning
                    Recordset1.close();
                    Recordset1 = stmt.executeQuery(sqlStatement);
                    Recordset1_hasData = Recordset1.next();

                    // set the number of rows displayed on this page
                    if (Recordset1_numRows < 0 || Recordset1_numRows > Recordset1_total) {
                Recordset1_numRows = Recordset1_total;
                    }

                    // set the first and last displayed record
                    Recordset1_first = Math.min(Recordset1_first, Recordset1_total);
                    Recordset1_last  = Math.min(Recordset1_first + Recordset1_numRows - 1, Recordset1_total);
                  }
                  %>
                  <% String MM_paramName = ""; %>
                  <%
                  // *** Move To Record and Go To Record: declare variables

                  ResultSet MM_rs = Recordset1;
                  int       MM_rsCount = Recordset1_total;
                  int       MM_size = Recordset1_numRows;
                  String    MM_uniqueCol = "";
            MM_paramName = "";
                  int       MM_offset = 0;
                  boolean   MM_atTotal = false;
                  boolean   MM_paramIsDefined = (MM_paramName.length() != 0 && request.getParameter(MM_paramName) != null);
                  %>
                  <%
                  // *** Move To Record: handle 'index' or 'offset' parameter

                  if (!MM_paramIsDefined && MM_rsCount != 0) {

                    //use index parameter if defined, otherwise use offset parameter
                    String r = request.getParameter("index");
                    if (r==null) r = request.getParameter("offset");
                    if (r!=null) MM_offset = Integer.parseInt(r);

                    // if we have a record count, check if we are past the end of the recordset
                    if (MM_rsCount != -1) {
                if (MM_offset >= MM_rsCount || MM_offset == -1) {  // past end or move last
                  if (MM_rsCount % MM_size != 0)    // last page not a full repeat region
              MM_offset = MM_rsCount - MM_rsCount % MM_size;
                  else
              MM_offset = MM_rsCount - MM_size;
                }
                    }

                    //move the cursor to the selected record
                    int i;
                    for (i=0; Recordset1_hasData && (i < MM_offset || MM_offset == -1); i++) {
                Recordset1_hasData = MM_rs.next();
                    }
                    if (!Recordset1_hasData) MM_offset = i;  // set MM_offset to the last possible record
                  }
%>
<%
// *** Move To Record: if we dont know the record count, check the display range

if (MM_rsCount == -1) {

  // walk to the end of the display range for this page
  int i;
  for (i=MM_offset; Recordset1_hasData && (MM_size < 0 || i < MM_offset + MM_size); i++) {
    Recordset1_hasData = MM_rs.next();
  }

  // if we walked off the end of the recordset, set MM_rsCount and MM_size
  if (!Recordset1_hasData) {
    MM_rsCount = i;
    if (MM_size < 0 || MM_size > MM_rsCount) MM_size = MM_rsCount;
  }

  // if we walked off the end, set the offset based on page size
  if (!Recordset1_hasData && !MM_paramIsDefined) {
    if (MM_offset > MM_rsCount - MM_size || MM_offset == -1) { //check if past end or last
      if (MM_rsCount % MM_size != 0)  //last page has less records than MM_size
        MM_offset = MM_rsCount - MM_rsCount % MM_size;
      else
        MM_offset = MM_rsCount - MM_size;
    }
  }

  // reset the cursor to the beginning
  Recordset1.close();
  Recordset1 = stmt.executeQuery(sqlStatement);
  Recordset1_hasData = Recordset1.next();
  MM_rs = Recordset1;

  // move the cursor to the selected record
  for (i=0; Recordset1_hasData && i < MM_offset; i++) {
    Recordset1_hasData = MM_rs.next();
  }
}
%>
<%
// *** Move To Record: update recordset stats

// set the first and last displayed record
Recordset1_first = MM_offset + 1;
Recordset1_last  = MM_offset + MM_size;
if (MM_rsCount != -1) {
  Recordset1_first = Math.min(Recordset1_first, MM_rsCount);
  Recordset1_last  = Math.min(Recordset1_last, MM_rsCount);
}

// set the boolean used by hide region to check if we are on the last record
MM_atTotal  = (MM_rsCount != -1 && MM_offset + MM_size >= MM_rsCount);
%>
<%
// *** Go To Record and Move To Record: create strings for maintaining URL and Form parameters

String MM_keepBoth,MM_keepURL="",MM_keepForm="",MM_keepNone="";
String[] MM_removeList = { "index", MM_paramName };

// create the MM_keepURL string
if (request.getQueryString() != null) {
  MM_keepURL = '&' + request.getQueryString();
  for (int i=0; i < MM_removeList.length && MM_removeList[i].length() != 0; i++) {
  int start = MM_keepURL.indexOf(MM_removeList[i]) - 1;
    if (start >= 0 && MM_keepURL.charAt(start) == '&' &&
        MM_keepURL.charAt(start + MM_removeList[i].length() + 1) == '=') {
      int stop = MM_keepURL.indexOf('&', start + 1);
      if (stop == -1) stop = MM_keepURL.length();
      MM_keepURL = MM_keepURL.substring(0,start) + MM_keepURL.substring(stop);
    }
  }
}

// add the Form variables to the MM_keepForm string
if (request.getParameterNames().hasMoreElements()) {
  java.util.Enumeration items = request.getParameterNames();
  while (items.hasMoreElements()) {
    String nextItem = (String)items.nextElement();
    boolean found = false;
    for (int i=0; !found && i < MM_removeList.length; i++) {
      if (MM_removeList[i].equals(nextItem)) found = true;
    }
    if (!found && MM_keepURL.indexOf('&' + nextItem + '=') == -1) {
      MM_keepForm = MM_keepForm + '&' + nextItem + '=' + java.net.URLEncoder.encode(request.getParameter(nextItem));
    }
  }
}

// create the Form + URL string and remove the intial '&' from each of the strings
MM_keepBoth = MM_keepURL + MM_keepForm;
if (MM_keepBoth.length() > 0) MM_keepBoth = MM_keepBoth.substring(1);
if (MM_keepURL.length() > 0)  MM_keepURL = MM_keepURL.substring(1);
if (MM_keepForm.length() > 0) MM_keepForm = MM_keepForm.substring(1);
%>
<%
// *** Move To Record: set the strings for the first, last, next, and previous links

String MM_moveFirst,MM_moveLast,MM_moveNext,MM_movePrev;
{
  String MM_keepMove = MM_keepBoth;  // keep both Form and URL parameters for moves
  String MM_moveParam = "index=";

  // if the page has a repeated region, remove 'offset' from the maintained parameters
  if (MM_size > 1) {
    MM_moveParam = "offset=";
    int start = MM_keepMove.indexOf(MM_moveParam);
    if (start != -1 && (start == 0 || MM_keepMove.charAt(start-1) == '&')) {
      int stop = MM_keepMove.indexOf('&', start);
      if (start == 0 && stop != -1) stop++;
      if (stop == -1) stop = MM_keepMove.length();
      if (start > 0) start--;
      MM_keepMove = MM_keepMove.substring(0,start) + MM_keepMove.substring(stop);
    }
  }

  // set the strings for the move to links
  StringBuffer urlStr = new StringBuffer(request.getRequestURI()).append('?').append(MM_keepMove);
  if (MM_keepMove.length() > 0) urlStr.append('&');
  urlStr.append(MM_moveParam);
  MM_moveFirst = urlStr + "0";
  MM_moveLast  = urlStr + "-1";
  MM_moveNext  = urlStr + Integer.toString(MM_offset+MM_size);
  MM_movePrev  = urlStr + Integer.toString(Math.max(MM_offset-MM_size,0));
}
%>
<%
int TFM_middlePages,TFM_startLink,TFM_endLink,TFM_LimitPageEndCount;
int TFM_LimitCounter,TFM_LimitLinksEndCount,TM_LinksEndCount;
%>
<%
int Recordset1_TFMcurrentPage;
int Recordset1_TFMtotalPages;
if(MM_size>0) {
  Recordset1_TFMcurrentPage = (int)Math.floor((double)Recordset1_last/MM_size + .5);
  Recordset1_TFMtotalPages = (int)Math.floor((double)Recordset1_total/MM_size + .5);
}else{
  Recordset1_TFMcurrentPage = 0;
  Recordset1_TFMtotalPages = 0;
}
%>




<html>
<head>
<title>Profile View  dfgdfgdfg</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" href="/resin-doc/EUROGAYDATE/css/eurogaydate.css" type="text/css">
</head>
<body bgcolor="#FFFFFF" text="#000000">

      <% while ((Recordset1_hasData)&&(Repeat1__numRows-- != 0)) { %>
      <table width="640" border="0" cellpadding="2" cellspacing="0">
        <tr>
          <td width="69" valign="top" bgcolor="#CCCCCC" rowspan="3">70x70</td>
          <td width="50" valign="top" bgcolor="#FFFFFF"><img src="/resin-doc/EUROGAYDATE/images/euroflags/euroflagthumbnails/unitedkingdom-flagtn.gif" width="50" height="25"></td>
          <td width="509" valign="top"><%=(((Recordset1_data = Recordset1.getObject("membername"))==null || Recordset1.wasNull())?"":Recordset1_data)%> / <%=(((Recordset1_data = Recordset1.getObject("members_ID"))==null || Recordset1.wasNull())?"":Recordset1_data)%> </td>
        </tr>
        <tr>
          <td colspan="2" valign="top"><%=(((Recordset1_data = Recordset1.getObject("introduction"))==null || Recordset1.wasNull())?"":Recordset1_data)%></td>
        </tr>
        <tr>
          <td colspan="2" valign="top"><%=(((Recordset1_data = Recordset1.getObject("country"))==null || Recordset1.wasNull())?"":Recordset1_data)%> | <%=(((Recordset1_data = Recordset1.getObject("city"))==null || Recordset1.wasNull())?"":Recordset1_data)%> | <%=(membersage)%> | <%=(((Recordset1_data = Recordset1.getObject("gender"))==null || Recordset1.wasNull())?"":Recordset1_data)%> | <%=(((Recordset1_data = Recordset1.getObject("weight"))==null || Recordset1.wasNull())?"":Recordset1_data)%> | <%=(((Recordset1_data = Recordset1.getObject("height"))==null || Recordset1.wasNull())?"":Recordset1_data)%> | <%=(((Recordset1_data = Recordset1.getObject("ethnic_origin"))==null || Recordset1.wasNull())?"":Recordset1_data)%></td>
        </tr>
      </table>
<%
  Repeat1__index++;
  Recordset1_hasData = Recordset1.next();
}
%>

<BR>
<table width="640" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td width="640" height="24" valign="top">&nbsp;</td>
  </tr>
</table>
<BR>
<!-- BEGIN Table containing google style navigation -->
<table width="640" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td width="640" height="19" valign="top">
      Results :
<%
TFM_middlePages = 20;
TFM_startLink = MM_offset/MM_size - (int)((double)(TFM_middlePages/2 + .5));
TFM_endLink = MM_offset/MM_size + (int)((double)(TFM_middlePages/2 + .5));
if (TFM_middlePages/2 != (int)((double)TFM_middlePages/2 + .5)) TFM_endLink = TFM_endLink + 1;
if (TFM_startLink < 0) {
   TFM_startLink = 0;
   TFM_endLink = TFM_middlePages;
}
TFM_LimitCounter = 0;
for (int i=1; i <= Recordset1_total;i = i+MM_size) {
  TFM_LimitCounter = TFM_LimitCounter + 1;
  TFM_LimitPageEndCount = i + MM_size - 1;
  if (TFM_LimitPageEndCount > Recordset1_total) {
    TFM_LimitPageEndCount = Recordset1_total;
    }
  if ((TFM_LimitCounter > TFM_startLink) && (TFM_LimitCounter <= TFM_endLink)) {
    if (i != MM_offset + 1) {
      out.print("<a href=" + '"' + MM_moveFirst + (i-1) + '"' + '>');
      out.print(TFM_LimitCounter + "</a>");
    }else{
      out.print("<b> " + TFM_LimitCounter + " </b>");
    }
    if((TFM_LimitPageEndCount != Recordset1_total) && ((int)(TFM_endLink) != TFM_LimitCounter)) out.print(" ");
  }
}
%>
 </td>
  </tr>
</table>
<!-- END Table containing google style navigation -->
<A HREF="<%=MM_moveNext%>">Next</A>
<A HREF="<%=MM_movePrev%>">Previous</A>

</body>
</html>
<%
Recordset1.close();
conn.close();
%>


Please help!! - When I remove the

<%
if (Recordset1_isEmpty) {
Recordset1.close();
conn.close();
response.sendRedirect("no_records.jsp");
} %>

portion of this code, it all works fine - with no deprecated api warning. So I assume it is the above code. This is the entire code for displaying profiles from a dating agency that I'm working on for a client.

Look forward to enlightenment!

Cheers
0
JamesFrogAuthor Commented:
.. note my JSP server is resin-pro-3.0.9... cheers
0
objectsCommented:
Have a look at the generated java code in _profileview__jsp.java to see exactly which line it is complaining about.

I'd also suggest adding a return after the sendRedirect() call.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JSP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.