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

Dani AI

Generated

Several likely causes for the runtime error described by are common and easy to verify: the servlet URL used by the form may not be mapped, the JDBC driver JAR might be missing from the deployed application or server classpath, SQL/logic mistakes can produce unexpected results, and exceptions can be hidden when only printed to the server console. The application server log (NetBeans Output or the server log files) usually contains a full stack trace and the exact failing line; that is the primary diagnostic.

Checklist to follow (in order):

  • Inspect the server log for the full stack trace. Look for ClassNotFoundException (JDBC driver), SQLException, NullPointerException, or HTTP 404/500 messages.
  • Confirm the MySQL Connector/J JAR is in the WAR’s WEB-INF/lib or the application server’s common lib directory.
  • Ensure the servlet is actually mapped to the URL the form posts to. Either add a web.xml mapping or use an annotation. Example web.xml mapping:
<servlet>
  <servlet-name>LoginAuth</servlet-name>
  <servlet-class>connection.LoginAuthentication</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>LoginAuth</servlet-name>
  <url-pattern>/LoginAuthentication</url-pattern>
</servlet-mapping>
  • Query only the row for the supplied username and use a prepared statement to avoid SQL injection and logic errors. Example pattern:
String sql = "SELECT password_hash FROM users WHERE username = ?";
try (PreparedStatement ps = conn.prepareStatement(sql)) {
  ps.setString(1, usernameFromForm);
  try (ResultSet rs = ps.executeQuery()) {
    if (rs.next()) { /* compare hashes */ } else { /* user not found */ }
  }
}

Best practices and cautions: avoid hard‑coding root credentials — create a least‑privilege DB user or use a container DataSource/JNDI with connection pooling. Never store plaintext passwords; use salted hashes (bcrypt/Argon2/PBKDF2). Close JDBC resources reliably (try‑with‑resources).

As suggested, the connection line is a frequent failure point; checking the stack trace will confirm whether it’s a missing driver or a bad URL/credentials. ’s point about following tutorial details matters: table/column names and package/class names must match the deployed code. Downgrading the IDE as proposed generally won’t fix classpath or mapping problems — fixing configuration and reading the server log will.

Recommended Answers

All 3 Replies

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.