Solved

Servlet Error

Posted on 2004-10-28
157 Views
Last Modified: 2013-11-24
This is pretty easy,
I can't get the boolean value allOk to "work" for lack of a better term.
It is a scoping issue and also pertains to my relative inexperience with java
please help.

        private boolean validateChanges(Connection cn, HttpServletRequest request)
        throws ServletException, IOException {
               
               try
               {      
                     boolean allOk=true;
                    String user = (String)request.getRemoteUser();
                    String query = "SELECT user_pass FROM users WHERE user_name = \"" + request.getRemoteUser() + "\";";
                    Statement statement = cn.createStatement();
                ResultSet resultSet = statement.executeQuery(query);
                    resultSet.moveToCurrentRow();
                    String pass = resultSet.getString(1);
                    
                    if (!request.getParameter("userName").equals(user)) {
                          allOk=false;
                    }
                    if (!request.getParameter("password").equals(pass)) {
                        allOk=false;
                    }
                    if (request.getParameter("password1").equals("q")) {
                          allOk=false;
                    }
                    if (!request.getParameter("password1").equals(request.getParameter("password2"))) {
                          allOk=false;
                    }
                          
              }
          catch(SQLException sqle) {
                  System.err.println("Error connecting: " + sqle);
          }
          catch(Exception ex) {
                  System.err.println("Error with input: " + ex);
          }
          
             return allOk;
      
        }
0
Question by:benk-master-flash
    8 Comments
     
    LVL 86

    Expert Comment

    by:CEHJ
    Change

    >>
    try
                 {    
                      boolean allOk=true;
    >>

    to



    boolean allOk=true;
    try
                 {    
    0
     
    LVL 9

    Expert Comment

    by:Nick_72
    You might have an exeption thrown, so you'll need to add a finally block to make sure it's always returned:

             catch(SQLException sqle) {
                    System.err.println("Error connecting: " + sqle);
                    allOK = false;  <--- add this
             }
             catch(Exception ex) {
                    System.err.println("Error with input: " + ex);
                    allOK = false;  <--- add this
             }
             finally {
                    return allOK;  <----- this code is always run no matter what
             }
           }

    /Nick
    0
     
    LVL 9

    Expert Comment

    by:Nick_72
    and of course, use CEHJ's suggestion also :)
    0
     

    Author Comment

    by:benk-master-flash
    followed both of your suggestions and incorporated both your adjustments plus one of my own
    with the following code allOk always evaluates to true and thus sends me to the /index2.jsp page
    how can make sure to execute the block of code that validates the changes to the password

    if (validateChanges(connection,request)) {
                              commitUpdate(connection,request);
                              connection.close();
                              address = "/index2.jsp";
                        } else {
                              address = "/ChangePassword.jsp";                                
                        }
                        RequestDispatcher dispatcher = request.getRequestDispatcher(address);
                        dispatcher.forward(request,response);

    private boolean validateChanges(Connection cn, HttpServletRequest request)
            throws ServletException, IOException {
                boolean allOk=true;
                   try
                   {      
                        String user = (String)request.getRemoteUser();
                        String query = "SELECT user_pass FROM users WHERE user_name = \"" + request.getRemoteUser() + "\";";
                        Statement statement = cn.createStatement();
                    ResultSet resultSet = statement.executeQuery(query);
                        resultSet.moveToCurrentRow();
                        String pass = resultSet.getString(1);
                        /*
                        if (!request.getParameter("userName").equals(user)) {
                              allOk=false;
                        }
                        if (!request.getParameter("password").equals(pass)) {
                            allOk=false;
                        }
                        if (request.getParameter("password1").equals("q")) {
                              allOk=false;
                        }
                        if (!request.getParameter("password1").equals(request.getParameter("password2"))) {
                              allOk=false;
                        }
                        */
                        allOk=false;            
                  }
              catch(SQLException sqle) {
                      System.err.println("Error connecting: " + sqle);
              }
              catch(Exception ex) {
                      System.err.println("Error with input: " + ex);
              }
              finally {
                  return allOk;
                    }
           }
    0
     
    LVL 86

    Accepted Solution

    by:
    I would set allOk to false first then do:

    String query = "SELECT user_pass FROM users WHERE user_name = \"" + request.getRemoteUser() + "\";";
    Statement statement = cn.createStatement();
    ResultSet resultSet = statement.executeQuery(query);
    String pass = resultSet.getString(1);
    if (resultSet.next()) {
          allOk = request.getParameter("userName").equals(user) &&
                request.getParameter("password").equals(pass) &&
                request.getParameter("password1").equals("q") &&
                request.getParameter("password1").equals(request.getParameter("password2"));
    }      

    (that would be the whole of the try block)      
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    You don't need terminating semicolons on your single SQL statements btw. There's an error in my last post:

    >>String pass = resultSet.getString(1);
    >>if (resultSet.next()) {

    should be

    if (resultSet.next()) {
         String pass = resultSet.getString(1);


    0
     
    LVL 92

    Expert Comment

    by:objects
    looks like your sql query is failing.
    check the logs for any errors.
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    8-)
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Course: From Zero to Hero with Nodejs & MongoDB

    Interested in Node.js, but don't know where to start or how to learn it properly? Confused about how the MEAN stack pieces of MongoDB, Expressjs, Angularjs, and Nodejs fit together? Or how it's even possible to run JavaScript outside of the browser?

    Suggested Solutions

    Title # Comments Views Activity
    what is spring 3 29
    fizzArray2 challenge 1 30
    pre4 challenge 19 56
    What is the compatible hibernate version for spring 4.3.1 10 34
    An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
    Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
    Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

    913 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now