Hi.
I am using windows, and NetBeans version 6.1

I create a JSP project:
and then I create an index.jsp in web folder
and then I created a LoginAuthentication class in a package named connection.

BUT I dont know why when I click the singin button of the index.jsp, an error displays.

here is my code:

index.jsp

<%@ page language="java" %>
<html>
<head>
<title>Login Page</title>
<script language = "Javascript">
function Validate(){
var user=document.frm.user
var pass=document.frm.pass

if ((user.value==null)||(user.value=="")){
alert("Please Enter user name")
user.focus()
return false
}
if ((pass.value==null)||(pass.value=="")){
alert("Please Enter password")
pass.focus()
return false
}
return true
}
</script>
</head>
<body>
<h1>Login
<br>
</h1>
<form name="frm" action="LoginAuthentication" method="Post" onSubmit="return Validate()" >
<table>
    <tr>
        <td>UserID</td>
        <td><input type="text" name="user" value=""/></td>
    </tr>

    <tr>
        <td>Password</td>
        <td><input type="password" name="pass" value=""/></td>
    </tr>
    
    <tr>
        <td><input type="submit" value="Sign-In" /></td>
        <td><input type="reset" value="Reset" /></td>
    </tr>
</table>
</form>
</body>
</html>

LoginAuthentication

package connection;

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class LoginAuthentication extends HttpServlet{

  private ServletConfig config;
  
    @Override
public void init(ServletConfig config) throws ServletException{
     this.config=config;
 }
    @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
      
    PrintWriter out = response.getWriter();
    String connectionURL = "jdbc:mysql://localhost:3306/danijsptutorial";
    Connection connection=null;
    ResultSet rs;
    String userName=new String("");
    String passwrd=new String("");
    response.setContentType("text/html");
    try {
       // Load the database driver
      Class.forName("com.mysql.jdbc.Driver");
      // Get a Connection to the database
      connection = DriverManager.getConnection(connectionURL, "root", "root"); 
      //Add the data into the database
      String sql = "select uid,password from User";
      Statement s = connection.createStatement();
      s.executeQuery (sql);
      rs = s.getResultSet();
      while (rs.next ()){
        userName=rs.getString("uid");
        passwrd=rs.getString("password");
      }
      rs.close ();
      s.close ();
      }catch(Exception e){
      System.out.println("Exception is ;"+e);
      }
      if(userName.equals(request.getParameter("user")) && passwrd.equals(request.getParameter("pass"))){
        out.println("WELCOME "+userName);
      }
      else{
        out.println("Please enter correct username and password");
        out.println("<a href='index.jsp'><br>Login again</a>");
      }
  }
}

What is the error, at what line does it occur, I am pretty sure Glassfish orTomcat whichever server you are using gives you the exact error and the exact line number on which it thinks the problem exists.

connection = DriverManager.getConnection(connectionURL, "root", "root");

Using root account of the MySQL server for performing normal tasks, haven't you ever read anything about security / Safety on MySQL databases.

Also use consistent indentation in your Code, I know for a fact that Netbeans automatically indents your code so you have no right to post this inconsistently formatted code for others to view.

String connectionURL = "jdbc:mysql://localhost:3306/danijsptutorial";
    
      String sql = "select uid,password from User";

Was there any specific reason why you made all these changes to my original tutorial?

Hello freind ,
According to me your programme is right so please try to these progrrame on netbeans 5.0 instead of 6.1.


Hi.
I am using windows, and NetBeans version 6.1

I create a JSP project:
and then I create an index.jsp in web folder
and then I created a LoginAuthentication class in a package named connection.

BUT I dont know why when I click the singin button of the index.jsp, an error displays.

here is my code:

index.jsp

<%@ page language="java" %>
<html>
<head>
<title>Login Page</title>
<script language = "Javascript">
function Validate(){
var user=document.frm.user
var pass=document.frm.pass

if ((user.value==null)||(user.value=="")){
alert("Please Enter user name")
user.focus()
return false
}
if ((pass.value==null)||(pass.value=="")){
alert("Please Enter password")
pass.focus()
return false
}
return true
}
</script>
</head>
<body>
<h1>Login
<br>
</h1>
<form name="frm" action="LoginAuthentication" method="Post" onSubmit="return Validate()" >
<table>
    <tr>
        <td>UserID</td>
        <td><input type="text" name="user" value=""/></td>
    </tr>

    <tr>
        <td>Password</td>
        <td><input type="password" name="pass" value=""/></td>
    </tr>
    
    <tr>
        <td><input type="submit" value="Sign-In" /></td>
        <td><input type="reset" value="Reset" /></td>
    </tr>
</table>
</form>
</body>
</html>

LoginAuthentication

package connection;

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class LoginAuthentication extends HttpServlet{

  private ServletConfig config;
  
    @Override
public void init(ServletConfig config) throws ServletException{
     this.config=config;
 }
    @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
      
    PrintWriter out = response.getWriter();
    String connectionURL = "jdbc:mysql://localhost:3306/danijsptutorial";
    Connection connection=null;
    ResultSet rs;
    String userName=new String("");
    String passwrd=new String("");
    response.setContentType("text/html");
    try {
       // Load the database driver
      Class.forName("com.mysql.jdbc.Driver");
      // Get a Connection to the database
      connection = DriverManager.getConnection(connectionURL, "root", "root"); 
      //Add the data into the database
      String sql = "select uid,password from User";
      Statement s = connection.createStatement();
      s.executeQuery (sql);
      rs = s.getResultSet();
      while (rs.next ()){
        userName=rs.getString("uid");
        passwrd=rs.getString("password");
      }
      rs.close ();
      s.close ();
      }catch(Exception e){
      System.out.println("Exception is ;"+e);
      }
      if(userName.equals(request.getParameter("user")) && passwrd.equals(request.getParameter("pass"))){
        out.println("WELCOME "+userName);
      }
      else{
        out.println("Please enter correct username and password");
        out.println("<a href='index.jsp'><br>Login again</a>");
      }
  }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.