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>");
}
}
}
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:
Question
rustix
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
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
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:
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 rustixLink to comment
Share on other sites
4 answers to this question
Recommended Posts