?
Solved

Urgent: JSP & redirecting

Posted on 2003-03-18
33
Medium Priority
?
341 Views
Last Modified: 2010-04-01
Hello All

I am trying to register a user if they are a user or a author that exist in my database already, so they dont need to register. Therefore I can redirect them to a error page or if they are successful they get redirect to successful registered page.

Technologies I am using are:

1. Tomcat 4.0.4
2. MySQL 3.23.52
3. JSP 1.2
4. JPadpro (editor)

Here is the Java class: Register2.java

import java.util.*;
import java.sql.*;

public class Register2 {
      private String bnFirstname = "un-named";
      private String bnSurname = "no-id";
      private String bnEmail = "nothing";
      private String bnPassword = "no-id";
      private String bnFaculty = "nothing";
      private String bnDbDriver = "org.gjt.mm.mysql.Driver";
      private String bnDbId = "jdbc:mysql://localhost/itx98jkdb";
      private String bnDbUser = "username";
      private String bnDbPassword = "Password";
      private int bnUserId;
      private int bnAuthorId;
      private int bnSecurityId = 1;
        private String returnAdd = "none";
      
      
      
      public void setBnFirstname(String argFirstname)
      {
            bnFirstname = argFirstname;
            System.out.println("Arg=" + argFirstname + "bn = " + bnFirstname);
      }
      
      public void setBnSurname(String argSurname)
      {
            bnSurname = argSurname.trim();
      }
      
      
      public void setBnEmail(String argEmail)
      {
            bnEmail = argEmail.trim();
            System.out.println("Arg=" + argEmail + "bn = " + argEmail);
      }
      
      
      
      public void setBnPassword(String argPassword)
      {
            bnPassword = argPassword.trim();
      }
      
      
      public void setBnFaculty(String argFaculty)
      {
            bnFaculty = argFaculty.trim();
      }
      
      
      public void setBnUserId(int argUserId)
      {
            bnUserId = argUserId;
      }

      
      public void setBnAuthorId(int argAuthorId)
      {
            bnAuthorId = argAuthorId;
      }
      
      public void setBnSecurityId(int argSecurityId)
      {
      
            bnSecurityId = argSecurityId;
      }
      
      
      public String getFirstName()
      {
            return firstName;
      }
      
      public String getLastName()
      {
            return lastName;
      }


/* See if the user exists  */
      public boolean isUser()
      {
            String returnString = "";
            boolean isUser = false;
                  try
                  {
                        //*********************
                        // get the connection
                        //*********************
                        Class.forName(bnDbDriver);
                        Connection con = DriverManager.getConnection(bnDbId, bnDbUser, bnDbPassword);
                        
                        
                        //*********************
                        // build the sql query
                        //*********************
                        Statement myStatement = con.createStatement();
                        String sqlQuery = "SELECT * FROM User WHERE dbUserId = '" + bnUserId + "'";
                        ResultSet myResultSet = myStatement.executeQuery(sqlQuery);
                        
                        
                        //********************
                        // do the sql query
                        //********************
                        if (myResultSet.next())
                        {
                              isUser = true;
                        }
                        else
                        {
                              return isUser;
                              }
                        
                  }
            
                  catch (Exception e)
                        
                  {
                        System.out.println("Register.CheckUser()");
                  }
            return isUser;
      }
      

         //To see if the author exists
      public boolean isAuthor()
      {
      boolean isAuthor = false;
            try
            {
                  //*********************
                  // get the connection
                  //*********************
                  Class.forName(bnDbDriver);
                  Connection con = DriverManager.getConnection(bnDbId, bnDbUser, bnDbPassword);
      
      
                  //*********************
                  // build the sql query
                  //*********************
                  Statement myStatement = con.createStatement();
                  String sqlQuery = "SELECT * FROM Author WHERE dbAuthorId = '" + bnAuthorId + "'";
                  ResultSet myResultSet = myStatement.executeQuery(sqlQuery);
      
      
                  //********************
                  // do the sql query
                  //********************
                  if (myResultSet.next())
                  {
                        isAuthor = true;
                  }
                  else
                  {
                        return isAuthor;
                  }
      
            }

            catch (Exception e)
      
            {
                  System.out.println("Register.CheckUser()");
            }
            return isAuthor;
      }


        //Add user to the DB
      public String addtoDb()
      {
            
            try
            {
                  //*********************
                  // get the connection
                  //*********************
                  Class.forName(bnDbDriver);
                  Connection con = DriverManager.getConnection(bnDbId, bnDbUser, bnDbPassword);
                  
            
                  
                  //*********************
                  // do the sql query
                  //*********************
                  Statement myStatement = con.createStatement();
                  
                  //if((this.isAuthor())||(this.isUser()))
                  //{
                        //returnAdd = "User Already Exists";
                  //}
                  //else
                  //{
                        String sqlQuery = "INSERT INTO User (dbFirstNames,dbSurName,dbPassword,dbEmail,dbFaculty,dbSecurityId) VALUES (?,?,?,?,?,?)";
                        PreparedStatement ps = con.prepareStatement( sqlQuery );
                        ps.setString(1, bnFirstname );
                        ps.setString(2, bnSurname );
                        ps.setString(3, bnPassword );
                        ps.setString(4, bnEmail );
                        ps.setString(5, bnFaculty );
                        ps.setInt(6, bnSecurityId );
                        ps.executeUpdate();
                        returnAdd="Successfully Added";
                        ps.close();
                        
                        
                  //}
            }
            catch (Exception e)
            {
                  System.out.println("Register2.addtoDb():" + e);
                  return "failed." + e;
      
            }
             return returnAdd;

      }

}


****************************************************************************************

My JSP page: Registration.jsp


/* See if the user exists  */
      public boolean isUser()
      {
            String returnString = "";
            boolean isUser = false;
                  try
                  {
                        //*********************
                        // get the connection
                        //*********************
                        Class.forName(bnDbDriver);
                        Connection con = DriverManager.getConnection(bnDbId, bnDbUser, bnDbPassword);
                        
                        
                        //*********************
                        // build the sql query
                        //*********************
                        Statement myStatement = con.createStatement();
                        String sqlQuery = "SELECT * FROM User WHERE dbUserId = '" + bnUserId + "'";
                        ResultSet myResultSet = myStatement.executeQuery(sqlQuery);
                        
                        
                        //********************
                        // do the sql query
                        //********************
                        if (myResultSet.next())
                        {
                              isUser = true;
                        }
                        else
                        {
                              return isUser;
                              }
                        
                  }
            
                  catch (Exception e)
                        
                  {
                        System.out.println("Register.CheckUser()");
                  }
            return isUser;
      }
      


      public boolean isAuthor()
      {
      boolean isAuthor = false;
            try
            {
                  //*********************
                  // get the connection
                  //*********************
                  Class.forName(bnDbDriver);
                  Connection con = DriverManager.getConnection(bnDbId, bnDbUser, bnDbPassword);
      
      
                  //*********************
                  // build the sql query
                  //*********************
                  Statement myStatement = con.createStatement();
                  String sqlQuery = "SELECT * FROM Author WHERE dbAuthorId = '" + bnAuthorId + "'";
                  ResultSet myResultSet = myStatement.executeQuery(sqlQuery);
      
      
                  //********************
                  // do the sql query
                  //********************
                  if (myResultSet.next())
                  {
                        isAuthor = true;
                  }
                  else
                  {
                        return isAuthor;
                  }
      
            }

            catch (Exception e)
      
            {
                  System.out.println("Register.CheckUser()");
            }
            return isAuthor;
      }



      public String addtoDb()
      {
            
            try
            {
                  //*********************
                  // get the connection
                  //*********************
                  Class.forName(bnDbDriver);
                  Connection con = DriverManager.getConnection(bnDbId, bnDbUser, bnDbPassword);
                  
            
                  
                  //*********************
                  // do the sql query
                  //*********************
                  Statement myStatement = con.createStatement();
                  
                  //if((this.isAuthor())||(this.isUser()))
                  //{
                        //returnAdd = "User Already Exists";
                  //}
                  //else
                  //{
                        String sqlQuery = "INSERT INTO User (dbFirstNames,dbSurName,dbPassword,dbEmail,dbFaculty,dbSecurityId) VALUES (?,?,?,?,?,?)";
                        PreparedStatement ps = con.prepareStatement( sqlQuery );
                        ps.setString(1, bnFirstname );
                        ps.setString(2, bnSurname );
                        ps.setString(3, bnPassword );
                        ps.setString(4, bnEmail );
                        ps.setString(5, bnFaculty );
                        ps.setInt(6, bnSecurityId );
                        ps.executeUpdate();
                        returnAdd="Successfully Added";
                        ps.close();
                        
                        
                  //}
            }
            catch (Exception e)
            {
                  System.out.println("Register2.addtoDb():" + e);
                  return "failed." + e;
      
            }
             return returnAdd;

      }

}

I would be garateful if anyone can help or give some guidance or tips on what to do.
Futhermore I am receiving these errors from Tomcat:

An error occurred between lines: 73 and 76 in the jsp file: /petshow/jsp/Registration.jsp

Generated servlet error:
C:\j3t\tomcat\work\Standalone\localhost\yourid\petshow\jsp\Registration$jsp.java:147: Missing term.
                out.print( if((myRegister.isAuthor())||(myRegister.isUser())==true) {
                          ^


An error occurred between lines: 73 and 76 in the jsp file: /petshow/jsp/Registration.jsp

Generated servlet error:
C:\j3t\tomcat\work\Standalone\localhost\yourid\petshow\jsp\Registration$jsp.java:147: ')' expected.
                out.print( if((myRegister.isAuthor())||(myRegister.isUser())==true) {
                          ^


An error occurred between lines: 76 and 165 in the jsp file: /petshow/jsp/Registration.jsp

Generated servlet error:
C:\j3t\tomcat\work\Standalone\localhost\yourid\petshow\jsp\Registration$jsp.java:150: ';' expected.
                     myRegister.addtoDb());
                                         ^
3 errors, 1 warning


Thanks again
Juggy
0
Comment
Question by:Juggy1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 17
  • 14
  • +1
33 Comments
 

Author Comment

by:Juggy1
ID: 8161368
Sorry folks

Wrong file for JSP, here it is:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<%@ page errorPage="Erroreg.jsp" %>

<jsp:useBean
      id="myRegister"
      scope="session"
      class="petshowbeans.Register2"
      />


<jsp:setProperty
      name="myRegister"
      property="bnUserId"
      param="frmUserId"
      />


<jsp:setProperty
      name="myRegister"
      property="bnFirstname"
      param="frmFirstnames"
      />

<jsp:setProperty
      name="myRegister"
      property="bnSurname"
      param="frmSurname"
      />


<jsp:setProperty
      name="myRegister"
      property="bnEmail"
      param="frmEmail"
      />

<jsp:setProperty
      name="myRegister"
      property="bnPassword"
      param="frmPassword"
      />

<jsp:setProperty
      name="myRegister"
      property="bnFaculty"
      param="frmFaculty"
      />



<jsp:setProperty
      name="myRegister"
      property="bnAuthorId"
      param="frmAuthorId"
      />

<jsp:setProperty
      name="myRegister"
      property="bnSecurityId"
      param="frmSecurityId"
      />

      


<html>
      <head>
            <title>Registration Page</title>
      </head>
      <body>
            <p><%= if((myRegister.isAuthor())||(myRegister.isUser())==true) {
                              response.sendRedirect("Erroreg.jsp");
                                 return; }
                     myRegister.addtoDb()%></p>
            <form method="post" action="Registration.jsp">
                  <center>
                  <table cellpadding=4 cellspacing=2 border=0 width="70%">
                        <th bgcolor="#99FF66" colspan=2>
                        <font size=5>USER REGISTRATION</font>
                        <font size=1><sup>*</sup> Required Fields</font>
                        </th>
                  
                        <tr bgcolor="#99CC66">
                              <td valign=top width="50%">
                                    <b>First name<sup>*</sup></b>
                                    <br>      
                                    <input type="text" name="frmFirstnames" size=22 maxlength=50>
                                    </td>

                              <td valign=top width="50%">
                                    <b>Surname<sup>*</sup></b>
                                    <br>      
                                    <input type="text" name="frmSurname" size=22 maxlength=50>
                                    </td>
                        </tr>
            
                        
                        
                        <tr bgcolor="#99CC66">
                                    <td valign=top>
                                          <b>Email<sup>*</sup></b>
                                          <br>
                                          <input type="text" name="frmEmail" size=22 maxlength=50>
                        </td>
      
                              
                              
                        <td valign="top">
                                    <b>Password<sup>*</sup></b>
                                    <br>
                                    <input type="password" name="frmPassword" size=10 maxlength=10>
                              </td>
                        </tr>      
                        

                              
                        
                        <tr bgcolor="#99CC66">
                              <td align=center colspan="2">
                                    <b>Faculty<sup>*</sup></b>
                                    <br>
                                    <select name="frmFaculty" >
                                          <option></option>
                                          <option>Computing Sciences</option>
                                          <option>Engineering & Technology</option>
                                    </select>
                              </td>
                        </tr>      

                        
                              <tr bgcolor="#99CC66">
                                    <td  align=center colspan="2">
                                          <input type="submit" value="Submit">
                                          <input type="reset"  value="Reset">
                                    </td>
                              </tr>
                  </table>
            </form>
      </body>
</html>





















      
      
0
 
LVL 3

Expert Comment

by:allahabad
ID: 8161371
1. out.print( if((myRegister.isAuthor())||(myRegister.isUser())==true)
This is boolean, its not a value to print.

2.  myRegister.addtoDb());
       will be :
    myRegister.addtoDb();
                                     
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 8161420
<p>
<%
if((myRegister.isAuthor())||(myRegister.isUser())==true) {
      response.sendRedirect("Erroreg.jsp");
      return;
}
myRegister.addtoDb()
%>
</p>
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Expert Comment

by:kennethxu
ID: 8161810
change this part:

          <p><%= if((myRegister.isAuthor())||(myRegister.isUser())==true) {
                         response.sendRedirect("Erroreg.jsp");
                            return; }
                   myRegister.addtoDb()%></p>

to

<%
          if((myRegister.isAuthor())||(myRegister.isUser())==true) {
                         response.sendRedirect("Erroreg.jsp");
                         return;
          }
%>
          <p><%=myRegister.addtoDb()%></p>

points goes to bobbit.
0
 

Author Comment

by:Juggy1
ID: 8169739
Thanks all for the answers...

Just one thing, with my code:

<% if((myRegister.isAuthor()||myRegister.isUser()) == true) {
        response.sendRedirect("Erroreg.jsp");
        return;
   }
%>

   <p><%=myRegister.addtoDb()%></p>

As you know it can check if there is exisitng users or authors which is good.
How could I insert data into my Db and redirect the page to state successful registration?

I would be greateful for any tipr or suggestions?

Thanks again all
Juggy
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 8170115
you mean <p><%=myRegister.addtoDb()%></p> isn't getting called?
0
 

Author Comment

by:Juggy1
ID: 8172280
Hello Bobbit31

Thanks for the quick reply, the question was:

I know I can set a condition to see if users or authors exist and redirect them to an eror jsp page.

But now what I want to do is add to my DB and redirect the user to another jsp page, to say they have been successful.

e.g. success.jsp should work if the user has inserted data and  is not an exisiting person, therefore should be redirected to this jsp page ("success.jsp").

Thanks
Juggy
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 8174325
so from the error page you want to register them and then redirect to success.jsp?
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 8174457
ok, i looked at your code more closely and i now understand what you want... one second

0
 
LVL 18

Expert Comment

by:bobbit31
ID: 8174531
make addToDb return boolean something like this:

       //Add user to the DB
     public boolean addtoDb()
     {

// BEFORE YOU DO THIS CHECK TO MAKE SURE YOUR VALUES HAVE BEEN SET SO YOU DON'T ENTER
// ERRONEOUS INFO TO DB... IE:
if (bnFirstname.equals("un-named")) return false;
         
          try
          {
               //*********************
               // get the connection
               //*********************
               Class.forName(bnDbDriver);
               Connection con = DriverManager.getConnection(bnDbId, bnDbUser, bnDbPassword);
               
         
               
               //*********************
               // do the sql query
               //*********************
               Statement myStatement = con.createStatement();
               
               //if((this.isAuthor())||(this.isUser()))
               //{
                    //returnAdd = "User Already Exists";
               //}
               //else
               //{
                             
                    String sqlQuery = "INSERT INTO User (dbFirstNames,dbSurName,dbPassword,dbEmail,dbFaculty,dbSecurityId) VALUES (?,?,?,?,?,?)";
                    PreparedStatement ps = con.prepareStatement( sqlQuery );
                    ps.setString(1, bnFirstname );
                    ps.setString(2, bnSurname );
                    ps.setString(3, bnPassword );
                    ps.setString(4, bnEmail );
                    ps.setString(5, bnFaculty );
                    ps.setInt(6, bnSecurityId );
                    ps.executeUpdate();
                    ps.close();
                   
                   
               //}
          }
          catch (Exception e)
          {
               System.out.println("Register2.addtoDb():" + e);
               return "failed." + e;
     
          }
           return true;

     }
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 8174552
in your jsp:

<%
if((myRegister.isAuthor())||(myRegister.isUser())==true) {
     response.sendRedirect("Erroreg.jsp");
     return;
} else if (myRegister.addToDb()) {
     response.sendRedirect("Success.jsp");
     return;
}
%>

so basically, if the user is an author or is a user it will go to Errorreg.jsp.

if the user just registered and it was a success it will go to Success.jsp

otherwise it will show the registration page.

good luck!
0
 

Author Comment

by:Juggy1
ID: 8175287
Thanks Bobbit31 for the help.

I have tried the code and made the changes you suggested, but Tomcat is giving me these errors:

An error occurred between lines: 73 and 81 in the jsp file: /petshow/jsp/Registration.jsp

Generated servlet error:
C:\j3t\tomcat\work\Standalone\localhost\yourid\petshow\jsp\Registration$jsp.java:147: Missing term.
                out.print( if((myRegister.isAuthor())||(myRegister.isUser())==true) {
                          ^


An error occurred between lines: 73 and 81 in the jsp file: /petshow/jsp/Registration.jsp

Generated servlet error:
C:\j3t\tomcat\work\Standalone\localhost\yourid\petshow\jsp\Registration$jsp.java:147: ')' expected.
                out.print( if((myRegister.isAuthor())||(myRegister.isUser())==true) {
                          ^

C:\j3t\tomcat\work\Standalone\localhost\yourid\petshow\jsp\Registration$jsp.java:154: Missing term.
               }
                ^

C:\j3t\tomcat\work\Standalone\localhost\yourid\petshow\jsp\Registration$jsp.java:154: ';' expected.
               }
                ^

C:\j3t\tomcat\work\Standalone\localhost\yourid\petshow\jsp\Registration$jsp.java:155: Invalid expression statement.
          );
          ^
5 errors, 1 warning

I am unable to see where the errors are coming from, but I think its to with my 'try and catch' block and when I caught  the exception I do "System.out.println" is this correct or is ther another cause to my problem and how would I solve it?

I would be grateful if you can give me further solutuions to solve this.

Thanks again
Juggy





0
 
LVL 18

Expert Comment

by:bobbit31
ID: 8175345
don't use <%= %>, use <% %>

<%= %> is a shortcut for <% out.print("..."); %>

if this doesn't solve the problem, please post the code for your jsp again
0
 

Author Comment

by:Juggy1
ID: 8176789

Thanks again for your help.
The change in the JSP scriplet made a difference.

But now its not fuunctioning properly, the problems are:

1. When I did a test of entering an existing user details in the registration form, then submitted it should of sent me to the error page. But it didn't it just added these details in the user table, which is wrong.

2. Same test again for the author and same thing happened again.

3. Once the above actions were done, I hit the back button in browser to go to my directory and start again.
But when I click on my link to the 'Registration.jsp' it directs me to my 'success2.jsp'.

Again I'm slighty unsure what's happening, please could you give a tip or suggestion I could use.

Here is the Registration.jsp code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<%@ page errorPage="Erroreg.jsp" %>

<jsp:useBean
      id="myRegister"
      scope="session"
      class="petshowbeans.Register2"
      />


<jsp:setProperty
      name="myRegister"
      property="bnUserId"
      param="frmUserId"
      />


<jsp:setProperty
      name="myRegister"
      property="bnFirstname"
      param="frmFirstnames"
      />

<jsp:setProperty
      name="myRegister"
      property="bnSurname"
      param="frmSurname"
      />


<jsp:setProperty
      name="myRegister"
      property="bnEmail"
      param="frmEmail"
      />

<jsp:setProperty
      name="myRegister"
      property="bnPassword"
      param="frmPassword"
      />

<jsp:setProperty
      name="myRegister"
      property="bnFaculty"
      param="frmFaculty"
      />



<jsp:setProperty
      name="myRegister"
      property="bnAuthorId"
      param="frmAuthorId"
      />

<jsp:setProperty
      name="myRegister"
      property="bnSecurityId"
      param="frmSecurityId"
      />

      


<html>
      <head>
            <title>Registration Page</title>
      </head>
      <body>
            <% if((myRegister.isAuthor())||(myRegister.isUser())==true) {
                              response.sendRedirect("Erroreg.jsp");
                                 return;
               }else if(myRegister.addtoDb()) {
                              response.sendRedirect("success2.jsp");
                        return;
               }
            %>
            <form method="post" action="Registration.jsp">
                  <center>
                  <table cellpadding=4 cellspacing=2 border=0 width="70%">
                        <th bgcolor="#99FF66" colspan=2>
                        <font size=5>USER REGISTRATION</font>
                        <font size=1><sup>*</sup> Required Fields</font>
                        </th>
                  
                        <tr bgcolor="#99CC66">
                              <td valign=top width="50%">
                                    <b>First name<sup>*</sup></b>
                                    <br>      
                                    <input type="text" name="frmFirstnames" size=22 maxlength=50>
                                    </td>

                              <td valign=top width="50%">
                                    <b>Surname<sup>*</sup></b>
                                    <br>      
                                    <input type="text" name="frmSurname" size=22 maxlength=50>
                                    </td>
                        </tr>
            
                        
                        
                        <tr bgcolor="#99CC66">
                                    <td valign=top>
                                          <b>Email<sup>*</sup></b>
                                          <br>
                                          <input type="text" name="frmEmail" size=22 maxlength=50>
                        </td>
      
                              
                              
                        <td valign="top">
                                    <b>Password<sup>*</sup></b>
                                    <br>
                                    <input type="password" name="frmPassword" size=10 maxlength=10>
                              </td>
                        </tr>      
                        

                              
                        
                        <tr bgcolor="#99CC66">
                              <td align=center colspan="2">
                                    <b>Faculty<sup>*</sup></b>
                                    <br>
                                    <select name="frmFaculty" >
                                          <option></option>
                                          <option>Computing Sciences</option>
                                          <option>Engineering & Technology</option>
                                    </select>
                              </td>
                        </tr>      

                        
                              <tr bgcolor="#99CC66">
                                    <td  align=center colspan="2">
                                          <input type="submit" value="Submit">
                                          <input type="reset"  value="Reset">
                                    </td>
                              </tr>
                  </table>
            </form>
      </body>
</html>

*************************************************************

Here is the success2.jsp code to state successful registration:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">



<jsp:useBean
      id="myRegister"
      class="petshowbeans.Register2"
      scope="session"
/>

<html>
      <body>
            <center>
                  <table cellpadding=4 cellspacing=2 border=0>
                        
                        <th bgcolor="#99FF66" colspan=2>
                              <font size=5>USER REGISTRATION SUCCESSFUL!</font>
                        </th>
                        
                        <font size=4>
                              <tr bgcolor="#99CC66">
                                    <td valign=top>
                                          <b>First Name</b>
                                          <br>
                                          <jsp:getProperty name="myRegister" property="firstName"/>
                                    </td>
                                    
                                    <td valign=top>
                                          <b>Last Name</b>
                                          <br>
                                          <jsp:getProperty name="myRegister" property="lastName"/>
                                    </td>
                              </tr>
                        </font>
                  </table>
            </center>
      </body>
</html>
                  

***************************************************************
Here is my Errorpage for Errorreg.jsp code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
      <head>
            <title>Error Page For Examples</title>
      </head>
      <body bgcolor="white">
            <%@ page isErrorPage="true" %>
            Invalid user and/or, please try
            <a href='<%= response.encodeURL("Registration.jsp") %>'>again</a>.
      </body>
</html>


I would be grateful again if you could give advice or a suggestion.

Thanks again
Juggy
0
 
LVL 18

Accepted Solution

by:
bobbit31 earned 390 total points
ID: 8177031
ok, let's put in some System.outs so we can see what's going on:

<% if(myRegister.isAuthor()||myRegister.isUser()) {
System.out.println("Do this 1");
                         response.sendRedirect("Erroreg.jsp");
                            return;
             }else if(myRegister.addtoDb()) {
System.out.println("Do this 2");
                         response.sendRedirect("success2.jsp");
                      return;
             } else {
System.out.println("Do this 3");
             }
%>

ok, please tell me what it's displaying in the following situations:

1) first time to hit page
2) after form submit w/ existing entry
3) after form submit w/ a non-existing entry
0
 

Author Comment

by:Juggy1
ID: 8177244
Bobbit31

I have put the system.outs:

Results
1. For first time to hit page, registration form appears. But it prints out "Do this 3" in tomcat.

2. For putting an existing entry of 'Author' it goes to the success2.jsp after been submitted. In Tomcat it displays,

Arg=Andrei bn = Andrei
Arg=dinu@dmu.ac.ukbn = dinu@dmu.ac.uk
Do this 2

3. For without exisiting entry =

Do this 3
Arg=davidbn = david
Arg=drhodes@fgh.combn = drhodes@fgh.com
Do this 2


These are the displays I get in Tomcat.
Thanks

 
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 8177377
> 1. For first time to hit page, registration form appears. But it prints out "Do this 3" in tomcat.

this is the expected result.

> 2. For putting an existing entry of 'Author' it goes to the success2.jsp after been submitted. In Tomcat it displays,

ok, first of all, your addToDb() method does not check whether or not a given author exists in the authors table or if a given user exists in the users table. you need to add a check to see if it's an existing account and if so return false.

you have this line:
<% if(myRegister.isAuthor()||myRegister.isUser()) {

but that won't do anything, b/c until the form is submitted w/ the new data it will return false

ALSO, your addToDb method doesn't seem to ever insert into the Authors table so if you add an author, they'll still be added in users table so isAuthor() will return false.

0
 

Author Comment

by:Juggy1
ID: 8180902
Hi Bobbit31

I have modified the code in my addtoDb() method so it can check existing user or author.

public boolean addtoDb()
     {
         
          if(bnFirstname.equals("un-named"))
          {
               return false;
          }
         
          if(bnSurname.equals("no-id"))
          {
               return false;
          }
         
          if(bnPassword.equals("no-id"))
          {
               return false;
          }
         
          if(bnEmail.equals("nothing"))
          {
               return false;
          }
         
          if(bnFaculty.equals("nothing"))
          {
               return false;
          }
         
         
         
         
         
          try
          {
               //*********************
               // get the connection
               //*********************
               Class.forName(bnDbDriver);
               Connection con = DriverManager.getConnection(bnDbId, bnDbUser, bnDbPassword);
               
         
               
               //*********************
               // do the sql query
               //*********************
               Statement myStatement = con.createStatement();
               
               if((this.isAuthor())||(this.isUser())==true)
               {
                    returnAdd = "User Already Exists";
               }
               else
               {
                   
                    return false;
               }
                   
                    String sqlQuery = "INSERT INTO User (dbFirstNames,dbSurName,dbPassword,dbEmail,dbFaculty,dbSecurityId) VALUES (?,?,?,?,?,?)";
                    PreparedStatement ps = con.prepareStatement( sqlQuery );
                    ps.setString(1, bnFirstname );
                    ps.setString(2, bnSurname );
                    ps.setString(3, bnPassword );
                    ps.setString(4, bnEmail );
                    ps.setString(5, bnFaculty );
                    ps.setInt(6, bnSecurityId );
                    ps.executeUpdate();
                    returnAdd="Successfully Added";
                    ps.close();
                   
                   
               
          }
          catch (Exception e)
          {
               System.out.println("Register2.addtoDb():" + e);
               //return "failed." + e;
     
          }
           return true;

     }

I'm never going to insert "Author" beacuse I want to keep that seperate and only reference it towards, if a new person wants to register.

The details of the Author will be inserted by the administrator, but a security status is given to all "Users"  who wish to access the website and so "Users" are more likely to access it.

I hope I clarified of what I'm doing, please could you give  me some more suggestions or tips?

I would be grateful fo more advice.

Thanks again
Juggy  

0
 
LVL 18

Expert Comment

by:bobbit31
ID: 8181219
in your addToDb function add the following lines:

System.out.println(bnFirstname);
System.out.println(bnSurname);
...

what does it output?
I'm guessing 'null' not 'un-named' or 'no-id'
0
 

Author Comment

by:Juggy1
ID: 8181950
I get neither.

I just get 'finished' once I compiled it.

Is this what I suppose to do? Below is the reuslt in Tomcat:

Do this 3
Arg=hfdjfhbn = hfdjf
Arg=hhutibn = hhuti
Do this 3

Thanks Juggy
ps please could you explain?
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 8182117
what does it output when you try to add a user
0
 

Author Comment

by:Juggy1
ID: 8183581
Bobbit31

It just outputs what ever I put in so if I put ghrfhg in firstname and that is what goes in.

Nothing else appears it seems roughly the same as before.

Juggy
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 8183653
oops, i didn't catch it the first time... when i said you have to check to see whether the user exists, i meant make a query to the db:

so use this
if(this.isAuthor()||this.isUser())
   return = false;
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 8183705
btw, your code is kind of confusing to figure out b/c your input form calls itself to do the update... why not create another page.

ie:

---------------------------------------------------------
input form (note i changed form action)
---------------------------------------------------------
<html>
     <head>
          <title>Registration Page</title>
     </head>
     <body>
          <form method="post" action="UpdateRegistration.jsp">
               <center>
               <table cellpadding=4 cellspacing=2 border=0 width="70%">
                    <th bgcolor="#99FF66" colspan=2>
                    <font size=5>USER REGISTRATION</font>
                    <font size=1><sup>*</sup> Required Fields</font>
                    </th>
               
                    <tr bgcolor="#99CC66">
                         <td valign=top width="50%">
                              <b>First name<sup>*</sup></b>
                              <br>    
                              <input type="text" name="frmFirstnames" size=22 maxlength=50>
                              </td>

                         <td valign=top width="50%">
                              <b>Surname<sup>*</sup></b>
                              <br>    
                              <input type="text" name="frmSurname" size=22 maxlength=50>
                              </td>
                    </tr>
         
                   
                   
                    <tr bgcolor="#99CC66">
                              <td valign=top>
                                   <b>Email<sup>*</sup></b>
                                   <br>
                                   <input type="text" name="frmEmail" size=22 maxlength=50>
                    </td>
     
                         
                         
                    <td valign="top">
                              <b>Password<sup>*</sup></b>
                              <br>
                              <input type="password" name="frmPassword" size=10 maxlength=10>
                         </td>
                    </tr>    
                   
                    <tr bgcolor="#99CC66">
                         <td align=center colspan="2">
                              <b>Faculty<sup>*</sup></b>
                              <br>
                              <select name="frmFaculty" >
                                   <option></option>
                                   <option>Computing Sciences</option>
                                   <option>Engineering & Technology</option>
                              </select>
                         </td>
                    </tr>    

                   
                         <tr bgcolor="#99CC66">
                              <td  align=center colspan="2">
                                   <input type="submit" value="Submit">
                                   <input type="reset"  value="Reset">
                              </td>
                         </tr>
               </table>
          </form>
     </body>
</html>

--------------------------------------------------------
UpdateRegistration page
--------------------------------------------------------
<%@ page errorPage="Erroreg.jsp" %>

<jsp:useBean
     id="myRegister"
     scope="session"
     class="petshowbeans.Register2"
     />


<jsp:setProperty
     name="myRegister"
     property="bnUserId"
     param="frmUserId"
     />


<jsp:setProperty
     name="myRegister"
     property="bnFirstname"
     param="frmFirstnames"
     />

<jsp:setProperty
     name="myRegister"
     property="bnSurname"
     param="frmSurname"
     />


<jsp:setProperty
     name="myRegister"
     property="bnEmail"
     param="frmEmail"
     />

<jsp:setProperty
     name="myRegister"
     property="bnPassword"
     param="frmPassword"
     />

<jsp:setProperty
     name="myRegister"
     property="bnFaculty"
     param="frmFaculty"
     />

<jsp:setProperty
     name="myRegister"
     property="bnAuthorId"
     param="frmAuthorId"
     />

<jsp:setProperty
     name="myRegister"
     property="bnSecurityId"
     param="frmSecurityId"
     />

<%
if(myRegister.addtoDb()) {
   response.sendRedirect("success2.jsp");
   return;
} else {
   response.sendRedirect("Erroreg.jsp");
   return;
}
%>

0
 

Author Comment

by:Juggy1
ID: 8198023
Hi Bobbit31

Its still causing the same problem, I cannot redirect the browser to an error page if a user or autor exists.
But I have changed the code:

The registration.jsp:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html>
      <head>
            <title>Registration Page</title>
      </head>
      <body>
            <form method="post" action="UpdateRegistration.jsp">
                  <center>
                  <table cellpadding=4 cellspacing=2 border=0 width="50%">
                        <th bgcolor="#99FF66" colspan=2>
                        <font size=5>USER REGISTRATION</font>
                        <font size=1><sup>*</sup> Required Fields</font>
                        </th>
                  
                        <tr bgcolor="#99CC66">
                              <td valign=top width="50%">
                                    <b>First name<sup>*</sup></b>
                                    <br>      
                                    <input type="text" name="frmFirstnames" size=22 maxlength=50>
                                    </td>

                              <td valign=top width="50%">
                                    <b>Surname<sup>*</sup></b>
                                    <br>      
                                    <input type="text" name="frmSurname" size=22 maxlength=50>
                                    </td>
                        </tr>
            
                        
                        
                        <tr bgcolor="#99CC66">
                                    <td valign=top>
                                          <b>Email<sup>*</sup></b>
                                          <br>
                                          <input type="text" name="frmEmail" size=22 maxlength=50>
                        </td>
      
                              
                              
                        <td valign="top">
                                    <b>Password<sup>*</sup></b>
                                    <br>
                                    <input type="password" name="frmPassword" size=10 maxlength=10>
                              </td>
                        </tr>      
                        

                              
                        
                        <tr bgcolor="#99CC66">
                              <td align=center colspan="2">
                                    <b>Faculty<sup>*</sup></b>
                                    <br>
                                    <select name="frmFaculty" >
                                          <option></option>
                                          <option>Computing Sciences</option>
                                          <option>Engineering & Technology</option>
                                    </select>
                              </td>
                        </tr>      

                        
                              <tr bgcolor="#99CC66">
                                    <td  align=center colspan="2">
                                          <input type="submit" value="Submit">
                                          <input type="reset"  value="Reset">
                                    </td>
                              </tr>
                  </table>
            </form>
      </body>
</html>

********************************************************************************************

UpdateRegistration.jsp


<%@ page errorPage="Erroreg.jsp" %>


<jsp:useBean
      id="myRegister"
      scope="request"
      class="petshowbeans.Register2"
      />


<jsp:setProperty
      name="myRegister"
      property="bnUserId"
      param="frmUserId"
      />


<jsp:setProperty
      name="myRegister"
      property="bnFirstname"
      param="frmFirstnames"
      />

<jsp:setProperty
      name="myRegister"
      property="bnSurname"
      param="frmSurname"
      />


<jsp:setProperty
      name="myRegister"
      property="bnEmail"
      param="frmEmail"
      />

<jsp:setProperty
      name="myRegister"
      property="bnPassword"
      param="frmPassword"
      />

<jsp:setProperty
      name="myRegister"
      property="bnFaculty"
      param="frmFaculty"
      />



<jsp:setProperty
      name="myRegister"
      property="bnAuthorId"
      param="frmAuthorId"
      />

<jsp:setProperty
      name="myRegister"
      property="bnSecurityId"
      param="frmSecurityId"
      />





            <%
            if(myRegister.addtoDb()) {
                  response.sendRedirect("success2.jsp");
                  return;
            } else {
                  response.sendRedirect("Erroreg.jsp");
                  return;
            }
            %>


*******************************************************************************************

The Register2.java class:

package petshowbeans;

import java.util.*;
import java.sql.*;

public class Register2 {
      private String bnFirstname = "un-named";
      private String bnSurname = "no-id";
      private String bnEmail = "nothing";
      private String bnPassword = "no-id";
      private String bnFaculty = "nothing";
      private String bnDbDriver = "org.gjt.mm.mysql.Driver";
      private String bnDbId = "jdbc:mysql://localhost/itx98jkdb";
      private String bnDbUser = "username";
      private String bnDbPassword = "Password";
      private int bnUserId;
      private int bnAuthorId;
      private int bnSecurityId = 1;
      private String firstName;
      private String lastName;
      private String email;
      private String password;
      private String faculty;
      private String jobTitle;
      private String returnAdd = "none";
      
      public void setBnFirstname(String argFirstname) {
            bnFirstname = argFirstname;
      }
      
      public void setBnSurname(String argSurname) {
            bnSurname = argSurname.trim();
      }
      
      public void setBnEmail(String argEmail) {
            bnEmail = argEmail.trim();
      }
      
      public void setBnPassword(String argPassword) {
            bnPassword = argPassword.trim();
      }
      
      public void setBnFaculty(String argFaculty) {
            bnFaculty = argFaculty.trim();
      }
      
      public void setBnUserId(int argUserId) {
            bnUserId = argUserId;
      }
      
      public void setBnAuthorId(int argAuthorId) {
            bnAuthorId = argAuthorId;
      }
      
      public void setBnSecurityId(int argSecurityId) {
            bnSecurityId = argSecurityId;
      }
      
      public String getFirstName() {
            return firstName;
      }
      
      public String getLastName() {
            return lastName;
      }
      
      /* See if the user exists  */
      public boolean isUser() {
            boolean isUser = false;
            try {
                  Class.forName(bnDbDriver);
                  Connection con = DriverManager.getConnection(bnDbId, bnDbUser, bnDbPassword);
                  Statement myStatement = con.createStatement();
                  String sqlQuery = "SELECT * FROM User WHERE dbUserId = '" + bnUserId + "'";
                  ResultSet myResultSet = myStatement.executeQuery(sqlQuery);
                  isUser = myResultSet.next();
            }
            catch (Exception e) {
                  System.out.println("Register.CheckUser()");
            }
            return isUser;
      }

      public boolean isAuthor() {
            boolean isAuthor = false;
            try {
                  Class.forName(bnDbDriver);
                  Connection con = DriverManager.getConnection(bnDbId, bnDbUser, bnDbPassword);
                  Statement myStatement = con.createStatement();
                  String sqlQuery = "SELECT * FROM Author WHERE dbAuthorId = '" + bnAuthorId + "'";
                  ResultSet myResultSet = myStatement.executeQuery(sqlQuery);
                  isAuthor = myResultSet.next();
            }
            catch (Exception e) {
                  System.out.println("Register.CheckUser()");
            }
            return isAuthor;
      }

      public boolean addtoDb() {
            boolean added = false;
            if(bnFirstname.equals("un-named")) return false;
            if(bnSurname.equals("no-id")) return false;
            if(bnPassword.equals("no-id")) return false;
            if(bnEmail.equals("nothing")) return false;
            if(bnFaculty.equals("nothing")) return false;
      try {
                   Class.forName(bnDbDriver);
                   Connection con = DriverManager.getConnection(bnDbId, bnDbUser, bnDbPassword);
                   Statement myStatement = con.createStatement();
                   if(this.isAuthor()||this.isUser()) {
                        returnAdd = "User Already Exists"; }
                   else {
                        String sqlQuery = "INSERT INTO User (dbFirstNames,dbSurName,dbPassword,dbEmail,dbFaculty,dbSecurityId) VALUES (?,?,?,?,?,?)";
                        PreparedStatement ps = con.prepareStatement(sqlQuery);
                        ps.setString(1, bnFirstname);
                        ps.setString(2, bnSurname);
                        ps.setString(3, bnPassword);
                        ps.setString(4, bnEmail);
                        ps.setString(5, bnFaculty);
                        ps.setInt(6, bnSecurityId);
                        ps.executeUpdate();
                        returnAdd = "Successfully Added";
                        ps.close();
                        added = true;
                  }      
            }
            catch (Exception e) {
                  System.out.println("Register2.addtoDb()");
                  return added = false;
            }
            return added;
      }
}













      
      
0
 

Author Comment

by:Juggy1
ID: 8198030
Hi Bobbit31

Its still causing the same problem, I cannot redirect the browser to an error page if a user or author exists.
But I have changed the code:

The registration.jsp:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html>
      <head>
            <title>Registration Page</title>
      </head>
      <body>
            <form method="post" action="UpdateRegistration.jsp">
                  <center>
                  <table cellpadding=4 cellspacing=2 border=0 width="50%">
                        <th bgcolor="#99FF66" colspan=2>
                        <font size=5>USER REGISTRATION</font>
                        <font size=1><sup>*</sup> Required Fields</font>
                        </th>
                  
                        <tr bgcolor="#99CC66">
                              <td valign=top width="50%">
                                    <b>First name<sup>*</sup></b>
                                    <br>      
                                    <input type="text" name="frmFirstnames" size=22 maxlength=50>
                                    </td>

                              <td valign=top width="50%">
                                    <b>Surname<sup>*</sup></b>
                                    <br>      
                                    <input type="text" name="frmSurname" size=22 maxlength=50>
                                    </td>
                        </tr>
            
                        
                        
                        <tr bgcolor="#99CC66">
                                    <td valign=top>
                                          <b>Email<sup>*</sup></b>
                                          <br>
                                          <input type="text" name="frmEmail" size=22 maxlength=50>
                        </td>
      
                              
                              
                        <td valign="top">
                                    <b>Password<sup>*</sup></b>
                                    <br>
                                    <input type="password" name="frmPassword" size=10 maxlength=10>
                              </td>
                        </tr>      
                        

                              
                        
                        <tr bgcolor="#99CC66">
                              <td align=center colspan="2">
                                    <b>Faculty<sup>*</sup></b>
                                    <br>
                                    <select name="frmFaculty" >
                                          <option></option>
                                          <option>Computing Sciences</option>
                                          <option>Engineering & Technology</option>
                                    </select>
                              </td>
                        </tr>      

                        
                              <tr bgcolor="#99CC66">
                                    <td  align=center colspan="2">
                                          <input type="submit" value="Submit">
                                          <input type="reset"  value="Reset">
                                    </td>
                              </tr>
                  </table>
            </form>
      </body>
</html>

********************************************************************************************

UpdateRegistration.jsp


<%@ page errorPage="Erroreg.jsp" %>


<jsp:useBean
      id="myRegister"
      scope="request"
      class="petshowbeans.Register2"
      />


<jsp:setProperty
      name="myRegister"
      property="bnUserId"
      param="frmUserId"
      />


<jsp:setProperty
      name="myRegister"
      property="bnFirstname"
      param="frmFirstnames"
      />

<jsp:setProperty
      name="myRegister"
      property="bnSurname"
      param="frmSurname"
      />


<jsp:setProperty
      name="myRegister"
      property="bnEmail"
      param="frmEmail"
      />

<jsp:setProperty
      name="myRegister"
      property="bnPassword"
      param="frmPassword"
      />

<jsp:setProperty
      name="myRegister"
      property="bnFaculty"
      param="frmFaculty"
      />



<jsp:setProperty
      name="myRegister"
      property="bnAuthorId"
      param="frmAuthorId"
      />

<jsp:setProperty
      name="myRegister"
      property="bnSecurityId"
      param="frmSecurityId"
      />





            <%
            if(myRegister.addtoDb()) {
                  response.sendRedirect("success2.jsp");
                  return;
            } else {
                  response.sendRedirect("Erroreg.jsp");
                  return;
            }
            %>


*******************************************************************************************

The Register2.java class:

package petshowbeans;

import java.util.*;
import java.sql.*;

public class Register2 {
      private String bnFirstname = "un-named";
      private String bnSurname = "no-id";
      private String bnEmail = "nothing";
      private String bnPassword = "no-id";
      private String bnFaculty = "nothing";
      private String bnDbDriver = "org.gjt.mm.mysql.Driver";
      private String bnDbId = "jdbc:mysql://localhost/itx98jkdb";
      private String bnDbUser = "username";
      private String bnDbPassword = "Password";
      private int bnUserId;
      private int bnAuthorId;
      private int bnSecurityId = 1;
      private String firstName;
      private String lastName;
      private String email;
      private String password;
      private String faculty;
      private String jobTitle;
      private String returnAdd = "none";
      
      public void setBnFirstname(String argFirstname) {
            bnFirstname = argFirstname;
      }
      
      public void setBnSurname(String argSurname) {
            bnSurname = argSurname.trim();
      }
      
      public void setBnEmail(String argEmail) {
            bnEmail = argEmail.trim();
      }
      
      public void setBnPassword(String argPassword) {
            bnPassword = argPassword.trim();
      }
      
      public void setBnFaculty(String argFaculty) {
            bnFaculty = argFaculty.trim();
      }
      
      public void setBnUserId(int argUserId) {
            bnUserId = argUserId;
      }
      
      public void setBnAuthorId(int argAuthorId) {
            bnAuthorId = argAuthorId;
      }
      
      public void setBnSecurityId(int argSecurityId) {
            bnSecurityId = argSecurityId;
      }
      
      public String getFirstName() {
            return firstName;
      }
      
      public String getLastName() {
            return lastName;
      }
      
      /* See if the user exists  */
      public boolean isUser() {
            boolean isUser = false;
            try {
                  Class.forName(bnDbDriver);
                  Connection con = DriverManager.getConnection(bnDbId, bnDbUser, bnDbPassword);
                  Statement myStatement = con.createStatement();
                  String sqlQuery = "SELECT * FROM User WHERE dbUserId = '" + bnUserId + "'";
                  ResultSet myResultSet = myStatement.executeQuery(sqlQuery);
                  isUser = myResultSet.next();
            }
            catch (Exception e) {
                  System.out.println("Register.CheckUser()");
            }
            return isUser;
      }

      public boolean isAuthor() {
            boolean isAuthor = false;
            try {
                  Class.forName(bnDbDriver);
                  Connection con = DriverManager.getConnection(bnDbId, bnDbUser, bnDbPassword);
                  Statement myStatement = con.createStatement();
                  String sqlQuery = "SELECT * FROM Author WHERE dbAuthorId = '" + bnAuthorId + "'";
                  ResultSet myResultSet = myStatement.executeQuery(sqlQuery);
                  isAuthor = myResultSet.next();
            }
            catch (Exception e) {
                  System.out.println("Register.CheckUser()");
            }
            return isAuthor;
      }

      public boolean addtoDb() {
            boolean added = false;
            if(bnFirstname.equals("un-named")) return false;
            if(bnSurname.equals("no-id")) return false;
            if(bnPassword.equals("no-id")) return false;
            if(bnEmail.equals("nothing")) return false;
            if(bnFaculty.equals("nothing")) return false;
      try {
                   Class.forName(bnDbDriver);
                   Connection con = DriverManager.getConnection(bnDbId, bnDbUser, bnDbPassword);
                   Statement myStatement = con.createStatement();
                   if(this.isAuthor()||this.isUser()) {
                        returnAdd = "User Already Exists"; }
                   else {
                        String sqlQuery = "INSERT INTO User (dbFirstNames,dbSurName,dbPassword,dbEmail,dbFaculty,dbSecurityId) VALUES (?,?,?,?,?,?)";
                        PreparedStatement ps = con.prepareStatement(sqlQuery);
                        ps.setString(1, bnFirstname);
                        ps.setString(2, bnSurname);
                        ps.setString(3, bnPassword);
                        ps.setString(4, bnEmail);
                        ps.setString(5, bnFaculty);
                        ps.setInt(6, bnSecurityId);
                        ps.executeUpdate();
                        returnAdd = "Successfully Added";
                        ps.close();
                        added = true;
                  }      
            }
            catch (Exception e) {
                  System.out.println("Register2.addtoDb()");
                  return added = false;
            }
            return added;
      }
}



I dont want to use html docs, but I want to just use JSP and Javabeans, please can you help.


Thanks again
Juggy





      
      
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 8198212
in your addToDb(), is the below clause returning true?
               if(this.isAuthor()||this.isUser()) {
                   System.out.println("do this");
                   return false; }
                else {

ie. does it output "do this"?

if not, check the queries (sqlQuery) that isAuthor() and isUser() is sending to the database. Are they correct?

The code you have above, "should" work as desired so long as this.isAuthor() and this.isUser() are returning the correct results.
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 8198462
ok, i think i found it... in your isAuthor and isUser you are using:

String sqlQuery = "SELECT * FROM User WHERE dbUserId = '" + bnUserId + "'";

and

String sqlQuery = "SELECT * FROM Author WHERE dbAuthorId = '" + bnAuthorId + "'";

respectively...

HOWEVER, i'm quite certain that bnUserId and bnAuthorId will be 0 b/c:

<jsp:setProperty
     name="myRegister"
     property="bnUserId"
     param="frmUserId"
     />

there is no value in your form named frmUserId so since bnUserId is an int it will probably default it to 0 giving

SELECT * FROM User WHERE dbUserId = '0'

which is why it's never finding the user.

an alternative approach could be to reference against some unique field in your table (ie. bnEmail?)

"SELECT * FROM User WHERE dbEmail = '" + bnEmail + "'"

(or create a new field for userName... ie. j-smith) and make that field unique and use that for comparison.
0
 

Author Comment

by:Juggy1
ID: 8203658
Thanks Bobbit31 fro the info, it has slighltly helped.

I have changed certain things as you can see from the code below, but its doing it properly:

public boolean addtoDb() {
            boolean added = false;
            if(bnFirstname.equals("un-named")) return false;
            if(bnSurname.equals("no-id")) return false;
            if(bnPassword.equals("no-id")) return false;
            if(bnEmail.equals("nothing")) return false;
            if(bnFaculty.equals("nothing")) return false;
      try {
                   Class.forName(bnDbDriver);
                   Connection con = DriverManager.getConnection(bnDbId, bnDbUser, bnDbPassword);
                   Statement myStatement = con.createStatement();
                   if(this.isUser()||this.isAuthor()) {
                        returnAdd = "User Already Exists";
                        return false; }
                   //else if (this.isAuthor()) {
                        //returnAdd = "User Already Exists"; }      
                   else {
                        //added = true;
                        String sqlQuery = "INSERT INTO User (dbFirstNames,dbSurName,dbPassword,dbEmail,dbFaculty,dbSecurityId) VALUES (?,?,?,?,?,?)";
                        PreparedStatement ps = con.prepareStatement(sqlQuery);
                        ps.setString(1, bnFirstname);
                        ps.setString(2, bnSurname);
                        ps.setString(3, bnPassword);
                        ps.setString(4, bnEmail);
                        ps.setString(5, bnFaculty);
                        ps.setInt(6, bnSecurityId);
                        ps.executeUpdate();
                        returnAdd = "Successfully Added";
                        ps.close();
                        added = true;
                  }
            }
            catch (Exception e) {
                  System.out.println("Register2.addtoDb()");
                  //return added = false;
            }
            return added;
      }

I tried splitting the the if statement and set it too false, for example:
              if(this.isUser()==false)
              {
                  returnAdd = "User already exists";
              }
              else {
              Start inserting etc.


/* See if the user exists  */
      public boolean isUser() {
            boolean isUser = false;
            try {
                  Class.forName(bnDbDriver);
                  Connection con = DriverManager.getConnection(bnDbId, bnDbUser, bnDbPassword);
                  Statement myStatement = con.createStatement();
                  String sqlQuery = "SELECT * FROM User WHERE dbEmail = '" + bnUserId + "'";
                  ResultSet myResultSet = myStatement.executeQuery(sqlQuery);
                  isUser = myResultSet.next();
            }
            catch (Exception e) {
                  System.out.println("Register.CheckUser()");
            }
            return isUser;
      }




      public boolean isAuthor() {
            boolean isAuthor = false;
            try {
                  Class.forName(bnDbDriver);
                  Connection con = DriverManager.getConnection(bnDbId, bnDbUser, bnDbPassword);
                  Statement myStatement = con.createStatement();
                  String sqlQuery = "SELECT * FROM Author WHERE dbEmail = '" + bnAuthorId + "'";
                  ResultSet myResultSet = myStatement.executeQuery(sqlQuery);
                  isAuthor = myResultSet.next();
            }
            catch (Exception e) {
                  System.out.println("Register.CheckUser()");
            }
            return isAuthor;
      }



I used an existing user and redirected me to the error jsp page, but when I inserted set of new details it still continued to send me to the error jsp page.

I would be grateful if you can take another look, it must be tiredsome because its just one minor detail to solve. Anyway  thanks again for all your help.

Juggy
ps It will be difficult to state the same bean in jsp will it not cause a problem of conflict (dbEmail = bnEmail for User and dbEmail = bnEmail for Author)?
0
 

Author Comment

by:Juggy1
ID: 8203697
I tried splitting the the if statement and set it too false, for example:
             if(this.isUser()==false)
             {
                 returnAdd = "User already exists";
             }
             else {
             Start inserting etc.

This is what I carried out to test it out.
It directs the user to the error jsp page, if they are existing or new user.

Thanks again
Juggy
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 8203907
/* See if the user exists  */
     public boolean isUser() {
          boolean isUser = false;
          try {
               Class.forName(bnDbDriver);
               Connection con = DriverManager.getConnection(bnDbId, bnDbUser, bnDbPassword);
               Statement myStatement = con.createStatement();
               String sqlQuery = "SELECT * FROM User WHERE dbEmail = '" + bnEmail + "'";
               ResultSet myResultSet = myStatement.executeQuery(sqlQuery);
               isUser = myResultSet.next();
          }
          catch (Exception e) {
               System.out.println("Register.CheckUser()");
          }
          return isUser;
     }




     public boolean isAuthor() {
          boolean isAuthor = false;
          try {
               Class.forName(bnDbDriver);
               Connection con = DriverManager.getConnection(bnDbId, bnDbUser, bnDbPassword);
               Statement myStatement = con.createStatement();
               String sqlQuery = "SELECT * FROM Author WHERE dbEmail = '" + bnEmail + "'";
               ResultSet myResultSet = myStatement.executeQuery(sqlQuery);
               isAuthor = myResultSet.next();
          }
          catch (Exception e) {
               System.out.println("Register.CheckUser()");
          }
          return isAuthor;
     }
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 8203959
if(this.isUser()==false)
             {
                 returnAdd = "User already exists";
             }

this is wrong... if this.isUser() == false that means that the user does not exist, so it is ok to add.

this should work fine:

if (this.isUser() || this.isAuthor()) {
   returnAdd = "User already exists";
   return false;
} else {
   // start inserting
}
0
 

Author Comment

by:Juggy1
ID: 8251867
thanks for all your help.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
The Summer 2017 Scholarship Winners have been announced!
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question