Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Need help finding whats wrong with my servlet

843789Mar 26 2009 — edited Mar 26 2009
I've been trying to create a simple login page following the tutorial found http://www.roseindia.net/mysql/loginauthentication.shtml

There is a single JSP (AuthenticateLogin.jsp) and a servlet (LoginAuthentication.java). Basically what has to happen is the user enters the login information in the JSP which sends these info to the servlet. The servlet checks whether the username and password exist in the table and if they do it displays the user if not it says to that the username and/or password is invalid. I'm using IntelliJ IDEA as the IDE, Tomcat as the application server and MySQL 5.0.18 for the database. I'm also using Ant as a build tool.

The following are my code:

AuthenticateLogin.jsp
<head>
	<title>Login Page</title>

	<script type="text/javascript">
		function validate() {
			var user = document.frm.user
			var pass = document.frm.pass

			if ((user == null) || (user == "")) {
				alert("Please enter a username")
				user.focus()
				return false
			}

			if ((pass == null) || (pass == "")) {
				alert("Please enter a password")
				pass.focus()
				return false
			}
		}
	</script>
</head>

<body>
<form name="frm" action="LoginAuthentication" method="post" onsubmit="return validate()">
	Name:       <input type="text" name="user"/>
	<br/>
	Password:   <input type="text" name="pass" />
	<br />
	<input type="submit" value="Sign in"/>      
	<input type="reset" value="Reset"/>
</form>
</body>
</html>{code}

LoginAuthentication.java
{code:java}import java.io.*;*
*import java.sql.*;
import javax.servlet.*;*
*import javax.servlet.http.*;

public class LoginAuthentication extends HttpServlet {

	private ServletConfig config;

	public void init (ServletConfig config) throws ServletException {
		this.config = config;
	}

	public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		PrintWriter out = response.getWriter();

		String connectionUrl = "jdbc:mysql://192.168.0.95:3306/loginTester";
		Connection connection = null;
		ResultSet rs;
		String userName = new String("");
		String passwrd = new String("");
		response.setContentType("text/html");

		try {
			Class.forName("com.mysql.jdbc.Driver");
			connection = DriverManager.getConnection(connectionUrl, "root", "");
			String sql = "select user, password from user";
			Statement s = connection.createStatement();
			s.executeQuery(sql);
			rs = s.getResultSet();

			while (rs.next()) {
				userName = rs.getString("user");
				passwrd = rs.getString("password");
			}

			rs.close();
			s.close();
		} catch (Exception e){
			System.out.println("Exception thrown: ["+e+"}");
		}

		if (userName.equals(request.getParameter("user")) && passwrd.equals(request.getParameter("pass"))) {
//			response.sendRedirect("http://localhost:8080/xplanner_reports/");
			out.println("Hello"+userName);
		} else {
			out.println("Please enter a valid username and password");
			out.println("<a href='AuthenticateLogin.jsp'><br/>Login again</a>");
		}
	}
}{code}

web.xml
{code:java}<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
		   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
		  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
		   version="2.5">

	<welcome-file-list>
		<welcome-file>AuthenticateLogin.jsp</welcome-file>
	</welcome-file-list>

	<servlet>
		<servlet-name>LoginAuthentication</servlet-name>
		<servlet-class>LoginAuthentication</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>LoginAuthentication</servlet-name>
		<url-pattern>/LoginAuthentication</url-pattern>
	</servlet-mapping>

</web-app>

</web-app>{code}
 
The problem I'm facing is the validation (checking whether the username and password match to those in the database) isn't working properly. No matter what I enter as the username and password (even if it is the correct pair) it always shows as my username and/or password is invalid. When I check the tomcat (catalina) log the following entry was found:

{code:java}Exception thrown: [java.lang.ClassNotFoundException: com.mysql.jdbc.Driver}{code}

Could someone please show me what I could be doing wrong here? It would be a great help.  Spent a day trying to figure this out :no:                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 23 2009
Added on Mar 26 2009
4 comments
161 views