You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Sabrina Staedt <Sa...@grayson-wireless.com> on 2000/08/30 21:03:34 UTC

new to tomcat

Hi.  I just recently installed jakarta-tomcat 3.1 on a RedHat6.2b machine
which is using Apache1.3.11 as it's web server, and has SDK1.2.2 installed.
I am new to web page development, and would appreciate it if anyone could
help me understand what is happening.
I have written a .jsp page that connects to a MySQL database that is on a
different machine than my Linux machine.  I want it to connect to the
database, query and display the data, allow the user to modify fields, and
when the user hits the submit button, update the database record.  After the
update, I want it to redirect to another page.   I've included the code for
my page at the end of this message.
I open the page in Explorer and the page displays fine.  I change a couple
values and hit submit, and the update reaches the database fine.  But there
seems to be a problem when it is done, and the next page is to be loaded.
What ends up being displayed is  a combination of my original page (called
debug.jsp, it is the page that sends the update), and the next page (called
sitePage.jsp, which is the page I want loaded after the update is finished)
.  It appears as if the top half of debug.jsp is cut off, and sitePage.jsp
is tacked on to the bottom of it.  If I hit the refresh button, then
sitePage.jsp is reloaded correctly and I can continue on.
Can anyone explain to me why this is happening, and how I could fix it?

Here is my code (generated with the help of Dreamweaver UtltraDev) :

<%@page language="java" import="java.sql.*"%> <% String MM_editAction; %> <%
// *** Update Record:
MM_editAction = request.getRequestURI();
if (request.getQueryString() != null && request.getQueryString().length() >
0) MM_editAction += "?" + request.getQueryString();
if (request.getParameter("MM_update") != null &&
    request.getParameter("MM_recordId") != null) {

  String MM_tableName = "GCC";
  String MM_tableCol = "cName";
  String MM_recordId = "'" + request.getParameter("MM_recordId") + "'";
  String MM_fields =
"tf1,gPN,none,none,NULL,tf2,gName,',none,'',tf3,lDFs,none,none,NULL,tf4,tSPB
lock,none,none,NULL,tf5,bDFproc,none,none,NULL,tf6,mBF,none,none,NULL,tf7,mS
DFrq,none,none,NULL,tf8,mxSDFrq,none,none,NULL,tf9,fRRec,none,none,NULL,tf10
,fEC,none,none,NULL,tf11,cT,none,none,NULL,tf12,vRate,',none,''";
  String MM_redirectPage =
"http://10.0.0.999/grayson/geometrix/database/sitePage.jsp";

  // create the field list array
  java.util.StringTokenizer tokens = new
java.util.StringTokenizer(MM_fields,",");
  String[] MM_fieldsList = new String[tokens.countTokens()];
  for (int i=0; tokens.hasMoreTokens(); i++) MM_fieldsList[i] =
tokens.nextToken();

  // create the update sql statement
  StringBuffer MM_updateStr = new StringBuffer("update
").append(MM_tableName).append(" set ");
  for (int i=0; i+4 < MM_fieldsList.length; i+=5) {
    String formVal =
((request.getParameter(MM_fieldsList[i])!=null)?(String)request.getParameter
(MM_fieldsList[i]):"");
    String delim =
(MM_fieldsList[i+2].compareTo("none")!=0)?MM_fieldsList[i+2]:"";
    String altVal =
(MM_fieldsList[i+3].compareTo("none")!=0)?MM_fieldsList[i+3]:"";
    String emptyVal =
(MM_fieldsList[i+4].compareTo("none")!=0)?MM_fieldsList[i+4]:"";
    if (formVal.length() == 0) {
      formVal = emptyVal;
    } else {
      if (altVal.length() != 0) {
        formVal = altVal;
      } else if (delim.compareTo("'") == 0) {  // escape quotes
        StringBuffer escQuotes = new StringBuffer(formVal);
        for (int j=0; j < escQuotes.length(); j++)
          if (escQuotes.charAt(j) == '\'') escQuotes.insert(j++,'\'');
        formVal = "'" + escQuotes + "'";
      } else {
        formVal = delim + formVal + delim;
      }
    }
    MM_updateStr.append((i!=0)?",":"").append(MM_fieldsList[i+1]).append(" =
").append(formVal);
  }
  MM_updateStr.append(" where ").append(MM_tableCol).append(" =
").append(MM_recordId);

  // finish the sql and execute it
  Driver MM_driver =
(Driver)Class.forName("org.gjt.mm.mysql.Driver").newInstance();
  Connection MM_connection =
DriverManager.getConnection("jdbc:mysql://myhost:3306/GDBpractice", "user1",
"pass1");
  PreparedStatement MM_updateStatement =
MM_connection.prepareStatement(MM_updateStr.toString());
  MM_updateStatement.setQueryTimeout(0);
  MM_updateStatement.executeUpdate();
  MM_connection.close();

  // redirect with URL parameters
  if (MM_redirectPage.length() == 0) MM_redirectPage =
request.getRequestURI();
  if (MM_redirectPage.indexOf('?') == -1 && request.getQueryString() !=
null)
    MM_redirectPage += "?" + request.getQueryString();
  response.sendRedirect(response.encodeRedirectURL(MM_redirectPage));
}
%> <%
Driver Driverrs1 =
(Driver)Class.forName("org.gjt.mm.mysql.Driver").newInstance();
Connection Connrs1 =
DriverManager.getConnection("jdbc:mysql://myhost:3306/GDBpractice", "user1",
"pass1");
PreparedStatement Statementrs1 = Connrs1.prepareStatement("SELECT *  FROM
GSCon  WHERE cName = 'current'");
Statementrs1.setQueryTimeout(0);
ResultSet rs1 = Statementrs1.executeQuery();
boolean rs1_isEmpty = !rs1.next();
boolean rs1_hasData = !rs1_isEmpty;
int rs1_numRows = 0;
%>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF">
Name: <%= ((rs1.getObject("cName")!=null)?rs1.getObject("cName"):"") %>
ID:<%= ((rs1.getObject("gId")!=null)?rs1.getObject("gId"):"") %>
<form name="form1" method="post" action="<%=MM_editAction%>">
  <p>label 1:
    <input type="text" name="tf1" value="<%=
((rs1.getObject("gPN")!=null)?rs1.getObject("gPN"):"") %>">
  </p>
  <p>label 2:
    <input type="text" name="tf2" value="<%=
((rs1.getObject("gName")!=null)?rs1.getObject("gName"):"") %>">
  </p>
  <p>label 3:
    <input type="text" name="tf3" value="<%=
((rs1.getObject("lDFs")!=null)?rs1.getObject("lDFs"):"") %>">
  </p>
  <p>label 4:
    <input type="text" name="tf4" value="<%=
((rs1.getObject("tSPBlock")!=null)?rs1.getObject("tSPBlock"):"") %>">
  </p>
  <p>label 5:
    <input type="text" name="tf5" value="<%=
((rs1.getObject("bDFproc")!=null)?rs1.getObject("bDFproc"):"") %>">
  </p>
  <p>label 6:
    <input type="text" name="tf6" value="<%=
((rs1.getObject("mBF")!=null)?rs1.getObject("mBF"):"") %>">
  </p>
  <p> label 7:
    <input type="text" name="tf7" value="<%=
((rs1.getObject("mSDFrq")!=null)?rs1.getObject("mSDFrq"):"") %>">
  </p>
  <p>label 8:
    <input type="text" name="tf8" value="<%=
((rs1.getObject("mxSDFrq")!=null)?rs1.getObject("mxSDFrq"):"") %>">
  </p>
  <p>label 9:
    <input type="text" name="tf9" value="<%=
((rs1.getObject("fRRec")!=null)?rs1.getObject("fRRec"):"") %>">
  </p>
  <p>label 10:
    <input type="text" name="tf10" value="<%=
((rs1.getObject("fEC")!=null)?rs1.getObject("fEC"):"") %>">
  </p>
  <p>label 11:
    <input type="text" name="tf11" value="<%=
((rs1.getObject("cT")!=null)?rs1.getObject("cT"):"") %>">
  </p>
  <p>label 12:
    <input type="text" name="tf12" value="<%=
((rs1.getObject("vRate")!=null)?rs1.getObject("vRate"):"") %>">
  </p>
  <p>
    <input type="submit" name="Submit" value="Submit">
  </p>
  <input type="hidden" name="MM_recordId" value="<%=
((rs1.getObject("cName")!=null)?rs1.getObject("cName"):"") %>">
  <input type="hidden" name="MM_update" value="true">
</form>
</body>
</html>
<%
rs1.close();
Connrs1.close();
%>

Sabrina Staedt

Grayson Wireless    e-mail: Sabrina_Staedt@grayson-wireless.com

381 Elden St.         Voice : 703-787-7944 x113

Suite 1100              Fax : 703-787-8007

Herndon, VA 20170                  http://www.geometrix911.com



Re: new to tomcat

Posted by Ben DeBiase <db...@toast.net>.
Hi,

Using the <jsp:forward> action instead of the "sendRedirect" may solve
your problem. Simply replacing:
    response.sendRedirect(response.encodeRedirectURL(MM_redirectPage));
with
    %><jsp:forward page="<%=MM_redirectPage %>" /><%
should do the trick. Please note that <jsp:forward> immediately stops
executing the current page and starts processing the next one.

Ben.



Sabrina Staedt wrote:

> Hi.  I just recently installed jakarta-tomcat 3.1 on a RedHat6.2b
> machine which is using Apache1.3.11 as it's web server, and has
> SDK1.2.2 installed.    I am new to web page development, and would
> appreciate it if anyone could help me understand what is happening. I
> have written a .jsp page that connects to a MySQL database that is on
> a different machine than my Linux machine.  I want it to connect
> to the database, query and display the data, allow the user to modify
> fields, and when the user hits the submit button, update the database
> record.  After the update, I want it to redirect to another page. I've
> included the code for my page at the end of this message.I open the
> page in Explorer and the page displays fine.  I change a couple values
> and hit submit, and the update reaches the database fine.  But there
> seems to be a problem when it is done, and the next page is to be
> loaded.  What ends up being displayed is  a combination of my original
> page (called debug.jsp, it is the page that sends the update), and the
> next page (called sitePage.jsp, which is the page I want loaded after
> the update is finished) .  It appears as if the top half of debug.jsp
> is cut off, and sitePage.jsp is tacked on to the bottom of it.  If I
> hit the refresh button, then sitePage.jsp is reloaded correctly and I
> can continue on. Can anyone explain to me why this is happening, and
> how I could fix it?Here is my code (generated with the help of
> Dreamweaver UtltraDev) :<%@page language="java" import="java.sql.*"%>
> <% String MM_editAction; %> <%
> // *** Update Record:
> MM_editAction = request.getRequestURI();
> if (request.getQueryString() != null &&
> request.getQueryString().length() > 0) MM_editAction += "?" +
> request.getQueryString();
> if (request.getParameter("MM_update") != null &&
>     request.getParameter("MM_recordId") != null) {   String
> MM_tableName = "GCC";
>   String MM_tableCol = "cName";
>   String MM_recordId = "'" + request.getParameter("MM_recordId") +
> "'";
>   String MM_fields =
> "tf1,gPN,none,none,NULL,tf2,gName,',none,'',tf3,lDFs,none,none,NULL,tf4,tSPBlock,none,none,NULL,tf5,bDFproc,none,none,NULL,tf6,mBF,none,none,NULL,tf7,mSDFrq,none,none,NULL,tf8,mxSDFrq,none,none,NULL,tf9,fRRec,none,none,NULL,tf10,fEC,none,none,NULL,tf11,cT,none,none,NULL,tf12,vRate,',none,''";
>
>   String MM_redirectPage =
> "http://10.0.0.999/grayson/geometrix/database/sitePage.jsp";   //
> create the field list array
>   java.util.StringTokenizer tokens = new
> java.util.StringTokenizer(MM_fields,",");
>   String[] MM_fieldsList = new String[tokens.countTokens()];
>   for (int i=0; tokens.hasMoreTokens(); i++) MM_fieldsList[i] =
> tokens.nextToken();   // create the update sql statement
>   StringBuffer MM_updateStr = new StringBuffer("update
> ").append(MM_tableName).append(" set ");
>   for (int i=0; i+4 < MM_fieldsList.length; i+=5) {
>     String formVal =
> ((request.getParameter(MM_fieldsList[i])!=null)?(String)request.getParameter(MM_fieldsList[i]):"");
>
>     String delim =
> (MM_fieldsList[i+2].compareTo("none")!=0)?MM_fieldsList[i+2]:"";
>     String altVal =
> (MM_fieldsList[i+3].compareTo("none")!=0)?MM_fieldsList[i+3]:"";
>     String emptyVal =
> (MM_fieldsList[i+4].compareTo("none")!=0)?MM_fieldsList[i+4]:"";
>     if (formVal.length() == 0) {
>       formVal = emptyVal;
>     } else {
>       if (altVal.length() != 0) {
>         formVal = altVal;
>       } else if (delim.compareTo("'") == 0) {  // escape quotes
>         StringBuffer escQuotes = new StringBuffer(formVal);
>         for (int j=0; j < escQuotes.length(); j++)
>           if (escQuotes.charAt(j) == '\'') escQuotes.insert(j++,'\'');
>
>         formVal = "'" + escQuotes + "'";
>       } else {
>         formVal = delim + formVal + delim;
>       }
>     }
>
> MM_updateStr.append((i!=0)?",":"").append(MM_fieldsList[i+1]).append("
> = ").append(formVal);
>   }
>   MM_updateStr.append(" where ").append(MM_tableCol).append(" =
> ").append(MM_recordId);   // finish the sql and execute it
>   Driver MM_driver =
> (Driver)Class.forName("org.gjt.mm.mysql.Driver").newInstance();
>   Connection MM_connection =
> DriverManager.getConnection("jdbc:mysql://myhost:3306/GDBpractice",
> "user1", "pass1");
>   PreparedStatement MM_updateStatement =
> MM_connection.prepareStatement(MM_updateStr.toString());
>   MM_updateStatement.setQueryTimeout(0);
>   MM_updateStatement.executeUpdate();
>   MM_connection.close();   // redirect with URL parameters
>   if (MM_redirectPage.length() == 0) MM_redirectPage =
> request.getRequestURI();
>   if (MM_redirectPage.indexOf('?') == -1 && request.getQueryString()
> != null)
>     MM_redirectPage += "?" + request.getQueryString();
>   response.sendRedirect(response.encodeRedirectURL(MM_redirectPage));
> }
> %> <%
> Driver Driverrs1 =
> (Driver)Class.forName("org.gjt.mm.mysql.Driver").newInstance();
> Connection Connrs1 =
> DriverManager.getConnection("jdbc:mysql://myhost:3306/GDBpractice",
> "user1", "pass1");
> PreparedStatement Statementrs1 = Connrs1.prepareStatement("SELECT *
> FROM GSCon  WHERE cName = 'current'");
> Statementrs1.setQueryTimeout(0);
> ResultSet rs1 = Statementrs1.executeQuery();
> boolean rs1_isEmpty = !rs1.next();
> boolean rs1_hasData = !rs1_isEmpty;
> int rs1_numRows = 0;
> %>
> <html>
> <head>
> <title>Untitled Document</title>
> <meta http-equiv="Content-Type" content="text/html;
> charset=iso-8859-1">
> </head>
> <body bgcolor="#FFFFFF">
> Name: <%= ((rs1.getObject("cName")!=null)?rs1.getObject("cName"):"")
> %>
> ID:<%= ((rs1.getObject("gId")!=null)?rs1.getObject("gId"):"") %>
> <form name="form1" method="post" action="<%=MM_editAction%>">
>   <p>label 1:
>     <input type="text" name="tf1" value="<%=
> ((rs1.getObject("gPN")!=null)?rs1.getObject("gPN"):"") %>">
>   </p>
>   <p>label 2:
>     <input type="text" name="tf2" value="<%=
> ((rs1.getObject("gName")!=null)?rs1.getObject("gName"):"") %>">
>   </p>
>   <p>label 3:
>     <input type="text" name="tf3" value="<%=
> ((rs1.getObject("lDFs")!=null)?rs1.getObject("lDFs"):"") %>">
>   </p>
>   <p>label 4:
>     <input type="text" name="tf4" value="<%=
> ((rs1.getObject("tSPBlock")!=null)?rs1.getObject("tSPBlock"):"") %>">
>   </p>
>   <p>label 5:
>     <input type="text" name="tf5" value="<%=
> ((rs1.getObject("bDFproc")!=null)?rs1.getObject("bDFproc"):"") %>">
>   </p>
>   <p>label 6:
>     <input type="text" name="tf6" value="<%=
> ((rs1.getObject("mBF")!=null)?rs1.getObject("mBF"):"") %>">
>   </p>
>   <p> label 7:
>     <input type="text" name="tf7" value="<%=
> ((rs1.getObject("mSDFrq")!=null)?rs1.getObject("mSDFrq"):"") %>">
>   </p>
>   <p>label 8:
>     <input type="text" name="tf8" value="<%=
> ((rs1.getObject("mxSDFrq")!=null)?rs1.getObject("mxSDFrq"):"") %>">
>   </p>
>   <p>label 9:
>     <input type="text" name="tf9" value="<%=
> ((rs1.getObject("fRRec")!=null)?rs1.getObject("fRRec"):"") %>">
>   </p>
>   <p>label 10:
>     <input type="text" name="tf10" value="<%=
> ((rs1.getObject("fEC")!=null)?rs1.getObject("fEC"):"") %>">
>   </p>
>   <p>label 11:
>     <input type="text" name="tf11" value="<%=
> ((rs1.getObject("cT")!=null)?rs1.getObject("cT"):"") %>">
>   </p>
>   <p>label 12:
>     <input type="text" name="tf12" value="<%=
> ((rs1.getObject("vRate")!=null)?rs1.getObject("vRate"):"") %>">
>   </p>
>   <p>
>     <input type="submit" name="Submit" value="Submit">
>   </p>
>   <input type="hidden" name="MM_recordId" value="<%=
> ((rs1.getObject("cName")!=null)?rs1.getObject("cName"):"") %>">
>   <input type="hidden" name="MM_update" value="true">
> </form>
> </body>
> </html>
> <%
> rs1.close();
> Connrs1.close();
> %>Sabrina Staedt
>
> Grayson Wireless    e-mail: Sabrina_Staedt@grayson-wireless.com
>
> 381 Elden St.         Voice : 703-787-7944 x113
>
> Suite 1100              Fax : 703-787-8007
>
> Herndon, VA 20170                  http://www.geometrix911.com