Solved

Can anyone tell me what is wrong with this code????????????????

Posted on 2001-06-19
8
251 Views
Last Modified: 2010-04-16
This coding has been edited from a currently woking code.  this should get data from a html file and put it in a table called survey.  the old code did the same but to a table called customers in the same database and sent out an email witch i do not need it to do anymore... PLEASE HELP.


package uk.co.eformoney.servlets;

import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import java.sql.*;
import javax.servlet.http.*;
import charlesworth.database.*;
import charlesworth.utils.*;
import uk.co.eformoney.Context.*;
import uk.co.eformoney.DataObjects.*;
import java.net.*;

public class SurveyDetailsServlet extends HttpServlet {

    private String errorPage;

    public void init(ServletConfig config){

      try{
        super.init(config);
        System.out.println( "Init method SurveyDetailsServlet " + new java.util.Date().toGMTString()  );

        String errorPageURL  = this.getInitParameter("errorPageURL");

        //Create a pool of connections to be used by the whole application
        if( ApplicationContext.getConnectionPool() == null ){

          //String username = this.getInitParameter("username");
         // String password = this.getInitParameter("password");
          String url      = this.getInitParameter("url");
          String driver   = this.getInitParameter("driver");
          String incrementConnectionsBy      = this.getInitParameter("incrementConnectionsBy");
          String initialDatabaseConnections  = this.getInitParameter("initialDatabaseConnections");

          ApplicationContext.setConnectionPool( new ConnectionPool(
                        url,
                                                                   // username,
                                                                  //  password,
                                                                    driver,
                                                                    Integer.parseInt(initialDatabaseConnections),
                                                                    Integer.parseInt(incrementConnectionsBy)));

        }
        //Retrieve the application's standing data
        ApplicationContext.populateStandingData();


        //Load the error page into a char array for efficiency
        errorPage = ApplicationContext.getRegErrorPage( errorPageURL );

        //start the Daemon for registration mailing
       // String smtpHost        = this.getInitParameter("smtphost");
       // String from            = this.getInitParameter("from");
       // String emailTitle      = this.getInitParameter("emailTitle");
      //  String emailBodyURL    = this.getInitParameter("emailBodyURL");

      //  new EmailSender(smtpHost, from, emailTitle, emailBodyURL);
      }
     catch( MalformedURLException e ){
      System.out.println("********ERROR OCCURRED AT " + new java.util.Date().toGMTString() );
      e.printStackTrace();
       this.getServletContext().log( "A MalformedURLException occurred while loading data for the Registration form error page. Check the url given in the initialisation parameters for this Servlet", e  );
   }
     catch( Exception e ){
        System.out.println("********ERROR OCCURRED AT " + new java.util.Date().toGMTString() );
        e.printStackTrace();
      this.getServletContext().log( "An error occurred during initialisation. Check the url given in the initialisation parameters for this Servlet and the database setup", e  );
     }
    }

   public void doGet(HttpServletRequest req,
                     HttpServletResponse res)
           throws IOException, ServletException
   {


         doPost( req, res );
    }

    public void doPost(HttpServletRequest req,
                       HttpServletResponse res)
        throws IOException, ServletException
    {
        try{
          PrintWriter out = res.getWriter();
          res.setContentType("text/html");
          insertRow(req,res);
          res.sendRedirect("registersuccess.html");
          //showSuccessPage(out);
        }
        catch( DatabaseObjectException e ){
          ErrorHandler.showErrorPage( res.getWriter(),
                        e.getMessage(),
                        errorPage );
        }
               catch( Exception e ){
          System.out.println("********ERROR OCCURRED AT " + new java.util.Date().toGMTString() );
          e.printStackTrace();
          this.getServletContext().log( "An error occurred during processing. Check the url given in the initialisation parameters for this Servlet and the database setup", e  );
          ErrorHandler.showErrorPage( res.getWriter(),
                        "A server error occurred while processing your input.<br>The error was not your fault.<br>Please try again later.",
                        errorPage );

       }

    }



    private boolean insertRow(HttpServletRequest req,
                           HttpServletResponse res)
          throws Exception{

        String referrerID = null;

        //Check for a unique username
        //checkUsername(req.getParameter("username"));

        //Check for a correct referrer
        //referrerID = getReferrer(req.getParameter("referrer"));

        //Check that the passwords match
       // if( !req.getParameter("password").equals( req.getParameter("verifypassword") )){
       //   throw new DatabaseObjectException( "Your passwords do not match. Please re-enter them.");
      //  }

        ConnectionPool pool = ApplicationContext.getConnectionPool();

        //Create a transaction to save the registration details
                    SurveyRegistrationTransaction trans = new SurveyRegistrationTransaction();

        //Create a Survey customer object to hold the form data
        SurveyDBObject reg = new SurveyDBObject();
        reg.setId(req.getParameter("firstname"));
        reg.setTitle(req.getParameter("title"));
        reg.setFirst_name(req.getParameter("firstname"));
        reg.setSurname(req.getParameter("surname"));
        reg.setGender(req.getParameter("gender"));
        reg.setDob(DatabaseUtils.getDDMonYYYY(req.getParameter("day"),req.getParameter("month"),req.getParameter("year")));
        reg.setEmail_address(req.getParameter("e-mail"));
        reg.setAddr_1(req.getParameter("address1"));
        reg.setAddr_2(req.getParameter("address2"));
        reg.setTown(req.getParameter("town"));
        reg.setChildren(req.getParameter("children"));
        reg.setAddr_county(req.getParameter("addr_county"));
        reg.setPost_code(req.getParameter("post_code"));
        reg.setQ1a(req.getParameter("q1a"));
        reg.setQ1b(req.getParameter("q1b"));
        reg.setQ1c(req.getParameter("q1c"));
        reg.setQ1d(req.getParameter("q1d"));
        reg.setQ1e(req.getParameter("q1e"));
        reg.setQ2a(req.getParameter("q2a"));
        reg.setQ2b(req.getParameter("q2b"));
        reg.setQ2c(req.getParameter("q2c"));
        reg.setQ2d(req.getParameter("q2d"));
        reg.setQ3a(req.getParameter("q3a"));
        reg.setQ4a(req.getParameter("q4a"));
        reg.setQ5a(req.getParameter("q5a"));
        reg.setQ6a(req.getParameter("q6a"));
        reg.setQ7a(req.getParameter("q7a"));
        reg.setQ8a(req.getParameter("q8a"));
        reg.setQ9a(req.getParameter("q9a"));
        reg.setQ10a(req.getParameter("q10a"));
        reg.setQ11a(req.getParameter("q11a"));
        reg.setQ12a(req.getParameter("q12a"));
        reg.setQ13a(req.getParameter("q13a"));
        reg.setQ14a(req.getParameter("q14a"));
        reg.setQ15a(req.getParameter("q15a"));
        reg.setQ16a(req.getParameter("q16a"));
        reg.setQ17a(req.getParameter("q17a"));
        reg.setQ18a(req.getParameter("q18a"));
        reg.setQ19a(req.getParameter("q19a"));
        reg.setQ20a(req.getParameter("q20a"));
        reg.setQ21a(req.getParameter("q21a"));
        reg.setQ22a(req.getParameter("q22a"));
        reg.setQ23a(req.getParameter("q23a"));
        reg.setQ24a(req.getParameter("q24a"));
        reg.setQ25a(req.getParameter("q25a"));
        reg.setQ25b(req.getParameter("q25b"));
        reg.setQ25c(req.getParameter("q25c"));
        reg.setQ25d(req.getParameter("q25d"));
        reg.setQ25e(req.getParameter("q25e"));
        reg.setQ25f(req.getParameter("q25f"));
        reg.setQ26a(req.getParameter("q26a"));
        reg.setQ27a(req.getParameter("q27a"));
        reg.setQ27b(req.getParameter("q27b"));
        reg.setQ27c(req.getParameter("q27c"));
        reg.setQ28a(req.getParameter("q28a"));
        reg.setQ29a(req.getParameter("q29a"));
        reg.setQ30a(req.getParameter("q30a"));
        reg.setQ31a(req.getParameter("q31a"));
        reg.setQ32a(req.getParameter("q32a"));
        reg.setSelect(req.getParameter("select1"));
        reg.setSelect3(req.getParameter("select3"));
        reg.setSelect6(req.getParameter("select6"));
        reg.setSelect7(req.getParameter("select7"));
        reg.setSelect8(req.getParameter("select8"));
        reg.setSelect9(req.getParameter("select9"));
        reg.setSelect10(req.getParameter("select10"));
        reg.setSelect11(req.getParameter("select11"));
        reg.setSelect12(req.getParameter("select12"));
        reg.setSelect13(req.getParameter("select13"));


        //Add the survey details to the transaction
                    trans.setSurvey(reg);

        //Ask the transaction to insert the data into the database
                    trans.insert();

        //send cookies with a 90 day expiry time
        //Cookie userCookie = new Cookie("username", req.getParameter("user") );
        //userCookie.setMaxAge(7776000);
        //res.addCookie( userCookie);
        //Cookie passCookie = new Cookie("password", req.getParameter("password") );
        //passCookie.setMaxAge(7776000);
        //res.addCookie( passCookie);

        //return true;
      }

    {
  private void showSuccessPage( PrintWriter out ){

      out.println("<html>");
      out.println("<head><title>Success</title></head>");
      out.println("<body>");
      out.println("<b><font SIZE=\"5\" COLOR=\"#007346\" FACE=\"arial,helvetica\">Congratulations, you have successfully registered your survey!</font></b>");
      out.println("</body></html>");
  }


   //private String getReferrer( String username )throws Exception{

    //if( username == null || username.length() == 0 ){
     // return "";
   // }

    //username = username.toUpperCase();

   // ConnectionPool pool = ApplicationContext.getConnectionPool();
  //  Connection con  = null;
  //  Statement stmt  = null;

//    try{
  //    con = pool.getConnection();
    //  stmt = con.createStatement();
     // StringBuffer sql = new StringBuffer("select id from survey where 1=1 ");
     // sql.append( DatabaseUtils.getWhereCharSQL(username, "username"));
     // ResultSet result = stmt.executeQuery( sql.toString() );

     // String referrerId = null;

     // while( result.next() ){
     //   referrerId = result.getString("id");
     // }

    //  if( referrerId == null){
     //   throw new DatabaseObjectException("The referred by username you entered does not exist .<br>Please check it and re-enter.");
     // }

     // return referrerId;

  //  }
   // finally{
 //     if( stmt != null ){
   //         stmt.close();
     //     }

    //  if( con != null ){
//        pool.returnConnection(con);
  //      con = null;
   //   }
 //   }
//  }

//  private void checkUsername( String username )throws Exception{

 //   ConnectionPool pool = ApplicationContext.getConnectionPool();
  //  Connection con = null;
   // Statement stmt  = null;

 //   try{

   //   if( username == null || username.length() == 0 ){
    //    throw new DatabaseObjectException("Please enter a username.");
     // }

     // username = username.toUpperCase();

      //con = pool.getConnection();
     // stmt = con.createStatement();
     // StringBuffer sql = new StringBuffer("select 0 from customers where 1=1 ");
     // sql.append( DatabaseUtils.getWhereCharSQL(username, "username"));
     // ResultSet result = stmt.executeQuery( sql.toString() );

    //  if(result.next()){
      //  throw new DatabaseObjectException("The username you entered has been taken by another user.<br>Please enter another one.");
     // }

 //   }
   // finally{
     // if( stmt != null ){
       //     stmt.close();
       //   }

      if( con != null ){
        pool.returnConnection(con);
        con = null;
      }
0
Comment
Question by:Duke01
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 9

Expert Comment

by:Ovi
ID: 6205795
I can tell you is a big source. What kind of error are you getting ? Describe the simptoms. By a "look from an airplane" I say that at the end of the code posted here you have left an uncommented if() statement and end method brace.
0
 

Author Comment

by:Duke01
ID: 6205839
When i trie to compile i get theys errors..

SurveyDetailsServlet.java:6: Package javax.servlet not found in import.
import javax.servlet.*;
       ^
SurveyDetailsServlet.java:8: Package javax.servlet.http not found in import.
import javax.servlet.http.*;
       ^
SurveyDetailsServlet.java:15: Superclass uk.co.eformoney.servlets.HttpServlet
 class uk.co.eformoney.servlets.SurveyDetailsServlet not found.
public class SurveyDetailsServlet extends HttpServlet {

0
 
LVL 2

Expert Comment

by:dbaora
ID: 6205846
hi,
try downloading the servlet package from http://java.sun.com or if you already have it, add it to your CLASSPATH environment variable.
0
 
LVL 3

Accepted Solution

by:
rjackman earned 300 total points
ID: 6205903
hi
yes dbaora is right u have to download the package from
jsdk1.2 from
www.java.sun.com/products
and then set the class path to include the jar file
may be servlet.jar
or
jsdk.jar
depends on the jsdk version
cheers
RJ
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:Duke01
ID: 6205934
done that now i have new errors.

javac -deprecation SurveyDetailsServlet.java
SurveyDetailsServlet.java:23: Note: The method java.lang.String
toGMTString() in class java.util.Date has been deprecated.
        System.out.println( "Init method SurveyDetailsServlet " + new
java.util.Date().toGMTString()  );

^
SurveyDetailsServlet.java:37: Wrong number of arguments in constructor.
          ApplicationContext.setConnectionPool( new ConnectionPool(
                                                ^
SurveyDetailsServlet.java:62: Note: The method java.lang.String
toGMTString() in class java.util.Date has been deprecated.
      System.out.println("********ERROR OCCURRED AT " + new
java.util.Date().toGMTString() );

^
SurveyDetailsServlet.java:67: Note: The method java.lang.String
toGMTString() in class java.util.Date has been deprecated.
        System.out.println("********ERROR OCCURRED AT " + new
java.util.Date().toGMTString() );

^
SurveyDetailsServlet.java:99: Note: The method java.lang.String
toGMTString() in class java.util.Date has been deprecated.
          System.out.println("********ERROR OCCURRED AT " + new
java.util.Date().toGMTString() );

^
SurveyDetailsServlet.java:112: Return required at end of boolean
insertRow(javax.servlet.http.HttpServletRequest,
javax.servlet.http.HttpServletResponse).
    private boolean insertRow(HttpServletRequest req,
                    ^
Note: SurveyDetailsServlet.java uses or overrides a deprecated API.  Please
consult the documentation for a better alternative.
2 errors, 1 warning

0
 
LVL 9

Expert Comment

by:Ovi
ID: 6206039
This are programming errors and some warnings about some deprecated methods.
0
 

Author Comment

by:Duke01
ID: 6206056
thanks mate..
0
 
LVL 3

Expert Comment

by:rjackman
ID: 6206198
Thanx
Cheers
RickyJackman
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now