|
Add a Link to Create a New User
Most websites will allow you to create a new user in the system. Create a link on the authentication page that links to another Web site where the user can enter information and have their user account created.
- Create an HTML page with a text field to enter in the username and passwordof a user and add a link to another page to create a user.
- Open the HTML tag by typing <html>.
- Open the HEAD tag by typing <head>.
- Set a title by typing <title>Authentication by Email Address</title>.
- Close the HEAD tag by typing </head>
- Open the BODY tag by typing <body>.
- Output a header 1 by typing <h1>Enter your email address and password</h1>.
- Open the form tag and complete the method and post attributes of the form. This tells the HTML page where it is posting. The statement is:
<form method = "post" action = "http://server_name:8080/servlet/auth2">
- Open one input text field with the input tag and label their names email.
<input type = "text" size = "50" name = "email" />
- Add a link to create.html with the following line.
<p><a href = "create.html">Create New User</a></p>
- Open two buttons, a submit and reset button, then close the form, body and html tags.
<input type = "submit" value = "Authenticate" /> <input type = "reset" value = "Clear Fields" />
Here is my code for the HTML page.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>Authenticate to eDirectory</title> </head> <body bgcolor = "ffffde"> <h1>Please Enter Your Username and Password</h1> <form method = "post" action = "http://edu-qc.provo.novell.com:8080/servlet/auth1"> <p><label>Username:<input type = "text" size = "50" name = "username" /></label></p> <p><label>Password:<input type = "password" size = "50" name = "password" /></label></p> <p><a href = "create.html">Create New User</a></p> <p> <input type = "submit" value = "Authenticate" /> <input type = "reset" value = "Clear Fields" /> </p> </form> </body> </html>
- Now create an HTML page called create.html that the user will use to enter the properties of his user object. This HTML page will post to a servlet which will authenticate into eDirectory and create a user object with the specified username, password and other properties.
- Begin the HTML page with the <html> tag.
- Open the <head> tag.
- Open the <title> tag and type a title for the page such as "Create a User in eDirectory" and close the tag with </title>.
- Close the head tag with </head>.
- Ouput a header 1 tag with <h1>Enter your username and password</h1>.
- Open the form tag with the method and action properties.
- Open a table tag to format the text fields.
- Open a table row tag by typing <tr>.
- Open a table cell by typing <td>.
- Open a paragraph tag and type: User Name:
- Close the table cell tag.
- Open a new table cell and create a text field with the name username.
- Close the table cell and table row tags.
- Open a new table row and table cell.
- Open a new paragraph tag and type: First Name:
- Close the table cell and table row tags.
- Open a new table cell and row and create a new text field called firstname.
- Close the table cell and row tags.
- Create three more text fields for the last name, email address and password.
- Create submit and rest buttons.
- Close the form, body, and html tags.
Here is my code for create.html:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>Create a New User Account</title> </head> <body bgcolor = "ffffde"> <h1>Enter in the following information to create a user account</h1> <form method = "post" action = "http://edu-qc.provo.novell.com:8080/servlet/create"> <table cellpadding = "5" cellspacing = "5"> <tr> <td> <p>User Name:</p> </td> <td> <input type = "text" size = "50" name = "username" /> </td> </tr> <tr> <td> <p>First Name:</p> </td> <td> <input type = "text" size = "50" name = "firstname" /> </td> </tr> <tr> <td> <p>Last Name:</p> </td> <td> <input type = "text" size = "50" name = "lastname" /> </td> </tr> <tr> <td> <p>Email Address:</p> </td> <td> <input type = "text" size = "50" name = "email" /> </td> </tr> <tr> <td> <p>Password:</p> </td> <td> <input type = "password" size = "50" name = "password" /> </td> </tr> </table> <p> <input type = "submit" value = "Authenticate" /> <input type = "reset" value = "Clear Fields" /> </p>
</form> </body> </html>
- Create a servlet that will receive the data from create.html and authenticate to eDirectory as admin and create the user specified in create.html with the properties.
- Begin with the basic servlet stub from Exercise 1.
- First, instantiate an LdapConnection bean.
- Second, instantiate an AuthenticateLdap bean and set the properties so that you log in as admin.
- Now set the Ldap Connection object to the Ldap connection you created by logging in as admin with the AuthenticateLdap bean.
- Now, instantiate a CreateLdapEntry bean passing as a parameter the LdapConnection bean you previously created.
- Create a string and concatenate the proper context with the username the user types in at create.html in the user name text field.
String s = "cn=" + request.getParameter("username") + ", o=novell";
- Now, create a string and concatenate the first name and last name together.
String fullname = request.getParameter("firstname") + " " + request.getParameter("lastname");
- Set each of the properties for the bean with the following statements.
cle.setName(s); cle.addAttribute("objectClass", new String[] {"inetOrgPerson", "ndsLoginProperties"}); cle.addAttribute("uid", request.getParameter("username")); cle.addAttribute("sn", request.getParameter("lastname")); cle.addAttribute("givenName", request.getParameter("firstname")); cle.addAttribute("fullName", fullname); cle.addAttribute("userPassword", request.getParameter("password")); cle.addAttribute("mail", request.getParameter("email"));
- If the user is created successfully, output HTML that says that the user was created successfully.
out.println("<h1 align=center>"); out.println("User " + request.getParameter("username") + " created successfully"); out.println("</h1>");
- Catch an LdapNameAlreadyBoundException in case the user name specified by the user already exists in eDirectory.
Here is my code for the Create servlet:
import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; import com.novell.ecb.*; import com.novell.ecb.ldap.*; public class create extends HttpServlet { LdapConnection connection = new LdapConnection(); private static final String CONTENT_TYPE = "text/html"; public void init() throws ServletException { } //Process the HTTP Post request public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(CONTENT_TYPE); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<head><title>create</title></head>"); out.println("<body bgcolor=\"#ffc800\">"); //I'll first authenticate as admin AuthenticateLdap bean = new AuthenticateLdap(); bean.setURL("ldap://edu-qc.provo.novell.com"); bean.setDN("cn=admin, o=novell"); bean.setPassword("password"); try { bean.execute(); } catch(Exception e) { out.println("<h1>Connection to the Directory failed</h1>"); e.printStackTrace(out); } connection = bean.getLdapConnection();
//The LdapConnection is the parameter to instantiate the CreateLdapEntry bean CreateLdapEntry cle = new CreateLdapEntry(connection);
//I create a string for the full context of the user object to be created String s = "cn=" + request.getParameter("username") + ", o=novell";
//I create a string to concatenate the first name and last name to create the full name attribute String fullname = request.getParameter("firstname") + " " + request.getParameter("lastname");
//I set the attributes for the user object cle.setName(s); cle.addAttribute("objectClass", new String[] {"inetOrgPerson", "ndsLoginProperties"}); cle.addAttribute("uid", request.getParameter("username")); cle.addAttribute("sn", request.getParameter("lastname")); cle.addAttribute("givenName", request.getParameter("firstname")); cle.addAttribute("fullName", fullname); cle.addAttribute("userPassword", request.getParameter("password")); cle.addAttribute("mail", request.getParameter("email")); try { cle.execute(); out.println("<h1 align=center>"); out.println("User " + request.getParameter("username") + " created successfully"); out.println("</h1>"); } catch(LdapNameAlreadyBoundException e) { //Catch this exception if the user already exists in eDirectory out.println("<h1>The user name you have chosen already exists</h1>"); e.printStackTrace(out); } catch(Exception e) { e.printStackTrace(out); } out.println("</body></html>"); } //Clean up resources public void destroy() { } }
|