hi i am new to j2me. can any body say how an http connection could be done in j2me

Http connection is very little different from classic java http connection. If we omit IOExceptions, code would look like this

String url = "http://www.daniweb.com";
InputConnection ic = (InputConnection)Connector.open(url);
InputStream in = ic.openInputStream();
// read from InputStream
ic.close();

For some examples check this place, also this book is very good resource

Thanks peter.I have a small coding to open an url from the examples u given .but i could not get the opened web page . and i have enclosed the code with this web page.

public class HttpMIDlet extends MIDlet
                    implements CommandListener, Runnable {

    private Display display;
    private Form addressForm;
    private Form connectForm;
    private Form displayForm;
    private TextField serverURL;
    private StringItem messageLabel;
    private StringItem errorLabel;
    private Command okCommand;
    private Command exitCommand;
    private Command backCommand;

    protected void startApp() throws MIDletStateChangeException {
        if (display == null) {
            initialize();
            display.setCurrent(addressForm);
        }
    }

    protected void pauseApp() {
    }

    protected void destroyApp(boolean unconditional)
                        throws MIDletStateChangeException {
    }

    public void commandAction(Command cmd, Displayable d) {
        if (cmd == okCommand) {
            Thread t = new Thread(this);
            t.start();
            display.setCurrent(connectForm);
        } else if (cmd == backCommand) {
            display.setCurrent(addressForm);
        } else if (cmd == exitCommand) {
            try {
                destroyApp(true);
            } catch (MIDletStateChangeException ex) {
            }
            notifyDestroyed();
        }
    }

    public void run() {
        InputStream is = null;
        HttpConnection conn = null;

        try {
            String url = serverURL.getString();
            if (!url.startsWith("http://") && 
      !url.startsWith("https://")) {
                url = "http://" + url;
            }
            conn = (HttpConnection)Connector.open(url, Connector.READ_WRITE);
        } catch (Exception ex) {
    System.out.println(ex);
    ex.printStackTrace();
            Alert alert = new Alert("Invalid Address",
                        "The supplied address is invalid\n" +
                        "Please correct it and try again.", null,
                        AlertType.ERROR);
            alert.setTimeout(Alert.FOREVER);
            display.setCurrent(alert, addressForm);
            return;
        }

        try {
            // Fetch the required page, reading up
            // to a maximum of 128 bytes
            if (conn.getResponseCode() == HttpConnection.HTTP_OK) {
                is = conn.openInputStream();
                final int MAX_LENGTH = 128;
                byte[] buf = new byte[MAX_LENGTH];
                int total = 0;
                while (total < MAX_LENGTH) {
                    int count = is.read(buf, total, MAX_LENGTH - total);
                    if (count < 0) {
                        break;
                    }
                    total += count;
                }
                is.close();
                String reply = new String(buf, 0, total);
                messageLabel.setText(reply);
            } else {
                messageLabel.setText("Failed: error " + conn.getResponseCode() +
                                "\n" + conn.getResponseMessage());
            }
            
            // Get the response code and print all the headers
            System.out.println("Response code = " + conn.getResponseCode());
            System.out.println("Response message = " + conn.getResponseMessage());
            for (int i = 0; ; i++) {
                String key = conn.getHeaderFieldKey(i);
                String value = conn.getHeaderField(i);
                if (key == null) {
                    // Reached last header
                    break;
                }
                System.out.println(key + " = " + value);
            }
            conn.close();
            display.setCurrent(displayForm);
        } catch (IOException ex) {
    System.out.println(ex);
    ex.printStackTrace();
            Alert alert = new Alert("I/O Error",
                        "An error occurred while communicating with the server.",
                        null, AlertType.ERROR);
            alert.setTimeout(Alert.FOREVER);
            display.setCurrent(alert, addressForm);
            return;
        } finally {
            // Close open streams
           try {
                if (is != null) {
                    is.close();
                    is = null;
                }
            } catch (IOException ex1) {
            }
            try {
                if (conn != null) {
                    conn.close();
                    conn = null;
                }
            } catch (IOException ex1) {
            }
        }
    }

    private void initialize() {
        display = Display.getDisplay(this);

        // Commands
        exitCommand = new Command("Exit", Command.EXIT, 0);
        okCommand = new Command("OK", Command.OK, 0);
        backCommand = new Command("Back", Command.BACK, 0);

        // The address form
        addressForm = new Form("HTTP Client");
        serverURL = new TextField("URL:", "", 256, TextField.ANY);
        addressForm.append(serverURL);
        addressForm.addCommand(okCommand);
        addressForm.addCommand(exitCommand);
        addressForm.setCommandListener(this);

        // The connect form
        connectForm = new Form("Connecting");
        messageLabel = new StringItem(null, "Connecting...\nPlease wait.");
        connectForm.append(messageLabel);
        connectForm.addCommand(backCommand);
        connectForm.setCommandListener(this);

        // The display form
        displayForm = new Form("Server Reply");
        messageLabel = new StringItem(null, null);
        displayForm.append(messageLabel);
        displayForm.addCommand(backCommand);
        displayForm.setCommandListener(this);
    }
}

please help me to open a webpage through passing the url............

That code that you provided is absolutely fine. I run the code and tried few pages, the output depends on "language" that is used in the site plus security implied.
The screenshot attached to post show output from my website that is just under construction, plain html used.
Output bellow is from daniweb

Response code = 200
Response message = OK
date = Mon, 03 Dec 2007 16:53:53 GMT
server = Apache/2.2
x-powered-by = PHP/5.1.6
set-cookie = bbsessionhash=782b9a7aa2d85821ddd6cea62f4fdcd8; path=/; domain=.daniweb.com
set-cookie = bblastvisit=1196700833; expires=Tue, 02-Dec-2008 16:53:53 GMT; path=/; domain=.daniweb.com
set-cookie = bblastactivity=0; expires=Tue, 02-Dec-2008 16:53:53 GMT; path=/; domain=.daniweb.com
cache-control = private
pragma = private
vary = Accept-Encoding,User-Agent
transfer-encoding = chunked
content-type = text/html; charset=UTF-8

I do not know what exactly you trying to do, so if you can please provide more detailed explenation

PS: For admin/moderator, if you think this is a breach of security please remove that data about daniweb

once again thanx for ur reply.what we have tried to do is to display an webpage .In midlet while we have entered the url whether it is possible to display an webpage..

As i have said before we have tried a lot to open and view a web page in a midlet.But we have conected an emulator to browser and it is working properly.So please help us to open and view a web page.

To open a webpage on mobile phone in visual form you need either micro browser whch is on advanced mobile phones or PDAs. Else you had to create MIDlet that will translate receive content of webpage to visual form, which will be pointless.

so is there any program to do same thing using Blackberry.

I have no experience with Blackberry (I do not own one or plan to have one), but most latest one let user browse the net.
However the main problem with browsing net with mobile device is, that there is very few sites that are optimized for mobile device. Because of that visitor is confused and discouraged from futher use

Thanks for your information.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.