Hi everyone,
I'm working on
jsp with eclipse and
tomcat and for back-end I'm using Postgresql database. My connection with database and server is working fine as I have tested it by printing few things but when I made a little bit bigger form for login page then I'm getting some errors.
My code for
Login.jsp is given below-
<%@ page language="java" import="java.sql.*" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.io.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login</title>
<script type="text/javascript">
function validate()
{
if (isName() && isPass() && isno())
{
var hash = str_md4(document.forms[0].password.value);
document.forms[0].password.value=hash;
document.forms[0].submit();
}
}
function isName()
{
var str = document.forms[0].userid.value;
if (str == "")
{
alert("\nThe Login Name field is blank .\n\nPlease re-enter your Login Name.")
return false;
}
if((str.substring(0,1)<"a" || str.substring(0,1)>"z") && (str.substring(0,1)<"A" || str.substring(0,1)>"Z"))
{
alert("The Login Name should begin with an alphabetic character.");
return false;
}
for (var i = 1; i < str.length; i++)
{
var ch = str.substring(i, i + 1);
if ( ((ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch)) && (ch < "0" || "9" < ch) && (ch != '_'))
{
alert("\nThe Login Name field accepts letters,numbers & underscore.\n\nPlease re-enter your Login Name.");
return false;
}
}
return true;
}
// Password Validator
function isPass()
{
var str = document.forms[0].password.value;
if ((str == "") || (str.length < 5))
{
alert("\nThe PASSWORD field is either empty or less than 5chars.\n\nPlease re-enter your Password.")
return false;
}
return true;
}
</script>
</head>
<body background="back.gif">
<center>
<strong>Enter your ID and password</strong>
<pre>
<form name=login action="http://localhost:8080/MusicStore/Login.jsp" method="get" target="rightf" >
<strong>User Id :<input type=text name="userid"size=20>
Password :<input type=password name="password"size=20>
</strong>
<clear=left>
<BUTTON TYPE=BUTTON Style= NAME=button VALUE=Submit onclick=validate()>Submit</BUTTON>
<BUTTON TYPE=Reset Style= NAME=Reset VALUE=Reset>Clear</BUTTON>
<BUTTON TYPE=button Style= NAME=back VALUE=Back onclick=history.back()>Back</BUTTON>
Forgot/Change Password
</form></center>
<%
PrintWriter pw = response.getWriter();
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException cnfe) {
System.out.println("Couldn't find the driver!");
System.out.println("Let's print a stack trace, and exit.");
cnfe.printStackTrace();
System.exit(1);
}
System.out.println("Registered the driver ok, so let's make a connection.");
Connection con = null;
try {
// The second and third arguments are the username and password,
// respectively. They should be whatever is necessary to connect to the database.
con = DriverManager.getConnection("jdbc:postgresql://localhost/postgres","postgres","rahul");
} catch (SQLException se) {
System.out.println("Couldn't connect: print out a stack trace and exit.");
se.printStackTrace();
System.exit(1);
}
if (con != null)
System.out.println("Connected to the database!");
else
System.out.println("Connection Failed.");
try{
boolean valid=false;
boolean valid1=false,valid2=false,valid3=false;
String uname=null;
String userid = request.getParameter("userid").toLowerCase().trim();
String password = request.getParameter("password").trim();
// Get a statement from the connection
Statement stmt = con.createStatement() ;
// Execute the query
ResultSet rs = stmt.executeQuery( "SELECT user_id,password FROM new_user" ) ;
// System.out.println("Id \tName \tAddress");
// Loop through the result set
// Close the result set, statement and the connection
int count = 0;
while(rs.next())
{
String tempuserid = rs.getString(1).trim();
String temppassword = rs.getString(2).trim();
if ((userid.equalsIgnoreCase(tempuserid))&&(password.equalsIgnoreCase(temppassword)));
{
valid=true;
}
if ((userid.equalsIgnoreCase(tempuserid)))
{
valid1=true;
}
if ((password.equalsIgnoreCase(temppassword)))
{
valid2=true;
}
if (valid==true)
{
String dupuserid = userid.toLowerCase();
pw.println("<html>");
pw.println("<body>");
pw.println("<center>");
pw.println("<h2><font color=navy><strong>Welcome "+userid+" !!!</strong><br></font></h3>");
pw.println("</body>");
pw.println("</html>");
}
if (valid1==false)
{
pw.println("<html>");
pw.println("<body background=\"http://localhost:8080/back.gif\">");
pw.println("<h3><Strong><font color=red><center>No user found in the server with this Id...try again</center></font></strong></h3>");
pw.println("<BUTTON TYPE=Button NAME=back VALUE=Back onclick=history.back()><b>Back</b></BUTTON> ");
pw.println("</body>");
pw.println("</html>");
pw.close();
}
if (valid2==false)
{
pw.println("<html>");
pw.println("<body>");
pw.println("<h4><Strong><font color=red>Sorry..U have Entered wrong password...try again </strong></h4>");
pw.println("</body>");
pw.println("</html>");
}
System.out.println(""+rs.getString(1)+"\t"+rs.getString(2)+"\t"+rs.getString(3));
count++;
}
rs.close() ;
stmt.close() ;
con.close() ;
}catch (SQLException e) {
System.out.println("Could not create statement in JDBC");
e.printStackTrace();
}
%>
</body>
</html>
The error message which I'm getting is:-
HTTP Status 500 -
--------------------------------------------------------------------------------
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: An exception occurred processing JSP page /Login.jsp at line 113
110: boolean valid=false;
111: boolean valid1=false,valid2=false,valid3=false;
112: String uname=null;
113: String userid = request.getParameter("userid").toLowerCase().trim();
114: String password = request.getParameter("password").trim();
115: // Get a statement from the connection
116: Statement stmt = con.createStatement() ;
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
root cause
java.lang.NullPointerException
org.apache.jsp.Login_jsp._jspService(Login_jsp.java:175)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.23 logs.
--------------------------------------------------------------------------------
Apache Tomcat/7.0.23
Please help me out with this one. I've a table named new_user in database which has two fields user_id and password.
Thanks in advance.