• 0

[JAVA/Servlet] Need help finding whats wrong with my code


Question

I've been trying to create a simple login page following the tutorial found here.

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, Glassfish 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

<html>
<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>

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

web.xml

<?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>

build.xml (Ant build file)

<project name="LoginForm" basedir=".">

	<property name="app.name" value="LoginForm"/>

	<property name="src.dir" location="src"/>
	<property name="build.dir" location="build"/>
	<property name="build.webinf.classes.dir" location="${build.dir}/WEB-INF/classes"/>
	<property name="web.dir" location="web"/>
	<property name="dist.dir" location="dist"/>
	<property name="lib.dir" location="lib"/>

	<property name="tomcat.home" location="/home/ruzaik/software/tomcat/apache-tomcat-5.5.20"/>
	<property name="tomcat.url" value="http://localhost:8080/manager"></property>
	<property name="tomcat.username" value="tomcat"/>
	<property name="tomcat.passward" value="tomcat"/>

	<property name="appserver.deploy.dir" location="${tomcat.url}/webapps"></property>
	<property name="war.file" value="${app.name}.war"/>

	<import file="${tomcat.home}/bin/catalina-tasks.xml"/>

	<path id="project.classpath">
		<dirset dir="${build.dir}"/>
		<fileset dir="${lib.dir}">
			<include name="**/*.jar"/>
		</fileset>
	</path>

	<target name="clean" description="Cleans directories">
		<delete dir="${build.dir}"/>
		<delete dir="${dist.dir}"/>
	</target>

	<target name="init" depends="clean" description="Creates directories">
		<mkdir dir="${build.dir}"/>
		<mkdir dir="${build.webinf.classes.dir}"/>
		<mkdir dir="${dist.dir}" />
	</target>

	<target name="compile" depends="init">
		<javac srcdir="${src.dir}" destdir="${build.webinf.classes.dir}">
			<classpath refid="project.classpath"/>
		</javac>
	</target>

	<target name="copy" description=" : Copy the content of web in to build directoy">
		<copy todir="${build.dir}" preservelastmodified="yes" overwrite="yes">
			<fileset dir="${web.dir}">
				<include name="**/*"/>
				<exclude name="**/*.bak"/>
			</fileset>
		</copy>
	</target>

	<target name="war" depends="compile, copy" description=" : Create a war file for deploying">
		<jar destfile="${dist.dir}/${app.name}.war" update="false" compress="true">
			<fileset dir="${build.dir}">
				<include name="**/*"/>
			</fileset>
		</jar>
	</target>

	<target name="install">
		<deploy url="${tomcat.url}" username="tomcat" password="tomcat" path="/LoginForm"
				war="${dist.dir}/${war.file}">
		</deploy>
	</target>

	<target name="uninstall">
		<undeploy url="${tomcat.url}" username="tomcat" password="tomcat" path="/LoginForm">
		</undeploy>
	</target>

</project>

The following is my directory structure

tempf.png

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:

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

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:

Edited by rustix
Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0

Thats not a problem with your code. For some reason you Java code isn't finding the mysql JDBC. It's hasn't even made the database connection yet (has failed trying to do so). The jar file is probably in the wrong place.

Copy the JDBC jar file to: $GLASS_HOME/lib - sorry I'm not familiar with GlassFish but thats basically GlassFish library folder.

Also it may be worth looking up JDBC realms. It will do it for you given a few parameters such as db table names and fields:

http://blogs.sun.com/swchan/entry/jdbcreal...fish_with_mysql

Edited by WarStorm
Link to comment
Share on other sites

  • 0

oops.. sorry I moved the project on to Tomcat since it's used more than Glassfish. I just adjusted it in my first post. However I still get the same exception. Do you suggest putting the mysql connector library in the $CATALINA_HOME/lib directory?

Link to comment
Share on other sites

This topic is now closed to further replies.