Wrox Programmer Forums
|
Apache Tomcat General discussion of the Apache Tomcat servlet container. For discussions specific to the Professional Apache Tomcat book, please see the book discussion forum for that book.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Apache Tomcat section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 26th, 2007, 05:50 AM
Registered User
 
Join Date: Jun 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default New to this error

Hi,

Sorry guys, I'm starting JSP only. So I hope you could bear w/ me & not loose patience. I have tomcat5.5 and oracle xe. What I doing is to have a login page for all users before using my app. Here's the code for my Users.class:
Code:
/*
 * Users.java
 *
 * Created on June 26, 2007, 3:27 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package biz;

import java.util.Hashtable;
import java.util.ArrayList;
import java.util.Enumeration;
import java.sql.*;

/**
 *
 * @author Me
 */
public class Users {

    public int Id = 0;
    public int groupId = 0;
    public String userName = null;
    public String password = null;

    public static String dbOraDriver = "com.sun.sql.jdbcx.oracle.OracleDataSource";
    public static String dbUrl = "jdbc:oracle:thin:oradb/******@localhost:1521:xe";

    /** Creates a new instance of Users */
    public Connection connect() {
        Connection conn = null;
        try {
            Class.forName(dbOraDriver);
            conn = DriverManager.getConnection(dbUrl);
        }
        catch(Exception e) {}

        return conn;
    }

    public boolean verifyLogin(String UserName, String Password) {
        boolean loginOk = false;
        Connection connect = null;
        try {
            Statement stmt = connect().createStatement();
            String sql = "SELECT count(*) AS cnt FROM users WHERE (username = '" + UserName + "' AND password = '" + Password + "')";
            ResultSet rs = stmt.executeQuery(sql);
            if (rs.next()) { 
                int cnt = rs.getInt("cnt");
                if (cnt > 0) { loginOk = true; }
            }
            connect.close();
        }
        catch (Exception e) {}

        return loginOk;
    }
}
and for my index.jsp
Code:
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%--
The taglib directive below imports the JSTL library. If you uncomment it,
you must also add the JSTL library to the project. The Add Library... action
on Libraries node in Projects view can be used to add the JSTL 1.1 library.
--%>
<%--
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 
--%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<%@ page language="Java" import="javax.naming.*,javax.sql.*,java.sql.*,java.util.*"%>

 <jsp:useBean id="Users"  scope = "page" class ="biz.Users" />
 <jsp:setProperty name ="Users" property = "*" />
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Customer Management System</title>
    </head>
    <body>

    <form name="form1" method="post" action="login.jsp">
      <table border="0" cellspacing="0" cellpadding="2">
        <tr>
          <td>Username</td>
          <td><input name="userName" type="text" size="20" maxlength="20"></td>
        </tr>
        <tr>
          <td>Password</td>
          <td><input name="password" type="password" size="20" maxlength="15"></td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td><input name="btnSubmit" type="submit" id="btnSubmit" value="Submit"></td>
        </tr>
      </table>
    </form>

    </body>
</html>
and my login.jsp to be called by index.jsp once the Submit button is click is:
Code:
<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>CRMS - Login</title>
</head>

<body>
<jsp:useBean id="Users" scope="page" class="biz.Users" />
<jsp:setProperty name="Users" property="userName" />
<jsp:setProperty name="Users" property="password" />
<form>
<input type='hidden' name='userName' value='<jsp:getProperty name="Users" property="userName" />'/>
</form>
</body>
</html>
Thanks guys! I really appreciate all your help!
 
Old August 23rd, 2007, 08:19 AM
Friend of Wrox
 
Join Date: Mar 2007
Posts: 373
Thanks: 0
Thanked 1 Time in 1 Post
Default

Did you forgot something?

You haven't mentioned what you want or what is the error?

Regards,
Rakesh
 
Old September 11th, 2007, 04:45 AM
Registered User
 
Join Date: Aug 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to neo.isbackin.thematrix
Default

Hi,

Well, I havent read your jsp Code but the code in ur 'Users.java' has to fail. It must return you false... Because it is throwing 'NullPointerException'. And you are not able to know it because you havent printed the Stack Trace in your code.. While Debugging, you should write 'e.printStackTrace();' when you catch your exception.

Now about the error in your code.

The Method 'connect()' returns an instance of 'Connection'. In your method 'verifyLogin()' you are declaring a reference of type Connection named 'connect' with pointing to 'null'. Now you are never changing its reference and it always points to 'null' only as you are directly taking statement object from the Connection returned by the method 'connect()' but you dont have ur 'connect' reference pointing to that Connection object. Your code should be,


public boolean verifyLogin(String UserName, String Password) {
        boolean loginOk = false;
        Connection connect = null;
        try {
            connect = connect();
            Statement stmt = connect.createStatement();
            String sql = "SELECT count(*) AS cnt FROM users WHERE (username = '" + UserName + "' AND password = '" + Password + "')";
            ResultSet rs = stmt.executeQuery(sql);
            if (rs.next()) {
                int cnt = rs.getInt("cnt");
                if (cnt > 0) { loginOk = true; }
            }
            connect.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }

        return loginOk;
    }








Similar Threads
Thread Thread Starter Forum Replies Last Post
Insert Query Error & Run-Time Error 3022 DavidWE Access 1 July 31st, 2008 11:17 AM
Ch 4: Parse error: syntax error, unexpected T_SL hanizar77 BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0 0 June 23rd, 2008 09:17 PM
[Resolved] Error calling a sp - parameter error snufse .NET Framework 2.0 2 February 12th, 2008 04:46 PM
Parse error: syntax error, unexpected T_STRING ginost7 Beginning PHP 1 November 9th, 2007 02:51 AM
Phile Page error, visual studio error reps BOOK: ASP.NET Website Programming Problem-Design-Solution 0 September 27th, 2003 10:11 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.