FileDocCategorySizeDatePackage
MagicServlet.javaAPI DocExample2517Sun Jul 06 01:05:38 BST 2003antipatterns

MagicServlet

public class MagicServlet extends HttpServlet

Fields Summary
private DirContext
peopleContext
Constructors Summary
Methods Summary
public voiddestroy()

        try {
            peopleContext.close();
        } catch(NamingException ne) {
            ne.printStackTrace();
        }
    
protected voiddoGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)

        response.setContentType("text/html");
        java.io.PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<table>");
    
        try {
            NamingEnumeration people = peopleContext.list("");
            while(people.hasMore()) {
                NameClassPair personName = (NameClassPair)people.next();
                Attributes personAttrs = 
                    peopleContext.getAttributes(personName.getName());
                Attribute cn = personAttrs.get("cn");
                Attribute sn = personAttrs.get("sn");
                Attribute phone = personAttrs.get("telephoneNumber");
                
                out.println("<tr><td>" + cn.get() + " " + sn.get() + "</td>"  +
                            "<td>" + phone.get() + "</td></tr>");
            }
        } catch(Exception ex) {
            out.println("Error " + ex + " getting data!");
        }
   
        out.println("</table>");
        out.println("</body>");
        out.println("</html>");
    
public voidinit(javax.servlet.ServletConfig config)

        super.init(config);
        
        Properties env = new Properties();
        env.put(Context.INITIAL_CONTEXT_FACTORY,
            "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, "ldap://localhost/o=jndiTest");
        env.put(Context.SECURITY_PRINCIPAL, "cn=Manager, o=jndiTest");
        env.put(Context.SECURITY_CREDENTIALS, "secret");
        
        try {
            DirContext initalContext = new InitialDirContext(env);
            peopleContext = (DirContext)initalContext.lookup("ou=people");
        } catch (NamingException ne) {
            ne.printStackTrace();
            throw new ServletException("Error inializing LDAP", ne);
        }