Testing LDAP Creation

I l @ ve RuBoard

Now you can try a JSP to test LDAP record creation (see Listing 15.5). Model this page after the form-submission pages that you've written to this point. A couple of refinements have been added to make the code a bit more straightforward, mainly in the form of notNull and bn (blanks from nulls) helper functions defined at the top. When the page is first requested (or if all the values aren't filled out), it displays a form with all the employee fields (see Figure 15.6). When all the fields are filled out correctly, it uses LDAP to create the record and to display the uid that was assigned (see Figure 15.7).

Figure 15.6. testLdapCreate.jsp requesting values.

graphics/15fig06.jpg

Figure 15.7. testLdapCreate.jsp creating a user .

graphics/15fig07.jpg

You have to explicitly set orgUnit to "" if it's null because you're going to use it later in an equals test and you don't want to get a null pointer exception. A future enhancement would be to get a list of valid ou values from LDAP and use those values to create the pull-down rather than hardwiring in this information.

Listing 15.5 testLdapCreate.jsp
 <%@ page import="com.bfg.employee.Employee" %> <jsp:useBean id="emp" class="com.bfg.employee.Employee" scope="request"/> <jsp:setProperty name="emp" property="*"/> <HEAD><TITLE>Create new employee</TITLE></HEAD><BODY> <%! private boolean notNull(String str) {     return ((str != null) && (str.length() > 0)); } private String bn (String val) {     if (val == null) return "";     return val; } %> <% if (emp.getOrgUnit() == null) {     emp.setOrgUnit(""); } if (request.getParameter("SUBMITTED") != null) {     if (notNull(emp.getLastName()) &&     notNull(emp.getFirstName()) &&     notNull(emp.getTitle()) &&     notNull(emp.getEmployeeNumber()) &&     notNull(emp.getOrgUnit()) &&     notNull(emp.getTelephoneNumber()) &&     notNull(emp.getPassword())) {     emp.setOrganization("bfg");     emp.createEmployee();     out.write("Created employee " + emp.getUserID() + "!</BODY>");     return;     } } %> <H1><CENTER>Create New Employee</CENTER></H1> <FORM ACTION="testLdapCreate.jsp" METHOD="POST"> <INPUT TYPE="HIDDEN" NAME="SUBMITTED" VALUE="YES"> <TABLE WIDTH="75%" BORDER=1>   <TR><TD>Last Name: </TD>   <TD><INPUT TYPE="TEXT" NAME="lastName"               VALUE="<%= bn(emp.getLastName()) %>"></TD></TR>   <TR><TD>First Name: </TD>   <TD><INPUT TYPE="TEXT" NAME="firstName"               VALUE="<%= bn(emp.getFirstName()) %>"></TD></TR>   <TR><TD>Title: </TD>   <TD><INPUT TYPE="TEXT" NAME="title"               VALUE="<%= bn(emp.getTitle()) %>"></TD></TR>   <TR><TD>Emp Num: </TD>   <TD><INPUT TYPE="TEXT" NAME="employeeNumber"               VALUE="<%= bn(emp.getEmployeeNumber()) %>"></TD></TR>   <TR><TD>Division: </TD>   <TD><SELECT NAME="orgUnit">     <OPTION VALUE="">Please Select     <OPTION VALUE="admin"      <%= emp.getOrgUnit().equals("admin")?"SELECTED":"" %>>      Administrative     <OPTION VALUE="sales"      <%= emp.getOrgUnit().equals("sales")?"SELECTED":"" %>>      Sales     <OPTION VALUE="editorial"      <%= emp.getOrgUnit().equals("editorial")?"SELECTED":"" %>>      Editorial     </SELECT>   </TD>   <TR><TD>Telephone Number: </TD>   <TD><INPUT TYPE="TEXT" NAME="telephoneNumber"               VALUE="<%= bn(emp.getTelephoneNumber()) %>"></TD></TR>   <TR><TD>Password</TD>   <TD><INPUT TYPE="TEXT" NAME="password"               VALUE="<%= bn(emp.getPassword()) %>"></TD></TR></TABLE>   <CENTER><INPUT TYPE=SUBMIT VALUE="Create User"></CENTER> </BODY> 

After you run it, you can bring up the LDAP browser and confirm for yourself that the entry has been dropped into the LDAP database (see Figure 15.8).

Figure 15.8. The new employee record appears.

graphics/15fig08.jpg

If you run this repeatedly on the same last name and first initial, you can see that the uid remains unique via the addition of a number to the end of the uid (see Figure 15.9). Now you know where JSMITH34234 comes from on all those AOL accounts.

Figure 15.9. Adding more of the same name.

graphics/15fig09.jpg

I l @ ve RuBoard


MySQL and JSP Web Applications. Data-Driven Programming Using Tomcat and MySQL
MySQL and JSP Web Applications: Data-Driven Programming Using Tomcat and MySQL
ISBN: 0672323095
EAN: 2147483647
Year: 2002
Pages: 203
Authors: James Turner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net