Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: JSP error

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JSP error

    Hi,
    I'm having a problem with a JSF managed bean. This is the source code:

     
    package com.dx.sr_57;
    /** include default packages for Beans */
    import java.io.Serializable;
    import javax.enterprise.context.SessionScoped;
       // or import javax.faces.bean.SessionScoped;
    import javax.inject.Named;
    /** include package for SHA-256 encryption */
    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
    /** SQL Packages */
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import javax.sql.DataSource;
    import javax.annotation.Resource;
       // or import javax.faces.bean.ManagedBean;   
     
     
    @Named("loginController")
    @SessionScoped
    public class user_check implements Serializable {
        private String user;
        private String password;    
     
           public user_check(){
           }
     
           /** Call the Oracle JDBC Connection driver */
           @Resource(name="java:/Oracle")
           private DataSource ds;
     
           /** get the content of the variables from the JSF Login page */
           public void setUser(String newValue) { 
               user = newValue; 
           }
     
           public String getUser(){
               return user;       
           }
     
           public void setPassword(String newValue) { 
               password = newValue; 
           } 
     
           public String getPassword(){
               return password;
           }
     
           /** method for converting simple string into SHA-256 hash */
           public String string_hash(String hash) throws NoSuchAlgorithmException{
     
                MessageDigest md = MessageDigest.getInstance("SHA-256");
                md.update(hash.getBytes());
     
                byte byteData[] = md.digest();
     
                /** convert the byte to hex format */
                StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < byteData.length; i++) {
                sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
                }              
               return sb.toString();
           }
     
           /** method for checking password into the Oracle database */
           public String CheckUserDB(String userToCheck) throws SQLException {
                String storedPassword;
                if (ds == null) throw new SQLException("No data source");      
           Connection conn = ds.getConnection();
                if (conn == null) throw new SQLException("No connection");      
     
           try {
                conn.setAutoCommit(false);
                boolean committed = false;
                    try {
                           PreparedStatement passwordQuery = conn.prepareStatement(
                                "SELECT passwd from USERS WHERE userz = ?");
                           passwordQuery.setString(1, userToCheck);
     
                           ResultSet result = passwordQuery.executeQuery();
     
                           result.next();
     
                           storedPassword = result.getString("passwd");                          
     
                           conn.commit();
                           committed = true;
                     } finally {
                           if (!committed) conn.rollback();
                           }
                }
                    finally {               
                    conn.close();
     
                    }       
           return storedPassword;
     
           }
     
     
     
           /** compare the user and the password */
           public String user_compare() throws NoSuchAlgorithmException, SQLException { 
                String hash_passwd;           
                String passwdQuery;
     
                passwdQuery = CheckUserDB(user);
     
                /** convert the plain password in SHA-256 hash */
                hash_passwd = string_hash(password);                                  
     
                if (hash_passwd.equals(passwdQuery)){
                    return "success";        
                } else {
                    return "failure";               
                }
     
           }      
     
     
    }

    I can compile it but when I run it on JBoss 7 this error appears:

    exception
     
    javax.servlet.ServletException: java.sql.SQLException: Exhausted Resultset
    	javax.faces.webapp.FacesServlet.service(FacesServlet.java:606)
     
    root cause
     
    javax.faces.el.EvaluationException: java.sql.SQLException: Exhausted Resultset
    	javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
    	com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    	javax.faces.component.UICommand.broadcast(UICommand.java:315)
    	javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
    	javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
    	com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    	com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    	com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    	javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
     
    root cause
     
    java.sql.SQLException: Exhausted Resultset
    	oracle.jdbc.driver.OracleResultSetImpl.getString(OracleResultSetImpl.java:1270)
    	oracle.jdbc.driver.OracleResultSet.getString(OracleResultSet.java:494)
    	org.jboss.jca.adapters.jdbc.WrappedResultSet.getString(WrappedResultSet.java:1359)
    	com.dx.sr_57.user_check.CheckUserDB(user_check.java:100)
    	com.dx.sr_57.user_check.user_compare(user_check.java:123)
    	sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    	sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    	java.lang.reflect.Method.invoke(Method.java:601)
    	org.apache.el.parser.AstValue.invoke(AstValue.java:196)
    	org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
    	com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
    	javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
    	com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    	javax.faces.component.UICommand.broadcast(UICommand.java:315)
    	javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
    	javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
    	com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    	com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    	com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    	javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)

    Can you help me o fix the problem?

    Regards
    Peter


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: JSP error

                           passwordQuery.setString(1, userToCheck);
                           ResultSet result = passwordQuery.executeQuery();
     
                           result.next();

    In reference to the above code, are you sure the user is in the database? What do you think would happen when the user is not found - the ResultSet is empty - and you try to call next on the ResultSet?

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: JSP error

    This thread has been cross posted here:

    http://www.java-forums.org/javaserver-faces/52623-jsp-error.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting