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.

  1. 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.

  2. Open the HTML tag by typing <html>.

  3. Open the HEAD tag by typing <head>.

  4. Set a title by typing <title>Authentication by Email Address</title>.

  5. Close the HEAD tag by typing </head>

  6. Open the BODY tag by typing <body>.

  7. Output a header 1 by typing <h1>Enter your email address and password</h1>.

  8. 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">

  1. Open one input text field with the input tag and label their names email.


<input type = "text" size = "50" name = "email" />

  1. Add a link to create.html with the following line.


<p><a href = "create.html">Create New User</a></p>

  1. 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>

  1. 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.

  2. Begin the HTML page with the <html> tag.

  3. Open the <head> tag.

  4. Open the <title> tag and type a title for the page such as "Create a User in eDirectory" and close the tag with </title>.

  5. Close the head tag with </head>.

  6. Ouput a header 1 tag with <h1>Enter your username and password</h1>.

  7. Open the form tag with the method and action properties.

  8. Open a table tag to format the text fields.

  9. Open a table row tag by typing <tr>.

  10. Open a table cell by typing <td>.

  11. Open a paragraph tag and type: User Name:

  12. Close the table cell tag.

  13. Open a new table cell and create a text field with the name username.

  14. Close the table cell and table row tags.

  15. Open a new table row and table cell.

  16. Open a new paragraph tag and type: First Name:

  17. Close the table cell and table row tags.

  18. Open a new table cell and row and create a new text field called firstname.

  19. Close the table cell and row tags.

  20. Create three more text fields for the last name, email address and password.

  21. Create submit and rest buttons.

  22. 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>

  1. 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.

  2. Begin with the basic servlet stub from Exercise 1.

  3. First, instantiate an LdapConnection bean.

  4. Second, instantiate an AuthenticateLdap bean and set the properties so that you log in as admin.

  5. Now set the Ldap Connection object to the Ldap connection you created by logging in as admin with the AuthenticateLdap bean.

  6. Now, instantiate a CreateLdapEntry bean passing as a parameter the LdapConnection bean you previously created.

  7. 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";

  1. Now, create a string and concatenate the first name and last name together.


String fullname = request.getParameter("firstname") + " " + request.getParameter("lastname");

  1. 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"));

  1. 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>");

  1. 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()
{
}
}

download sample files
Previous blank Table of Contents blank Next