View Full Version : Simple login page with MySQL and Servlets

04-25-2009, 09:54 PM
Hi, I'm trying to create a simple login page where the user can login or register their names into the MYSQL database. Here are the conditions that I'm using for this one:

1. If a user types in the name and password and clicks register for the first time, the message should read "Thank you [person's name here] for registering".
2. If a user types in the name and password and clicks login for the first time, it should say "Please register."
3. However if the person registered already and enters the information again and clicks login, the message should read "Welcome back [put person's name here]".

I am using Eclipse to run my servlet and I already have the MYSQL database connected already. My problem is that when I run it, it keeps asking me if I would like to drop the table. I believe I did something wrong with the coding. Please help!

Here is the HTML file for it:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Simple Login Page</title>
<form action="http://localhost/LoginServlet/SendName">
User Name:
<input name="name" type="text" value="">
<input name="password" type="password" value="">
<input name="register" type="Submit" value="Register">
<input name="login" type="Submit" value=" Login ">

And this is my servlet:

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.sql.*;
import java.io.*;

public class SendName extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
static final long serialVersionUID = 1L;
PrintWriter out = null;
String header = null;

public SendName() {

protected void doGet(HttpServletRequest request,
HttpServletResponse response)throws ServletException, IOException{
String name = request.getParameter("name");
String password = request.getParameter("password");
String register = request.getParameter("register");
String login = request.getParameter("login");
Statement stmt = null;
try {

Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?user=root&password=root");
stmt = con.createStatement();
"Would you like to remove the table simple [y/n]? ");
if(new java.util.Scanner(System.in).nextLine().charAt(0) == 'y')
stmt.execute("DROP TABLE simple");
stmt.execute("CREATE TABLE simple( name char(30), password char(30))");
}catch(Exception e){
}catch(Exception e){
System.out.println("Error 1: " + e);
out = response.getWriter();
header = "<html>\n<head><title>Processing" +
" DataBase</title></head>\n<BODY BGCOLOR=\"#FDF5E6\">\n";
if(name == "" || password == ""){
if(register != null)
processRegistration(stmt, name, password);
processLogin(stmt, name, password);

System.out.println("Program terminated with no error.");

private void processRegistration(Statement stmt, String name,
String password){

if(name==null && password==null)
else if(name!=null && password!=null)


private void processLogin(Statement stmt, String name,
String password){
if(name!=null && password!=null)


private void printEmptyNameAndPassword(){

out.print("Please enter your name and password");

private void printYouHaveAlreadyRegistered(String name){

out.print(name + " you have already registered");

private void printThanksForRegisteringWithus(String name){

out.print(name + " thank you for registering with us");

private void wellcomeBack(String name){

out.print("Welcome back " + name);

private void printPleaseRegisterFirst(String name){

out.print("Please register first");

It would be greatly appreciate if anyone can figure out what I did wrong :confused:

04-29-2010, 05:36 PM
I'm assuming since your last post, 4/25, you've found the solution?