3 Replies Latest reply on Jul 22, 2008 11:10 AM by Peter Johnson

    accessing JBoss servlet from midlet

    Arvind Nadagouda Newbie

      I followed these steps :--
      ---------------------------------------------------------------------------------------------------------------------
      1. Building a Servlet :
      Copy and Paste the below Hello Servlet CODE in TextPad and Save it as
      HelloServlet.java
      A Sample HelloServlet.java Code:
      // Import the servlet classes
      import javax.servlet.*;
      import javax.servlet.http.*;

      // Import the standard Java classes
      import java.io.*;
      import java.util.*;

      /**
      * Hello, Servlet Example
      */
      public class HelloServlet extends HttpServlet
      {
      public void init() throws ServletException {
      // Initialization here...
      }

      public void service( HttpServletRequest req, HttpServletResponse res )
      throws ServletException
      {
      // Read the request
      String name = req.getParameter( "name" );
      if( name == null ) {
      name = "Servlet";
      }

      try
      {
      // Write the response
      PrintWriter out = res.getWriter();
      out.print( "Hello, " +
      name +
      "<h1>Hello, " +
      name + "</h1>" );
      }
      catch( IOException ioe )
      {
      throw new ServletException( ioe );
      }
      }
      public void destroy() {
      // Cleanup here...
      }
      }
      ------------------------------------------------------------------------------------------------------------------------
      2. Compiling the Servlet

      To compile the Servlet, you need to add the Servlet classes to your CLASSPATH. The classes are located in different JAR files depending on the Servlet container you are going to use. If you are using Jboss , there is a javax.servlet.jar file located in the <jboss-me>/server/default/lib folder. In my case it is located in C:\jboss-4.2.2.GA\server\default\deploy\appln\webapp\ compile-lib
      set classpath=.;<java-home>\ server\default\deploy\appln\webapp\compile-lib\javax.servlet.jar

      Compile the HelloServlet code using following code :-

      javac HelloServlet.java

      -------------------------------------------------------------------------------------------------------------
      3.Deploying the Servlet

      To run the Servlet you must first build a deployment descriptor and then construct a Web Archive (WAR file) that contains both the compiled class as well as the deployment descriptor. The following is a Web deployment descriptor for the HelloServlet example:

      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE web-app PUBLIC '-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN'
      'http://java.sun.com/dtd/web-app_2_3.dtd'>

      <web-app>

      <servlet-name>MyHelloServlet</servlet-name>
      <servlet-class>HelloServlet</servlet-class>


      <servlet-mapping>
      <servlet-name>MyHelloServlet</servlet-name>
      <url-pattern>/*</url-pattern>
      </servlet-mapping>
      </web-app>

      The deployment descriptor creates a logical name for the Servlet in the 's <servlet-name> tag; this name will be used to refer to this Servlet else where in the deployment descriptor. The <servlet-name> is mapped to a class that implements that Servlet; in this case MyHelloServlet is the name mapped to the HelloServlet (if your Servlet was in a package you would specify the fully qualified class name, including the package.) The <servlet-mapping> node defines URL patterns to send to the named Servlet. In this case we match all requests sent to our Web Application to MyHelloServlet. If you had multiple Servlets you would want to uniquely identify each one: for example if you changes "/*" to "/hi" then the Servlet would only be invoked it the web application's "/hi" URL was requested. The deployment descriptor must be named "web.xml".
      Now that we have a deployment descriptor, we have to build a Web Archive.
      WAR files are simply JAR files with a .WAR extension; you can build them using the jar command that ships with the Java SDK.

      Now go to deploy Directory ( ie, C:\jboss-4.2.2.GA\server\default\deploy ) and follow the below steps :-
      a. Create a HelloServlet directory .
      b. Create WEB-INF directory in HelloServlet directory .
      c. Create another classes subdirectory in WEB_INF directory .
      d. Now copy HelloServlet.class file in classes directory .
      e. Copy web.xml WEB-INF directory

      The files need to appear in the following directory structure inside HelloServlet directory:
      /WEB-INF/
      /WEB-INF/web.xml
      /WEB-INF/classes
      /WEB-INF/classes/HelloServlet.class
      Execute the jar command.
      jar –cvf hello.war *
      The result will be a new hello.war file in your current directory.
      The final step is to deploy your Servlet. Using JBoss, you need to do two things:
      1. 1. Start the JBoss Server by executing the run.bat (or run.sh on Unix) in the <jboss-home>/bin directory
      2. 2. Copy the hello.war file to the <jboss-home>/server/default/deploy directory
      When JBoss is running it monitors the deploy directory for new files; if you copy a new version of the file to that directory, it will detect it and deploy your new code for you.
      4.Testing the Servlet
      Now that your Servlet is built and deploy, it is time to test it. Open a web browser to direct it to the following URL:

      http://localhost:8080/hello

      In this case the name of the Web Application, "hello", matches the name of the WAR file; when a request is made for anything starting with "hello", our deployment descriptor is loaded and the URL mappings are checked. We specified that "/*", or any request made to our Web Application, be sent to HelloServlet. The result of this query is the following HTML document:


      Hello, Servlet
      <h1>Hello, Servlet</h1>


      Looking back at the code for HelloServlet, you might have noticed that we tried to pull out a "name" parameter from the request object. If the name was null then we used the name "Servlet". So now try invoking the Servlet with the following URL:
      http://localhost:8080/hello?name=InformIT

      That request yields the following HTML document:

      Hello, InformIT
      <h1>Hello, InformIT</h1>

      The question mark denotes that the URL contains a parameter list is accompanying the URL request. Parameters follow the form of:
      name1=value1&name2=value2&...
      Where parameters are differentiated by ampersands. When HelloServlet calls getParameter("name") it returns it "InformIT".

      --------------------------------------------------------------------------------------------------------------------------------------

      From the above steps i am able to deploy the data in jboss and Display the servlet o/p in the web browser ..

      But i want to Display the web browser o/p in my j2me tool kit simulator ...


      --------------------------------------------------------------------------------------
      My MidLet code is :--

      import javax.microedition.lcdui.*;
      import javax.microedition.midlet.*;

      public class HelloMIDlet
      extends MIDlet
      implements CommandListener {
      private Form mMainForm;

      public HelloMIDlet() {
      mMainForm = new Form("HelloMIDlet");
      mMainForm.append(new StringItem(null, "Hello, MIDP!"));
      mMainForm.addCommand(new Command("Exit", Command.EXIT, 0));
      mMainForm.setCommandListener(this);
      }

      public void startApp() {
      Display.getDisplay(this).setCurrent(mMainForm);
      }

      public void pauseApp() {}

      public void destroyApp(boolean unconditional) {}

      public void commandAction(Command c, Displayable s) {
      notifyDestroyed();
      }
      }

      ----------------------------------------------------------------------------------------------------------------

      I can run this code in simulator and get the o/p HelloMidlet ,
      But i want to display the servlet o/p ..

      I have entered my Url ie, http://localhost:8080/Hello in toolkit settings--> user defined .. But when i run my program it shows connecting... and gives the following error :--

      Running with locale: English_United States.1252
      Uncaught exception java/lang/IllegalArgumentException: Null URL.


      If anybody know how to solve this .... Please let me know..
      thanx in advance..



        • 1. Re: accessing JBoss servlet from midlet
          Peter Johnson Master

          What is "o/p"?

          Why did you post instructions for how to build things? That makes it hard to distinguish between the problems you are encountering and build steps. In the future, state the problem first, then attach any relevant files. And use the Code button - its there for a reasons.

          Now, are you saying that when you enter the URL http://localhost:8080/Hello in your browser that you are not getting any response? (As I said, there is some much text here it is difficult for me to determine the problem your are trying to point out.)

          • 2. Re: accessing JBoss servlet from midlet
            Arvind Nadagouda Newbie

            Thanx for reply Peter ..

            Actually i am getting "Hello Servlet" as o/p in my web browser ... So I dont have any problem in getting the servlet o/p in the browser ..

            But my problem is i want the Hello Servlet o/p in my Simulator ( J2me tool kit ) .... So I want to access that o/p from Jboss to my Simulator or in other words the same Hello Servlet o/p , i should get in my simulator..
            --------------------------------------------------------------------------------
            [ i followed these steps to get that o/p
            I have entered my Url ie, http://localhost:8080/Hello in toolkit settings--> user defined ..
            But when i run my program in the simulator it shows connecting... and gives the following error :--

            Running with locale: English_United States.1252
            Uncaught exception java/lang/IllegalArgumentException: Null URL.
            ]
            ------------------------------------------------------------------------------------

            Any solutions...

            Thanx

            • 3. Re: accessing JBoss servlet from midlet
              Peter Johnson Master

              I still have no idea what "o/p" is...

              I have no idea how your emulator works, so the following is just a guess. I am guessing that the emulator might also be emulating a network, in which case localhost will not work as part of the URL. If that is the case, you should use the host name, and start JBossAS with the -b option (see http://wiki.jboss.org/wiki/JBoss42FAQ).

              If that does not solve the problem, you really need to ask the people who make the emulator how to do this. Because everything works from a browser, this is not a JBossAS issue.