11 Replies Latest reply: Dec 27, 2007 11:39 AM by 807603 RSS

    Returning true from boolean function

    807603
      I want to return true and false from the function below, does this function will automatically returns true or I have to write return statement

      public boolean connection()
      {
           Hashtable env = new Hashtable(11);

      env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
      env.put(Context.PROVIDER_URL, "ldap://ad.infosys.com:389");
      env.put(Context.SECURITY_AUTHENTICATION, "simple");
      env.put(Context.SECURITY_PRINCIPAL, userid+"@ad.infosys.com");
      env.put(Context.SECURITY_CREDENTIALS,pwd);

      DirContext ctx = new InitialDirContext(env);

      ctx.close();
      }
        • 1. Re: Returning true from boolean function
          user8478974
          What makes you believe that a method will return anything without a return statement?
          Have you tried it? Doesn't your compiler complain?
          Even if your last statement (ctx.close()) returns a boolean, you need to pass it on with a return statement.
          • 2. Re: Returning true from boolean function
            807603
            hi i m totally new to java and confused this piece of code about returning true, I am trying to execute it but its showing illegal start of expression
            • 3. Re: Returning true from boolean function
              807603
              yes you have to write a return statement to return anything

              please post the error
              • 4. Re: Returning true from boolean function
                807603
                i have added the return statement , but actually i m writing this program in JSP page and getting the following error

                org.apache.jasper.JasperException: Unable to compile class for JSP

                An error occurred at line: 5 in the jsp file: /Task/session.jsp
                Generated servlet error:
                C:\Program Files\Apache Software Foundation\Tomcat 5.0\work\Catalina\localhost\_\org\apache\jsp\Task\session_jsp.java:55: illegal start of expression
                public boolean connection()
                ^
                • 5. Re: Returning true from boolean function
                  807603
                  Maybe you want to add in some more codes or the entire class file since anything before this function or after this function may affect the current connection() function. In simple terms , what are the codes before coming to the connection() function and what are the codes after. It might lead to some more clues. What you posted is too vague.The problem may not lie in the boolean anymore but this time it maybe about your code structure like missing brackets , braces , unclosed if - else ...etc.
                  • 6. Re: Returning true from boolean function
                    807603
                    I have reduced my code to this to chk the function but still getting the same error the code is pasted below
                    <%@page import="javax.naming.*"%>
                    <%@page import="javax.naming.directory.*"%>
                    <%@page import="java.util.Hashtable"%>
                    <%@page import="java.util.*"%>
                    <%
                    String userid = request.getParameter("loginid");
                    String pwd=request.getParameter("password");


                    public boolean connection()
                    {
                         Hashtable env = new Hashtable(11);

                    env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
                    env.put(Context.PROVIDER_URL, "ldap://ad.infosys.com:389");
                    env.put(Context.SECURITY_AUTHENTICATION, "simple");
                    env.put(Context.SECURITY_PRINCIPAL, userid+"@ad.infosys.com");
                    env.put(Context.SECURITY_CREDENTIALS,pwd);

                    DirContext ctx = new InitialDirContext(env);

                    return ctx.close();
                    }

                    %>
                    *** it's a jsp file*
                    • 7. Re: Returning true from boolean function
                      807603
                      Inside a scriplet tag you cannot declare a method. Use declaration tag for declaring any methods and variables what will be visible at class level.

                      Remove this from your code
                      public boolean connection()
                      {
                      Hashtable env = new Hashtable(11);
                      
                      env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
                      env.put(Context.PROVIDER_URL, "ldap://ad.infosys.com:389");
                      env.put(Context.SECURITY_AUTHENTICATION, "simple");
                      env.put(Context.SECURITY_PRINCIPAL, userid+"@ad.infosys.com");
                      env.put(Context.SECURITY_CREDENTIALS,pwd);
                      
                      DirContext ctx = new InitialDirContext(env);
                      
                      return ctx.close();
                      }
                      and write it inside <%! %>. this is a declaration tag.
                      <%! public boolean connection()
                      {
                      Hashtable env = new Hashtable(11);
                      
                      env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
                      env.put(Context.PROVIDER_URL, "ldap://ad.infosys.com:389");
                      env.put(Context.SECURITY_AUTHENTICATION, "simple");
                      env.put(Context.SECURITY_PRINCIPAL, userid+"@ad.infosys.com");
                      env.put(Context.SECURITY_CREDENTIALS,pwd);
                      
                      DirContext ctx = new InitialDirContext(env);
                      
                      return ctx.close();
                      }%>
                      • 8. Re: Returning true from boolean function
                        807603
                        I have modifiefd the code as below
                        <%! String userid,pwd;%>
                        <%
                        userid = request.getParameter("loginid");
                        pwd=request.getParameter("password");

                        try {%>
                        <%!
                             public boolean connection()

                        {
                             Hashtable env = new Hashtable(11);

                        env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
                        env.put(Context.PROVIDER_URL, "ldap://ad.infosys.com:389");
                        env.put(Context.SECURITY_AUTHENTICATION, "simple");
                        env.put(Context.SECURITY_PRINCIPAL, userid+"@ad.infosys.com");
                        env.put(Context.SECURITY_CREDENTIALS,pwd);

                        DirContext ctx = new InitialDirContext(env);

                        ctx.close();
                             return true;
                        }
                        }
                        %>

                        <%
                        catch {
                             out.println("Unable to retrieve attribute: " + e);
                        }
                             %>

                        Now I am getting the following error
                        *class, interface, or enum expected  private static java.util.Vector jspxdependants;*
                        • 9. Re: Returning true from boolean function
                          807603
                          Your entire JSP code is wrong. Try to understand the use of try-catch block, and also how to use jsp.
                          You should not define method inside the try-catch block.

                          Your code should goes like below:

                          <%! String userid, pwd;%>
                          <%
                          userid = request.getParameter("loginid");
                          pwd = request.getParameter("password");
                          %>

                          <%!
                          public boolean connection() throws IOException

                          {
                          Hashtable env = new Hashtable(11);

                          env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
                          env.put(Context.PROVIDER_URL, "ldap://ad.infosys.com:389");
                          env.put(Context.SECURITY_AUTHENTICATION, "simple");
                          env.put(Context.SECURITY_PRINCIPAL, userid + "@ad.infosys.com");
                          env.put(Context.SECURITY_CREDENTIALS, pwd);
                          try {
                          DirContext ctx = new InitialDirContext(env);

                          ctx.close();
                          } catch (Exception e) {
                          out.println("Unable to retrieve attribute: " + e);
                          }
                          return true;
                          }


                          %>
                          • 10. Re: Returning true from boolean function
                            807603
                            hi,
                            thnx to all who replied , i am able to run the program now
                            • 11. Re: Returning true from boolean function
                              807603
                              Consider the advice you were given in your crosspost on www.thescripts.com:

                              http://www.thescripts.com/forum/thread752706.html

                              Why not keep Java out of your JSPs?