Presentation is loading. Please wait.

Presentation is loading. Please wait.

Session 10 J2ME Prof. Sridhar Iyer IIT Bombay

Similar presentations


Presentation on theme: "Session 10 J2ME Prof. Sridhar Iyer IIT Bombay"— Presentation transcript:

1 Session 10 J2ME Prof. Sridhar Iyer IIT Bombay
IT 601: Mobile Computing Session 10 J2ME Prof. Sridhar Iyer IIT Bombay

2 Why Java™ Technology for Small Devices?
The nature of wireless devices is changing Old devices: All the software hard-coded by the device manufacturer New devices: Can be customized by dynamically loading software over the air New opportunities for manufacturers, operators, and developers!

3 JAVA Architecture

4 Overview of the Java 2 Platform
J2EE A comprehensive platform for multi-user, enterprise-wide applications. based on J2SE and adds APIs for server-side computing. J2SE designed for desktop computers. Mostly runs on top of OS X, Linux, Solaris, or Microsoft Windows. J2ME subset of J2SE components set of technologies and specifications developed for small devices like pagers, mobile phones, and set-top boxes. J2ME uses smaller virtual machines and leaner APIs.

5 Elements of J2ME Architecture
Profile: Defines the Environment API exposing the functionality of a specific class of target devices, and necessary to support a particular set of services Optional Package: Augments the Environment APIs exposing specific functionality Deployment determined by the Platform Vendor Configuration: Defines the VM API exposing the minimal sized, preexisting profile defined for the specific VM it is deployed against. VM is either a full JVM or some subset of it (KiloByte VM).

6 Elements of J2ME Architecture

7 Configurations Connected, Limited Device Configuration(JSR-30,139)
Smallest mobile devices Phones, Pagers, PDAs (small) 128K to 2MB with Profile and Optional Packages Network connectivity usually limited Connected Device Configuration (JSR-36) Larger devices, some mobile, some not TVs, PDAs (larger), Communicators, Cars, Gateways 2MB and up with Profile and Optional Packages Connectivity includes TCP/IP

8 J2ME Connected Limited Device Configuration (CLDC)

9 Profiles for CLDC Mobile Information Device Profile (JSR-37, 118)
Will work with CLDC Volume Wireless Handsets LCDUI based GUI PDA Profile (JSR-75) References MIDP Smaller memory PDAs AWT based GUI

10 J2ME Mobile Information Device Profile (MIDP)

11 J2ME-CLDC Stack

12 MIDlet Life Cycle MIDP applications, or “MIDlets”, move from state to state in their lifecycle according to a state diagram MIDlet states include: Paused—initialized and quiescent (waiting) Active—has resources and is executing Destroyed—has released all resources, destroyed threads, and ended all activity

13 J2ME APIs not suitable for small devices with limited resources.
J2SE APIs require several megabytes of memory not suitable for small devices with limited resources. APIs for the CLDC, provide a minimum set of libraries useful for application development and profile definition for a variety of small devices. The CLDC library APIs can be divided into the following two categories: Classes that are a subset of the J2SE APIs: java.lang, java.io, and java.util packages Classes specific to the CLDC: These classes are located in the javax.microedition package

14 CLDC classes inherited from J2SE
ByteArrayInputStream, ByteArrayOutputStream, DataInput, DataOutput, DataInputStream, DataOutputStream, InputStream, OutputStream, InputStreamReader, OutputStreamWriter, PrintStream, Reader, Writer java.io Calendar, Date, Enumeration, Hashtable, Random, Stack, TimeZone, Vector java.util Boolean, Byte, Character, Class, Integer, Long, Math, Object, Runnable, Runtime, Short, String, StringBuffer, System, Thread, Throwable java.lang Classes Package

15 CLDC Specific Classes Classes Packages
Connection, ConnectionNotFoundException, Connector, ContentConnector, Datagram, DatagramConnection, InputConnection, OutputConnection, StreamConnection, StreamConnectionNotifier javax.microedition.io Classes Packages

16 MIDP Specific Classes MIDP-specific classes include
the javax.microedition.rms, javax.microedition.midlet, javax.microedition.lcdui packages Some additional classes, interfaces, and exception classes available: IllegalStateException class in the java.lang package Timer and TimerTask classes in the java.util package HttpConnection interface for HTTP protocol access over the network in the javax.microedition.io package

17 J2ME vs J2SE Reduced number support float, double primitives
Double, Float classes Extendable UI components Final High-level components New components subclass Canvas Lacks: JNI Sound API Serialization Reflection User defined class loaders

18 Some Links to Start with J2ME
midp/getstart articles/wtoolkit/ articles/tutorial2/ articles/api/

19 Sun J2ME Wireless Toolkit
Install J2SE SDK Available at Add bin subdirectory to the path. Install J2ME Wireless Toolkit Available at . Run KToolbar. Use Any IDE for coding java Eclipse, netbeans etc.

20 MIDP Development with J2MEWTK
Write your Java™ application (midlets) using any text editor or IDE Use J2ME Wireless Toolkit, Create a Project Follow the instructions and copy the source file to the location specified in the Wireless Toolkit Click on Build Run against any emulator

21 Sun J2ME Wireless Toolkit

22 Creating a new Project

23

24

25

26 Server Side Implementation
MIDlet: The application, a MIDlet, was designed for the Mobile Information Device Profile, one of the J2ME specifications. Servlet: Servlets are Java programs which run on the server. The MIDlets connect to servlets, which implement server side functionality. Next slides show how to write and deploy a servlet, and then how to hook up a MIDlet with the servlet

27 Installing Tomcat Download Tomcat server
Available at 4.0/nightly Unpack the distribution in to a directory and set the environment variable CATALINA_HOME to the path of the directory Upon starting the server, Tomcat 4.0 will be available by browsing: Further details of Installation refer to

28 Writing a Simple Servlet
HitServlet simply counts the number of times it's been invoked and writes back to the client a message containing the count. import javax.servlet.http.*; import javax.servlet.*; import java.io.*; public class HitServlet extends HttpServlet { private int mCount; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String message = "Hits: " + ++mCount; response.setContentType("text/plain"); response.setContentLength(message.length()); PrintWriter out = response.getWriter(); out.println(message); } Save the source code in a file under the Tomcat root directory named webapps/midp/WEB-INF/classes/HitServlet.java

29 Compiling and deploying the servlet
Execute the following shell commands: #export CLASSPATH=\jakarta-tomcat \common\lib\servlet.jar #javac HitServlet.java Deploying: you'll need to edit one of Tomcat's configuration files to tell Tomcat about the new web application. Open the conf/server.xml file with a text editor. In this file, web applications are called contexts. Scroll down to find the Context entry for the examples web application, which begins like this: <!-- Tomcat Examples Context --> <Context path="/examples" docBase="examples" debug="0" reloadable="true" crossContext="true"> Above or below this lengthy context entry (it's closed by </Context>, many lines down), create a new context entry for your new web application. It will look similar to the opening tag for the examples context, but you'll change the names to midp as appopriate and close the tag inline. <!-- MIDP Context --> <Context path="/midp" docBase="midp" reloadable="true"/> Once you're finished adding the context entry, save the file.

30 Create a web-application config
Copy the following text and save it as webapps/midp/WEB-INF/web.xml under the Tomcat root directory: <?xml version="1.0" encoding="ISO "?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" " <web-app> <servlet> <servlet-name>myServlet</servlet-name> <servlet-class>HitServlet</servlet-class> </servlet> <servlet-mapping> <url-pattern>/hits</url-pattern> </servlet-mapping> </web-app>

31 Hooking Up a MIDlet to the Servlet
Create a command action method in your MIDlet: public void commandAction(Command c, Displayable s) { if (c == mExitCommand) notifyDestroyed(); else if (c == mConnectCommand) { Form waitForm = new Form("Waiting..."); mDisplay.setCurrent(waitForm); Thread t = new Thread() { public void run() { connect(); } }; t.start();

32 Hooking Up a MIDlet to the Servlet
The Connect method private void connect() { HttpConnection hc = null; InputStream in = null; String url = getAppProperty("HitMIDlet.URL"); try { hc = (HttpConnection)Connector.open(url); in = hc.openInputStream(); int contentLength = (int)hc.getLength(); byte[] raw = new byte[contentLength]; int length = in.read(raw); in.close(); hc.close(); // Show the response to the user. String s = new String(raw, 0, length); mMessageItem.setText(s); } catch (IOException ioe) { mMessageItem.setText(ioe.toString()); mDisplay.setCurrent(mMainForm); Source code available here for download:

33 Build and run your MIDlet
Save it as HitMIDlet.java inside the apps/HelloSuite/src directory underneath the J2ME Wireless Toolkit root directory. Click on Settings..., then select the MIDlets tab. Click on Add and fill in "HitMIDlet" for both the MIDlet name and class name. You can leave Icon blank. Click on OK and you should see both HelloMIDlet and HitMIDlet listed. Define a system property that HitMIDlet uses as the URL for its network connection. (This property is retrieved in the third line of the connect() method.) In the toolkit, click on Settings..., then select the User Defined tab. Click on the Add button. Fill in the property name as HitMIDlet.URL; the value should be the URL that invokes HitServlet, the same URL you used in a browser to test the servlet. When you're finished, click on OK to dismiss the project settings window. Click on Build to build the project. Make sure your server is running first. Then click on Run and select HitMIDlet. Select the Connect command.

34 MIDP Programming General MIDP Tutorials Useful Links Event Handling ( midp/articles/event/index.html) Client Server Communication( techtopics/mobility/midp/ttips/clientserv/index.html) Session Handling ( mobility/midp/articles/sessions/index.html)

35 Recommended Programming Practices
Keep in mind that there are strict memory constraints on most CLDC target devices Allocate memory resources just before use Free resources as soon as they are not needed Avoid allocating small temporary chunks of memory to avoid heap fragmentation Make local copies of global variables Avoid redundant method calls, and inline functions rather than creating a new class

36 References J2ME Java FAQ Online Java Tutorial JSP Tutorial
Java FAQ Online Java Tutorial JSP Tutorial


Download ppt "Session 10 J2ME Prof. Sridhar Iyer IIT Bombay"

Similar presentations


Ads by Google