Title: Mobile Computing
1Mobile Computing
- Session 10
- J2ME
- S. K. Nayak
- Synergy, Dhenkanal
2Why Java Technologyfor 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!
3JAVA Architecture
4Overview 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.
5Elements 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).
6Elements of J2ME Architecture
7Configurations
- 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
8J2ME Connected LimitedDevice 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
10J2ME Mobile InformationDevice Profile (MIDP)
11J2ME-CLDC Stack
12MIDlet Life Cycle
- MIDP applications, or MIDlets, move from state
to state in their lifecycle according to a state
diagram - MIDlet states include
- Pausedinitialized and quiescent (waiting)
- Activehas resources and is executing
- Destroyedhas released all resources, destroyed
threads, and ended all activity
13J2ME APIs
- 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
14CLDC classes inherited from J2SE
15CLDC Specific Classes
16MIDP 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
17J2ME 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
18Some Links to Start with J2ME
- http//developers.sun.com/techtopics/mobility/lear
n/midp/getstart - http//developers.sun.com/techtopics/mobility/midp
/articles/wtoolkit/ - http//developers.sun.com/techtopics/mobility/midp
/articles/tutorial2/ - http//developers.sun.com/techtopics/mobility/midp
/articles/api/
19Sun J2ME Wireless Toolkit
- Install J2SE SDK
- Available at http//java.sun.com/j2se/.
- Add bin subdirectory to the path.
- Install J2ME Wireless Toolkit
- Available at http//java.sun.com/products/j2mewtoo
lkit/ . - Run KToolbar.
- Use Any IDE for coding java
- Eclipse, netbeans etc.
20MIDP 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
21Sun J2ME Wireless Toolkit
22Creating a new Project
23(No Transcript)
24(No Transcript)
25(No Transcript)
26Server 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
27Installing Tomcat
- Download Tomcat server
- Available at http//jakarta.apache.org/builds/jaka
rta-tomcat-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 http//localhost8080/ - Further details of Installation refer to
http//jakarta.apache.org/tomcat/tomcat-4.1-doc/RU
NNING.txt
28Writing 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
29Compiling and deploying the servlet
- Compiling
- Execute the following shell commands
- export CLASSPATH\jakarta-tomcat-4.1.27\common\li
b\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 - lt!-- Tomcat Examples Context --gt
- ltContext path"/examples" docBase"examples"
debug"0" - reloadable"true" crossContext"true"gt
- Above or below this lengthy context entry (it's
closed by lt/Contextgt, 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. - lt!-- MIDP Context --gt
- ltContext path"/midp" docBase"midp"
reloadable"true"/gt - Once you're finished adding the context entry,
save the file.
30Create a web-application config
- Copy the following text and save it as
webapps/midp/WEB-INF/web.xml under the Tomcat
root directory - lt?xml version"1.0" encoding"ISO-8859-1"?gt
- lt!DOCTYPE web-app
- PUBLIC "-//Sun Microsystems, Inc.//DTD Web
Application 2.3//EN" - "http//java.sun.com/dtd/web-app_2_3.dtd"gt
- ltweb-appgt
- ltservletgt
- ltservlet-namegtmyServletlt/servlet-namegt
- ltservlet-classgtHitServletlt/servlet-classgt
- lt/servletgt
-
- ltservlet-mappinggt
- ltservlet-namegtmyServletlt/servlet-namegt
- lturl-patterngt/hitslt/url-patterngt
- lt/servlet-mappinggt
- lt/web-appgt
-
31Hooking 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()
-
-
-
32Hooking 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 bytecontentLength
- int length in.read(raw)
- in.close() hc.close()
- // Show the response to the user.
- String s new String(raw, 0, length)
33Build 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.
34MIDP Programming
- General MIDP Tutorials http//developers.sun.com/t
echtopics/mobility/midp/reference/techart/ - Useful Links
- Event Handling (http//developers.sun.com/techtopi
cs/mobility/ - midp/articles/event/index.html)
- Client Server Communication( http//developers.sun
.com/ - techtopics/mobility/midp/ttips/clientserv/index.ht
ml) - Session Handling ( http//developers.sun.com/techt
opics/ - mobility/midp/articles/sessions/index.html)
35Recommended 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
36References
- J2ME
- http//java.sun.com/j2me/
- http//www.billday.com/j2me/
- Java FAQ
- http//java.sun.com/products/jdk/faq.html
- Online Java Tutorial
- http//java.sun.com/docs/books/tutorial/
- JSP Tutorial
- http//www.jsptut.com/