Please help me with code.
I have create WebApplication/HelloServlet and Midlet using netbean 6.7

When i access Servlet using IE is see "Hello World!"

But when i access the same from emulator calling it in Midlet.

i see following code on my emulator screen. Is this the correct output?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
 
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Hello World!</h1>
    </body>
</html>

My Serlvet code is

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hcsales;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author 
 */
public class HelloServlet extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet HelloServlet</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet HelloServlet at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
            */
        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

And the Midlet code is

*/
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

/**
 * An example MIDlet to invoke a servlet.
 */

public class InvokeServletMidlet1 extends MIDlet {

   private Display display;

   String url = "http://localhost:38877/HelloServlet/";


   public InvokeServletMidlet1() {
      display = Display.getDisplay(this);
   }

   /**
    * Initialization. Invoked when we activate the MIDlet.
    */
   public void startApp() {
      try {
         invokeServlet(url);
      } catch (IOException e) {
         System.out.println("IOException " + e);
         e.printStackTrace();
      }
   }

   /**
    * Pause, discontinue ....
    */
   public void pauseApp() {
   }

   /**
    * Destroy must cleanup everything.
    */
   public void destroyApp(boolean unconditional) {
   }

   /**
    * Retrieve a grade....
    */
   void invokeServlet(String url) throws IOException {
      HttpConnection c = null;
      InputStream is = null;
      StringBuffer b = new StringBuffer();
      TextBox t = null;
      try {
         String message = "Daniel+Agaba%21";
         c = (HttpConnection)Connector.open(url);
         c.setRequestMethod(HttpConnection.GET);
         String p= c.toString();
         c.setRequestProperty("User-Agent","Profile/MIDP-1.0 Confirguration/CLDC-1.0");
         c.setRequestProperty("Content-Language", "en-CA");
         is = c.openDataInputStream();
         int ch;
         int s = is.read();
         while ((ch = is.read()) != -1) {
            b.append((char) ch);
         }
         t = new TextBox("First Servlet", b.toString(), 1024, 0);
      } finally {
         if(is!= null) {
            is.close();
         }
         if(c != null) {
            c.close();
         }
      }
      display.setCurrent(t);
   }
}

That is correct because you accessing JSP page you get all data what put together content of the page but midlet doesn't know what to do with it. Where on other hand browser know about handling this content

Thanks Mr.Peter for your immediate reply.

How do i display the same like in Web page? by converting the content.?

Pls help

You can pass this to mobile browser like Opera, but generally midlet-servlet communication is about getting/passing data from/to database. What is your actual aim with this?

Thanks again Mr.Peter.
The aim is to create JavaFx mobile Application which querys data from mysql or ms access.

And the output should be similar to javafx local search example.

With displaying product pics as well.

Please advice on the approach for the project.

JavaFX is just add-on I'm not really interested, beside none of the present mobile devices so far support it even with all the hype Sun created around it. Servlet database connection is very simple and you should already know it, if not you can have quick look on this thread (exclude JSP, you are only interested in servlet to database communication). For midlet to servlet communication have look at chapter 10 from Beginning J2ME : From Novice to Professional (some pages may be missing as it is only Google Books limited preview)

Thanks Sir !! Thanks for the additional help. Will go thru the related links.

All the Best & Take Care.

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.