Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Oct 2008
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Unhappy Simple login page with MySQL and Servlets

    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:

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

    And this is my servlet:

    Code:
    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() {
        super();
      }   	
    	
      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 {
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?user=root&password=root");
    stmt = con.createStatement();
    	try{
    	  System.out.print(
        "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);
    	return;
        }
        out = response.getWriter();
        header = "<html>\n<head><title>Processing" +
          " DataBase</title></head>\n<BODY BGCOLOR=\"#FDF5E6\">\n";
        if(name == "" || password == ""){
          printEmptyNameAndPassword();
    	return;
        }
        if(register != null)
    processRegistration(stmt, name, password);
        else
    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)
    		  printPleaseRegisterFirst(name);
    	  else if(name!=null && password!=null)
    			  printThanksForRegisteringWithus(name); 
    	  else
    		  printYouHaveAlreadyRegistered(name);
    	  
      }
    	
      private void processLogin(Statement stmt, String name, 
        String password){
    	  if(name!=null && password!=null)
    		  wellcomeBack(name);
    	  
      }
    	
      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

  • #2
    New to the CF scene
    Join Date
    Apr 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm assuming since your last post, 4/25, you've found the solution?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •