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