I'm desperately trying to do some coursework and its not going well. I've been searching for example code for registering a new user to a database. I have a database connection and data access servlets but its riddled with errors. Its building but throwing up apache errors of all types. Could someone have a look at this for me please and suggest a nice easy fix? I'll be your friend forever!
At the moment I'm getting a "Cannot forward after response has been committed" error, but there was a nullpointer before and I expect a million other errors.
register.jsp
<html>
<head><title>Luvalarm - login</title></head>
<body>
<h1>Register with luvalarm to get woken by your virtual lover</h1>
<form action="Register.do" method="post" name="regForm">
<table width="526">
<tr>
<td>Choose a username:</td><td><input type="text" name="username"/></td>
</tr>
<tr>
<td>Select a password:</td><td><input type="password" name="password"/></td>
</tr>
<tr>
<td>Re-type your password:</td><td><input type="password" name="password"/></td>
</tr>
<tr>
<td>First name:</td><td><input type="text" name="firstname"/></td>
</tr>
<tr>
<td>Last name:</td><td><input type="text" name="lastname"/></td>
</tr>
<tr>
<td>Select the preferred gender of your lover:</td><td><input type="radio" name="sexpref" value="M" />
Male
<input type="radio" name="sex" value="F" /> Female</td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="login" value="Submit"/></td>
</tr>
</table>
</form>
Already a member? -<a href="login.jsp">Login</a>
</body>
</html>
RegistrationServlet (register.do in the web.xml)
package luvalarm;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
public class RegistrationServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
HttpSession session = request.getSession();
String username = request.getParameter("username");
String password = request.getParameter("password");
String firstname = request.getParameter("firstname");
String lastname = request.getParameter("lastname");
String sexpref = request.getParameter("sexpref");
/* double check that all fields have been filled */
if (username == null || password == null || firstname == null|| lastname == null|| sexpref == null) {
response.sendRedirect("/register.jsp");
} else {
DataInputter checknew = new DataInputter();
Boolean userTaken = checknew.isTaken(username);
if (userTaken == false)
{
DataInputter regNew = new DataInputter();
regNew.regUser(username,password,firstname,lastname,sexpref);
//and finally yor redirect to get them to log in
response.sendRedirect("/login.jsp");
}
else
{
//send them to a registration page that gets them to log in
response.sendRedirect("/reregister.jsp");
}
}
// forward the request to be handled or complete the handling of this list students request
getServletContext().getRequestDispatcher("/usermenu.jsp").forward(request, response);
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
doPost(request, response);
}
}
DataInputter (with the sql queries)
package luvalarm;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class DataInputter {
private Connection connection;
Boolean userTaken;
public DataInputter() {
// the next step is to make the connectionManager look after a pool of connections
this.connection = ConnectionManager.createConnection();
}
public Boolean isTaken(String username) {
String sql = "select * from user where user_name = '" + username + "'\"";
executeQuery(sql, new ResultSetCallback() {
public void handleResults(ResultSet resultSet) throws SQLException {
if (resultSet.next())
{
userTaken = true;
}
else
{
userTaken = false;
}
}
});
return userTaken;
}
public void regUser(String username, String password, String firstname, String lastname, String sexpref) {
String sql = "insert into user values('" + username + "','" + password + "','" + firstname + "','" + lastname + "','" + sexpref +"'\"";
executeQuery(sql, new ResultSetCallback() {
public void handleResults(ResultSet resultSet) throws SQLException {
}
});
}
public void executeQuery(String query, ResultSetCallback callback) {
Statement statement = null;
ResultSet resultSet = null;
try {
statement = connection.createStatement();
if (statement.execute(query)) {
resultSet = statement.getResultSet();
// loop through the results
callback.handleResults(resultSet);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(statement, resultSet);
}
}
public void close(Statement statement, ResultSet resultSet) {
try {
if (resultSet != null) resultSet.close();
if (statement != null) statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
ConnectionManager
package luvalarm;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectionManager {
private Connection connection;
private static ConnectionManager instance;
private static final String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
private static final String connectionString = "jdbc:odbc:luvadb";
private ConnectionManager() {
try {
Class.forName(driver);
this.connection = DriverManager.getConnection(connectionString);
} catch(ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
private Connection getConnection() {
return connection;
}
private void close() {
try {
if(connection != null) connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static Connection createConnection() {
if(instance == null) {
instance = new ConnectionManager();
}
return instance.getConnection();
}
public static void closeConnection() {
if(instance != null) instance.close();
}
}
If anyone knows any good video or detailed tutorials for a registration system that separates out the connection, database queries, servlets and jsp, then please let me know.
Alternatively, if anyone wants to check out my code, laugh at it, then suggest improvements, that would be even better. Please talk to me like I'm an idiot, which is abundantly the case
Andy