Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java Open Source > Apache Tomcat
|
Apache Tomcat General discussion of the Apache Tomcat servlet container. For discussions specific to the Professional Apache Tomcat book, please see the book discussion forum for that book.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Apache Tomcat section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 11th, 2005, 03:57 PM
Registered User
 
Join Date: Oct 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default How the servlet be able to add the entry into LDAP

hello
       I am making an application in which i want to add the name of the user into the directory server (LDAP) through the servlet
The code to add the entries to LDAP server is running quite fine but when i integrate the code into the servlet there is no result.
The log file is also not showing the error
As well as when i create any object as
Adder a = new Adder();
into the servlet for adding
The various classes that are lying in the Adder.java class (which adds the entries to the LDAP ) are bypassed

What maybe the problem

The code for the adding of the entries is
Code:
/*
 * Copyright (c) 1997.  Netscape Communications Corporation.  All
 * rights reserved.
 *
 * Add a new entry to the directory.
 *
 * Since it is an error to attempt to add an entry which already exists,
 * you cannot run this example program twice in a row.  You can use the
 * del.java example program to delete the entry which this example adds.
 *
 */
package com.chat;
import netscape.ldap.*;
import java.util.*;
import java.io.*;
import com.chat.LoginServlet;

public class Add {
    public Add()
    {
        /* Specify the DN we're adding */

        String dn = "uid, ou=People, o=Airius.com";

        /* Specify the attributes of the entry */

        // THIS STATEMENT DOES NOT EXECUTE


        System.out.println("1");
        String objectclass_values[] = { "top",
                                        "person",
                                        "organizationalPerson",
                                        "inetOrgPerson" };
        String cn_values[] = { "William B Jensen",
                               "William Jensen",
                               "Bill Jensen" };*/
        String sn_values[] = { "Jensen" };
        String givenname_values[] = { "William", "Bill" };
        String telephonenumber_values[] = { "+1 415 555 1212" };

        LDAPAttributeSet attrs = new LDAPAttributeSet();
        LDAPAttribute attr = new LDAPAttribute( "objectclass" );

        for( int i = 0; i < objectclass_values.length; i++ )
        {
            attr.addValue( objectclass_values[i] );
        }
        attrs.add( attr );


        attrs.add( new LDAPAttribute( "uid") );

        /* Create an entry with this DN and these attributes */

        LDAPEntry myEntry = new LDAPEntry( dn, attrs );

        LDAPConnection ld = null;
        int status = -1;
        try {
            ld = new LDAPConnection();
            /* Connect to server */
            String MY_HOST = "localhost";
            int MY_PORT = 389;
            ld.connect( MY_HOST, MY_PORT );

            /* Authenticate to the server as directory manager */
            String MGR_DN = "dc=monarch,dc=com";
            String MGR_PW = "ali";
            ld.authenticate( MGR_DN, MGR_PW );
            System.out.println("Entry established");

            /* Now add the entry to the directory */
            ld.add( myEntry );
            System.out.println( "Entry added"  );
            System.out.println("Entry established1111");
            }
        catch( LDAPException e ) {
            if ( e.getLDAPResultCode() == LDAPException.ENTRY_ALREADY_EXISTS )
                System.out.println( "Error: Entry already present" );
            else
                System.out.println( "Error: " + e.toString()+"AA che error" );
                                    }


        }

    }
The code for the servlet is
Code:
package it.com.chat.java;

import java.io.*;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.swing.*;
import it.com.chat.Chatter;
import it.com.chat.ChatRoomList;
import it.com.chat.ChatRoom;


public class LoginServlet extends HttpServlet
{
    boolean flag = false;
    String s1,s2;
    int i = 1800;

    public void doGet(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException
     {
         doPost(req,res);
     }
    public void doPost(HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse)
        throws ServletException, IOException
    {

        String s = "jdbc:odbc:chat";
        contextPath = httpservletrequest.getContextPath();
        s1 = httpservletrequest.getParameter("nickname");
        s2 = httpservletrequest.getParameter("pwd");
        System.out.println(s1);
        System.out.println(s2);

        s1 = s1.trim().toLowerCase();
        if(s2.length() > 0)
            s2 = s2.trim().toLowerCase();
        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection connection = DriverManager.getConnection(s);
            Statement statement = connection.createStatement();
            for(ResultSet resultset = statement.executeQuery("select * from Login"); resultset.next();)
            {

                String s3 = resultset.getString(1);
                String s4 = resultset.getString(2);
                if(s1.equals(s3) && s2.equals(s4))
                {
                    flag = true;
                 }

            }
          }//try ends here

         catch(SQLException sqlexception)
         {
             System.out.println("Error...." + sqlexception);
         }
         catch(ClassNotFoundException classnotfoundexception)
         {
             System.out.println("Error...." + classnotfoundexception);
         }


            out = new ObjectOutputStream(httpservletresponse.getOutputStream());
            out.writeBoolean(flag);
            out.flush();
            out.close();


             if(flag=true)
              {
                try
                {

                       System.out.println("Control is here now...");
                       ChatRoomList chatroomlist = (ChatRoomList)getServletContext().getAttribute("chatroomlist");
                     boolean flag1 = chatroomlist.chatterExists(s1);

                     //HERES WHERE THE CODE IS PLACED TO ADD THE ENTRIES TO THE LDAP SERVER
                     //THE OBJECT OF ADD.JAVA CLASS WHICH IS CALLED
                        if(flag1)
                           {
                               JOptionPane.showMessageDialog(null,"U have already Login......","Information",JOptionPane.INFORMATION_MESSAGE);
                           }
                           else
                           {
                               httpsession = httpservletrequest.getSession(true);

                              String s2 = getServletContext().getInitParameter("sessionTimeout");

                               if(s2 != null)
                            try
                            {
                                     i = Integer.parseInt(s2);
                                       i *= 60;
                            }
                            catch(NumberFormatException numberformatexception) { }

                        }
                       httpsession.setMaxInactiveInterval(i);
                    httpsession.setAttribute("nickname", s1);
                    ChatRoom chatroom = chatroomlist.getRoom("StartUp");
                    s1 = s1.toLowerCase();
                    Chatter chatter = null;
                      chatter = new Chatter(s1);
                    chatroom.addChatter(chatter);

                    ChatRoom chatRoom = null;
                    Chatter[] chatters = chatRoom.getChattersArray();
                    out = new ObjectOutputStream(httpservletresponse.getOutputStream());
                    out.writeObject(chatters);
                    out.flush();
                    out.close();

               }//try ends
                    catch(Exception exception)
                         {
                                  System.out.println("Exception thrown in LoginServlet: " + exception.getMessage());
                                   exception.printStackTrace();
                           }
                }//if ends here
                else
                {
                    JOptionPane.showMessageDialog(null,"Server is saying that Invalid User","Information",JOptionPane.INFORMATION_MESSAGE);
                }

    }//do post ends here

    private final void _mththis()
    {
        contextPath = "";
    }

    public LoginServlet()
    {
        _mththis();
    }

    private String contextPath;
    ObjectOutputStream out=null;
    HttpSession httpsession;

}
I am using Netscape sdk for java and openldap server and web server is Apache Tomcat

Plz tell
Regards
Jignesh







Similar Threads
Thread Thread Starter Forum Replies Last Post
LDAP Authentication -- Need Help avinashraj General .NET 1 May 25th, 2006 12:13 AM
Edit-add new entry to an acces table from word matpen Access 4 January 5th, 2006 06:30 PM
Access LDAP minhpx J2EE 0 June 14th, 2005 10:22 PM
LDAP bmains ASP.NET 1.0 and 1.1 Professional 0 May 26th, 2004 07:47 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.