[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Servlet Error

Posted on 2004-10-28
8
Medium Priority
?
176 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
Comment
Question by:benk-master-flash
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 12436194
Change

>>
try
             {    
                  boolean allOk=true;
>>

to



boolean allOk=true;
try
             {    
0
 
LVL 9

Expert Comment

by:Nick_72
ID: 12436208
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
ID: 12436219
and of course, use CEHJ's suggestion also :)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:benk-master-flash
ID: 12436544
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:
CEHJ earned 450 total points
ID: 12438408
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
ID: 12438446
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
ID: 12439661
looks like your sql query is failing.
check the logs for any errors.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12447955
8-)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

656 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