#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Posts
    0
    Rep Power
    0

    Nullpointerexception


    I keep getting a nullpointerexception for the following pieces of code, can anyone find out what the prob is:

    Login.jsp is as follows:

    <HTML>
    <HEAD>
    <TITLE>Registration Login Form</TITLE>
    </HEAD>
    <BODY>
    <form method="POST" action="ValidateLogin.jsp">
    <table border="0" cellpadding="0" cellspacing="0" width="100%"
    height="69">
    <tr>
    <td width="1%" height="23">UserId</td>
    <td width="70%" height="23"><input type="text" name="userid"
    size="20"></td>
    </tr>
    <tr>
    <td width="1%" height="25">Password</td>
    <td width="70%" height="25"><input type="password" name="password"
    size="20">
    </td>
    </tr>
    <tr>
    <td width="1%" height="21"></td>
    <td width="70%" height="21"></td>
    </tr>
    </table>
    <p>
    <input type="submit" value="Submit" name="SubmitBtn">
    <input type="reset" value="Reset" name="B2">
    </p>
    </form>
    </HTML>


    ValidateLogin.jsp is as follows:

    <%-- Import required Java Classes --%>
    <%@ page import="javax.servlet.*" %>
    <%@ page import="javax.servlet.http.*" %>
    <%@ page import="java.sql.*" %>

    <%
    String user = request.getParameter("userid");
    String pass = request.getParameter("password");

    Connection con=null;
    Statement stmt=null;
    ResultSet rs=null;

    String queryText="SELECT * FROM Register WHERE Username = '" + user + "' AND Password = '" + pass +"'";

    try {
    //initialize the JDBC-ODBC bridge driver
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

    //open a connection to the database
    String username = "";
    String password = "";
    con = DriverManager.getConnection("jdbcdbc:jsp_dbase",username,password);

    //execute the query
    stmt = con.createStatement();
    rs = stmt.executeQuery(queryText);
    } catch (Exception e) { }
    %>

    %>

    <HTML>
    <HEAD>
    <TITLE>Model 1 Architecture</TITLE>
    </HEAD>
    <BODY>
    <table border="0" cellspacing="0" cellpadding="5">
    <tr>
    <td>
    <div align="right">First name:</div>
    </td>
    <td><b><%= rs.getString("FirstName") %></b> </td>
    </tr>
    <tr>
    <td>
    <div align="right">Last name:</div>
    </td>
    <td><b><%= rs.getString("LastName") %></b> </td>
    </tr>
    </table>
    </body>
    </html>
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Posts
    0
    Rep Power
    0

    Got the last bit working


    I got hte above working but can anyone tell me how i can forward the user to an errorpage if they enter an incorrect username or password.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2001
    Posts
    45
    Rep Power
    13
    if ( "condition valid login" )
    {
    // go to valid login
    }else
    resp.sendRedirect("your error page")
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2002
    Posts
    2
    Rep Power
    0

    Error


    I've tried the above code but I am getting the following error

    Method redirect(java.lang.String) not found in interface javax.servlet.http.HttpServletResponse.

    Can you help me with this error please, this is what my code looks like

    <%-- Import required Java Classes --%>
    <%@ page import="javax.servlet.*" %>
    <%@ page import="javax.servlet.http.*" %>
    <%@ page import="java.sql.*" %>

    <%
    String user = request.getParameter("Username");
    String pass = request.getParameter("Password");

    Connection con=null;
    Statement stmt=null;
    ResultSet rs=null;

    if ((user == null)||(pass == null))
    {
    response.redirect("indexerror.jsp");
    }


    String queryText="SELECT * FROM User WHERE UserID = '" + user + "' AND Password = '" + pass +"'";

    try {
    //initialize the JDBC-ODBC bridge driver
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

    //open a connection to the database
    String username = "";
    String password = "";
    con = DriverManager.getConnection("jdbcdbc:db",username,password);

    //execute the query
    stmt = con.createStatement();
    rs = stmt.executeQuery(queryText);
    } catch (Exception e) { }

    if ((user == "UserID") && (pass == "Password"))
    {
    response.redirect("irishshareprices.jsp");
    }
    else
    {
    response.redirect("indexerror.jsp");
    }

    %>
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2001
    Posts
    45
    Rep Power
    13
    Sorry, I was out of town for the weekend.

    You have response.redirect()

    It should be response.sendRedirect()

    That should fix that! Let me know if you need any more help.
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2002
    Posts
    2
    Rep Power
    0

    Unhappy Still not working


    Thanks for your help.

    When I put in response.sendRedirect I got the following error:

    The server encountered an internal error (Internal Server Error) that prevented it from fulfilling this request.

    I'm not sure what the problem is, any ideas?
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2002
    Posts
    2
    Rep Power
    0

    Unhappy Login Screen


    I got that problem fixed thanks but i have a new one now

    I am working on the login screen so that when an invalid user logs in they go directly to the error page but when a registered user logs in they go to the first page which is called irishshareprices.jsp. The problem is that no matter if it's a registered user or not it's automatically going to the indexerror.jsp page.

    The following is the code i'm using:

    <%-- Import required Java Classes --%>
    <%@ page import="javax.servlet.*" %>
    <%@ page import="javax.servlet.http.*" %>
    <%@ page import="java.sql.*" %>

    <%
    String user = request.getParameter("username");
    String pass = request.getParameter("password");

    Connection con=null;
    Statement stmt=null;
    ResultSet rs=null;

    /*if ((user == null)||(pass == null))
    {
    response.sendRedirect("indexerror.jsp");
    }*/


    String queryText="SELECT * FROM User WHERE UserID = '" + user + "' AND Password = '" + pass +"'";

    try {
    //initialize the JDBC-ODBC bridge driver
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

    //open a connection to the database
    String username = "";
    String password = "";
    con = DriverManager.getConnection("jdbcdbc:db",username,password);

    //execute the query
    stmt = con.createStatement();
    rs = stmt.executeQuery(queryText);
    } catch (Exception e) { }


    if ((user == rs.getString("UserID")) && (pass == rs.getString("Password")))
    {
    response.sendRedirect("irishshareprices.jsp");
    }
    else
    {
    response.sendRedirect("indexerror.jsp");
    }

    %>
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2001
    Posts
    45
    Rep Power
    13
    From a quick look try this....

    if ((user.equals(rs.getString("UserID"))) && (pass.equals(rs.getString("Password"))))


    Just stands out to me. Let me know if this doesn't work and i will look at it closer when I get out of work.
  16. #9
  17. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2002
    Posts
    2
    Rep Power
    0
    Hi, thanks for your suggestion, but i'm afraid it didn't work. It gave an internal server error. Is there anyway I can check if there is anything in rs or if it's picking up any records from the database. I can't understand why the code isn't working. I put this line in:

    if ((user.equals("UserID")) && (pass.equals("Password")))

    but it's still going to the error page instead of the proper one.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2001
    Posts
    45
    Rep Power
    13
    Sorry, I should have caught this earlier. Your not getting any rows, thats why your getting the error page. Your not getting any data. rs.next() or rs.getRow()
  20. #11
  21. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2002
    Posts
    2
    Rep Power
    0

    Thanks


    Hi

    I got it working eventually, thanks very much. This is what i used:

    rs = stmt.executeQuery(queryText)
    boolean found = rs.next();

    if (found)
    {
    response.sendRedirect"irishshareprices.jsp");
    }
    else
    {
    response.sendRedirect("indexerror.jsp");
    }

    } catch (Exception e) { }
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2001
    Posts
    45
    Rep Power
    13
    Glad you got it working! Something I should mention is that you need to make sure you close your connection! Also close your query! and set them to null. This is good rule. Otherwise you will have major problems if your site gets any kind of traffic. Open connections will kill your site(deadlock) and open querys(database cursors) will run your site out of memory. These are lessons that I had to learn the hard way!! Just thought I would mention it.

    Anyways, good luck with your project and if you need help along the road, I tend to check these boards often so ask away!
    Best of luck,
    Mark
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2001
    Posts
    45
    Rep Power
    13
    One more thing about closing connection and querys.....

    try
    {
    //get connection
    //run query
    // login
    //close query
    //close connection
    // con=null;
    // rs=null;
    }
    catch
    {
    // exceptions
    }
    finally
    {
    // if connection and query !=null
    // close them
    }

    finally block will always get executed!!!!

IMN logo majestic logo threadwatch logo seochat tools logo