Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 603
  • Last Modified:

<jsp:setProperty Problem

I'm really stumped on using setProperty for jsp......

This is my initial login page:

<HTML>
<BODY>
<h2>Login</h2>
<FORM METHOD=POST ACTION="Authentication2.jsp">
ID: <INPUT TYPE=TEXT NAME=uid SIZE=20><BR>
Password: <INPUT TYPE=PASSWORD NAME=pw SIZE=20><BR>
<P><INPUT TYPE=SUBMIT>
</FORM>
</BODY>
</HTML>
----------------_----------------_----------------_----------------

<jsp:useBean id="auth" class="com.wrox.auth.Authenticate" scope="session"/>
<jsp:setProperty name="auth" property="*"/>
<HTML>
<BODY>
<%
if(auth.checkEntry())
{
     %>
     <A HREF="Logged.jsp">Continue</A>
     <%
}
else
{
     %>
     <A HREF="LoginPage.jsp">Invalid! Continue</A>
     <%
}
%>
</BODY>
</HTML>

----------------_----------------_----------------_--------------------------------_----------------_----------------_----------------


I checked my logs and basically it says that my username and password variables were never initialized....
here's my java code for Authenticate


----------------_----------------_----------------_--------------------------------_----------------_----------------_----------------


package com.wrox.auth;
import javax.naming.*;
import javax.naming.directory.*;
import java.util.Hashtable;


public class Authenticate
{
     String uid;
     String pw;
     String url = "ldap://127.0.0.1:389/";
     String BDN = "o=rcms2";

     public void setUid(String value)
     {
          uid = "uid="+value+", "+BDN;
     }

     public void setPw(String value)
     {
          pw = value;    
     }

     public String getUid()
     {
          return uid;
     }


     public boolean checkEntry()
     {
          Hashtable env = new Hashtable(11);
          env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
          env.put(Context.PROVIDER_URL, url);

          //Authenticate
          env.put(Context.SECURITY_AUTHENTICATION, "simple");
          System.out.println("TEST: "+uid);
          env.put(Context.SECURITY_PRINCIPAL, uid);
          env.put(Context.SECURITY_CREDENTIALS, pw);
          try
          {
                   // Create initial context
                   DirContext ctx = new InitialDirContext(env);
                   ctx.close();
          }
          catch (AuthenticationException e1)
          {
               //System.out.println("Invalid Password.");
               return false;
          }
          catch (NamingException e)
          {
                   e.printStackTrace();
          }
          return true;
     }
}



Any pointers? Thanks Sorry my points offering is so low, that's all i have =(.  I jsut know that my problem is that my two variables in my java class (uid and pw).  If i am doing it incorrectly, can someone please show me how to do it properlY? Thanks.
0
lapchern
Asked:
lapchern
  • 5
  • 3
1 Solution
 
jarasaCommented:
Hi Lapchern.

I'm not really good at Beans but shouldn't you need a constructor on your Bean which would set the value of uid & pw. Well if that was a normal class you should initialize those variable on a constructor like this

public void Authenticate (String myUid, String myPw)
     {
          uid = myUid;
          pw = myPw;
     }

and you should call that constructor with the form, but as I told you I'm not common with Beans so I can assure you that's your problem.

Maybe it will work if you initialize uid & pw at the declaration, like this:

String uid = "";
String pw = "";



Hope it helps anyway.

Javier
0
 
jarasaCommented:
>>and you should call that constructor with the form, but as I told you I'm not common with Beans so I can assure you that's your problem.

Can't assure you that's your problem, sorry.

Javier
0
 
jarasaCommented:
Hi again.
I've been reading, ha, yes reading, incredible, I think you must have a constructor but without parameters on your bean and initilize there all the fields of your bean, something like this.

public void Authenticate ()
     {
          uid = null;
          pw = null;
     }

I think That may be the problem.

regards
Javier
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.

 
lapchernAuthor Commented:
Hi jarasa, I really appreciate your effort and thanks.

I'm very new at this, but according to this tutorial I've been following at http://www.jsptut.com/Forms.jsp

you do not need a constructor. I verified this by testing the provided sample code myself and it ran perfectly fine. i'm really dumbfounded by my problem... i'm trying to pretty much follow the example of this site...but my variables aren't initialized properly via "set" methods as the site says.
0
 
jarasaCommented:
Hi again.

I have tryed the code you have here without the LDAP part I just put this:

package com.wrox.auth;
import javax.naming.*;
import javax.naming.directory.*;
import java.util.Hashtable;


public class Authenticate
{
     String uid;
     String pw;
     String url = "ldap://127.0.0.1:389/";
     String BDN = "o=rcms2";

     public void setUid(String value)
     {
          uid = "uid="+value+", "+BDN;
     }

     public void setPw(String value)
     {
          pw = value;    
     }

     public String getUid()
     {
          return uid;
     }


     public boolean checkEntry()
     {
           
      System.out.println(uid);
      System.out.println(pw);
     
      if(user.equals("uid=Javier, o=rcms2"))
                  return true;
      else
                  return false;
                  
/*                  
          Hashtable env = new Hashtable(11);
          env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
          env.put(Context.PROVIDER_URL, url);

          //Authenticate
          env.put(Context.SECURITY_AUTHENTICATION, "simple");
          System.out.println("TEST: "+uid);
          env.put(Context.SECURITY_PRINCIPAL, uid);
          env.put(Context.SECURITY_CREDENTIALS, pw);
          try
          {
                   // Create initial context
                   DirContext ctx = new InitialDirContext(env);
                   ctx.close();
          }
          catch (AuthenticationException e1)
          {
               //System.out.println("Invalid Password.");
               return false;
          }
          catch (NamingException e)
          {
                   e.printStackTrace();
          }
          return true;
          */
     }
}


And works fine with Tomcat 4. maybe you should see if you are doing well the LDAP check.

Javier
0
 
webhsuCommented:
I dont' know, but don't you need a getPw() method also?

To agree with someones' comments above, I've written a lot of these forms and a constructor is not necessary.
0
 
lapchernAuthor Commented:
Damn thank yoU!!!!!!!
0
 
jarasaCommented:
Just Curious.
did you solve your problem, putting the getPw() ??
Javier.
0
 
lapchernAuthor Commented:
yeah
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now