Wednesday, September 15, 2010

How to call JSP page from the MIDlet



First, I will discuss the HttpConnection interface that can be used to establish Http Connection

HttpConnection Interface

Connected Limited Device Configuration (Connected Limited Device configuration. Referred to as CLDC) provides a set of classes for the network connection is the common connection framework. A kind of platform-independent connectivity framework provides a hierarchical connection interface, it realize the operating system from the summary table to provide specific equipment (such as Mobile Information Device Profile (MIDP)).

MIDP HttpConnection HTTP, through the provision of support to extend the framework to achieve the general connection framework CLDC role. Implementation of all MIDP applications require support for HTTP, because HTTP is mainly through the use of IP-based protocols (such as TCP / IP) can also use non-IP protocols (such as WAP) to achieve.

All connections are using Connector class, open () method to create, and if the connection is successful, this method returns an excuse to achieve a common connection object, one example, the following code segment can be used to open a to a URL of the HTTP connection.

String url = http://www.ora.com/whatif.jsp;;

HttpConnection connection = Connector.open (url);

Once a connection is established, you can set properties, and then you can create I / O streams to send or receive data. For example, consider the following snippet of code that is used to set the property and the establishment of input / output stream. / / Set HTTP properties

/ / Set HTTP properties connection.setRequestMethod (HttpConnection.POST); connection.setRequestProperty ("IF-Modified-Since", "22 Dec 2001 16:33:19 GMT"); connection.setRequestProperty ("User-Agent" ;, "Profile/MIDP-1.0 Configuration/CLDC-1.0"); connection.setRequestProperty ("Content-Language", "en-CA"); / / create I / O stream InputStream is = connection.openInputStream (); OutputStream os = connection.openOutputStream ();

The JSP which wishes to obtain a value of the variable named name, if this value is made, creates an instance of Date, then the value of name and date will be hitting the client's output stream.

Now, let us see how to write a MIDlet to call the JSP page, we will use the POST request method to call it, which means that the data passed to JSP page instead of using URL-encoded, but in a separate information introduction, this MIDlet code such as code 2 below.

Code 2:

InvokeJSPMidlet.java import javax.microedition.lcdui .*; import javax.microedition.midlet .*; import javax.microedition.io .*; import java.io. *; public class InvokeJSPMidlet extends MIDlet implements CommandListener (; Display display = null ; / / name field TextField name = null; form form; String url = "http://127.0.0.1:8080/examples/jsp/today.jsp";; static final Command callCommand = new Command ("date ? ", Command.OK, 2); static final Command clearCommand = new Command (" clear ", Command.STOP, 2); String myname; public InvokeJSPMidlet () (; display = Display.getDisplay (this); name = new TextField ("Name:", "", 25, TextField.ANY); form = new form ("Invoke JSP");); public void startApp () throws MIDletStateChangeException (; form.append ( name); form.addCommand (clearCommand); form.addCommand (callCommand); form.setCommandListener (this); display.setCurrent (form);); public void pauseApp () (;); public void destroyApp (boolean unconditional) ( ; notifyDestroyed ();); void invokeJSP (String url) throws IOException (; HttpConnection c = null; InputStream is = null; OutputStream os = null; StringBuffer b = new StringBuffer (); TextBox t = null; try (; c = (HttpConnection) Connector.open (url); c.setRequestMethod (HttpConnection.POST); c.setRequestProperty ("IF-Modified-Since", "29 Dec 2001 15:17:19 GMT"); c.setRequestProperty ( "User-Agent", "Profile/MIDP-1.0 Configuration/CLDC-1.0"); c.setRequestProperty ("Content-Language", "en-CA"); c.setRequestProperty ("Content- Type "," application / x-www-form-urlencoded "); os = c.openOutputStream (); os.write ((" name = "+ myname). getBytes ()); os.flush () ; is = c.openDataInputStream (); int ch; while ((ch = is.read ())! = -1) (; b.append ((char) ch); System.out.print ((char) ch );); t = new TextBox ("Date", b.toString (), 1024, 0); t.setCommandListener (this);); finally (; if (is! = null) (; is.close ( );); if (os! = null) (; os.close ();); if (c! = null) (; c.close (););); display.setCurrent (t);); public void commandAction (Command c, Displayable d) (; String label = c.getLabel (); if (label.equals ("clear")) (; destroyApp (true);); else if (label.equals (" date? ")) (; myname = name.getString (); try (; invokeJSP (url);); catch (IOException e) (;););););

InvokeJSPMidlet code specifies the JSP page to be called the URL, then create two command buttons, and then create a text field that allows users to enter a name inside. In InvokeJSP () method, to establish a connection to this URL, HTTP, and then the establishment of I / O streams, MIDlet using the output stream to send data to the JSP page, and then re-use the input stream to receive data from the JSP page, note that In this case, we will send the name to the JSP page, in fact it is just to show you about how the data flow between the MIDlet and pages.

In the code section 2, should be borne in mind is to make JSP pages using the getParameter () to obtain data from the variable name the value, you must set the Content-Type attribute is application / x-www-form-urlencoded.

Summary

This article only demonstrates how to call JSP page from a MIDlet, InvokeJSPMidlet also can be easily modified to achieve the purpose of calling the other JSP. But be careful, JSP, and HTML with the main use, but if your mobile device's browser can not handle HTML, then XML is also a very good choice, because the MIDlet can parse XML documents.







Recommended links:



HD DVD SUPPORTS Dolby TrueHD New Firmware



GEM made the most rich of 10



Msvideo



Wmv to flv converter free



Introduction to Fibre Channel based: FCIP and iSCSI comparison



Interview with B & Q (China) Vice President, Human Resources Director Miss Hu Weiyan



Comment Active X



Rmvb ps3



Specialist Access Control



Nokia's Enemies



Report Compilers And Interpreters



Concerned About the anti-monopoly law enforcement agency jurisdiction overlap



INTRODUCTION Icons



Guo Wei, CEO of God Code: I not only more CHAOS Chuluanbujing also more exciting



Hiddink looking like the KIND of CEO



realplayer FLV



No comments:

Post a Comment