How to call JSP pages from the MIDlet

Source: Internet
Author: User

First, I will discuss the HttpConnection interface, which can be used to establish an Http connection.

HttpConnection Interface

Connected Limited Device Configuration (Limited connection Device Configuration. CLDC for short) provides a set of classes for network connection, which is a common connection framework. A platform independent connection framework that provides a hierarchical connection interface. The operating system is provided by a specific Device table (such as Mobile Information Device Profile (MIDP )).

MIDP extends the General connection framework of CLDC by providing an HTTP-supported HTTP connection framework. All MIDP application implementations require support for HTTP, mainly because HTTP can also be achieved through the use of IP-based protocols (such as TCP/IP) or through the use of non-IP protocols (such as WAP).

All connections are created using the open () method of the Connector class. If the connection is successful, this method returns an object that implements some common connection excuse. For example, the following code snippet can be used to open an HTTP connection to a URL.

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

HttpConnection connection = Connector. open (url );

Once a connection is established, you can set attributes and then create an I/O Stream to send or receive data. For example, see the following code to set attributes and create an input/output stream. // Set HTTP attributes

 
 
  1. // Set HTTP attributes
  2. Connection. setRequestMethod (HttpConnection. POST );
  3. Connection. setRequestProperty ("IF-Modified-Since", "22 Dec 2001 16:33:19 GMT ");
  4. Connection. setRequestProperty ("User-Agent", "Profile/MIDP-1.0 Configuration/CLDC-1.0 ");
  5. Connection. setRequestProperty ("Content-Language", "en-CA ");
  6. // Create an I/O Stream
  7. InputStreamIs=Connection. OpenInputStream ();
  8. OutputStreamOS=Connection. OpenOutputStream ();

In this JSP, you want to obtain the value of a variable named name. Once this value is obtained, a Date instance will be created, then the values of name and date will be hit in the output stream of the client.

Now, let's see how to write a MIDlet to call this JSP page. We will use the POST Request Method to call it, this means that the data transmitted to the JSP page is not URL encoded, but is passed in with a separate piece of information. This section of MIDlet code is shown in code segment 2.

Code 2:

 
 
  1. InvokeJSPMidlet. java
  2. Import javax. microedition. lcdui .*;
  3. Import javax. microedition. midlet .*;
  4. Import javax. microedition. io .*;
  5. Import java. io .*;
  6. Public class InvokeJSPMidlet extends MIDlet implements CommandListener {;
  7. DisplayDisplay=Null;
  8. // Name field
  9. TextFieldName=Null;
  10. Form;
  11. StringUrl="Http: // 127.0.0.1: 8080/examples/jsp/today. jsp";;
  12. Static final CommandCallCommand=NewCommand ("date? ", Command. OK, 2 );
  13. Static final CommandClearCommand=NewCommand ("clear", Command. STOP, 2 );
  14. String myname;
  15. Public InvokeJSPMidlet (){;
  16. Display=Display. GetDisplay (this );
  17. Name=NewTextField ("Name:", "", 25, TextField. ANY );
  18. Form=NewForm ("Invoke JSP ");
  19. };
  20. Public void startApp () throws MIDletStateChangeException {;
  21. Form. append (name );
  22. Form. addCommand (clearCommand );
  23. Form. addCommand (callCommand );
  24. Form. setCommandListener (this );
  25. Display. setCurrent (form );
  26. };
  27. Public void pauseApp (){;
  28. };
  29. Public void destroyApp (boolean unconditional ){;
  30. Yydestroyed ();
  31. };
  32. Void invokeJSP (String url) throws IOException {;
  33. HttpConnectionC=Null;
  34. InputStreamIs=Null;
  35. OutputStreamOS=Null;
  36. StringBufferB=NewStringBuffer ();
  37. TextBoxT=Null;
  38. Try {;
  39. C= (HttpConnection) Connector. open (url );
  40. C. setRequestMethod (HttpConnection. POST );
  41. C. setRequestProperty ("IF-Modified-Since", "29 Dec 2001 15:17:19 GMT ");
  42. C. setRequestProperty ("User-Agent", "Profile/MIDP-1.0 Configuration/CLDC-1.0 ");
  43. C. setRequestProperty ("Content-Language", "en-CA ");
  44. C. setRequestProperty ("Content-Type", "application/x-www-form-urlencoded ");
  45. OS=C. OpenOutputStream ();
  46. OS. write (("Name= "+ Myname). getBytes ());
  47. OS. flush ();
  48. Is=C. OpenDataInputStream ();
  49. Int ch;
  50. While ((Ch=Is. Read ())! =-1 ){;
  51. B. append (char) ch );
  52. System. out. print (char) ch );
  53. };
  54. T=NewTextBox ("Date", B. toString (), 1024, 0 );
  55. T. setCommandListener (this );
  56. }; Finally {;
  57. If (is! = Null ){;
  58. Is. close ();
  59. };
  60. If (OS! = Null ){;
  61. OS. close ();
  62. };
  63. If (c! = Null ){;
  64. C. close ();
  65. };
  66. };
  67. Display. setCurrent (t );
  68. };
  69. Public void commandAction (Command c, Displayable d ){;
  70. StringLabel=C. GetLabel ();
  71. If (label. equals ("clear ")){;
  72. DestroyApp (true );
  73. };
  74. Else if (label. equals ("date? ")){;
  75. Myname= Name. getString ();
  76. Try {;
  77. InvokeJSP (url );
  78. }; Catch (IOException e ){;
  79. };
  80. };
  81. };
  82. };

The InvokeJSPMidlet Code specifies the URL of the JSP page to be called, creates two command buttons, and creates a text field, allowing you to enter a name in it. In the InvokeJSP () method, an HTTP connection to this URL is established, and then an I/O Stream is established. The MIDlet uses the output stream to send data to the JSP page, then use the input stream to receive data from the JSP page. Note that in this example, we will send the name to the JSP page, in fact, it just shows you how data can flow between the MIDlet and page.

In code segment 2, you should note that the JSP page uses getParameter () to obtain data values from the name variable, you must set the Content-Type attribute to application/x-www-form-urlencoded.

Summary

This article only demonstrates how to call JSP pages from the MIDlet. InvokeJSPMidlet can also be easily modified to call other JSPs. However, note that JSP is mainly used in combination with HTML, but if the browser on your mobile device cannot process HTML, XML is also a good choice, because MIDlet can parse XML documents.

  1. JSP bean code optimization
  2. Detailed introduction to JSP environment configuration Scheme
  3. Use stored procedures in JSP JSTL
  4. Which of ASP. NET, JSP, and PHP is better?
  5. JSP-related software

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.