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;
}