Use MIDlet to activate Servlet

Source: Internet
Author: User

Use MIDlet to activate Servlet. You can activate Servlet like activating a CGI in MIDlet. This section describes two examples:

In the first example, use the GET operation to activate the Servlet and display the result.
The second example is that Servlet accepts the user's POST data from the mobile phone.

In the following example, the FirstMidletServlet is activated by the GET method and is returned to the mobile phone. In this example, no data is submitted to the Servlet. After the Servlet is activated, the string "Servlet Invoked" and date are returned to the client.

Below is the MIDlet code FirstMidletServlet. java

 
 
  1. import java.io.*;  
  2. import javax.microedition.io.*;  
  3. import javax.microedition.lcdui.*;  
  4. import javax.microedition.midlet.*;  
  5.  
  6. /**  
  7. * An example MIDlet to invoke a CGI script.  
  8. */  
  9.  
  10. public class FirstMidletServlet extends MIDlet {  
  11. private Display display;  
  12. String url = "http://somesite.com/servlet/HelloServlet";  
  13.  
  14. public FirstMidletServlet() {  
  15. display = Display.getDisplay(this);  
  16. }  
  17.  
  18. //Initialization. Invoked when MIDlet activates  
  19. public void startApp() {  
  20. try {  
  21. invokeServlet(url);  
  22. } catch (IOException e) {  
  23. System.out.println("IOException " + e);  
  24. e.printStackTrace();  
  25. }  
  26. }  
  27.  
  28. //Pause, discontinue ....  
  29. public void pauseApp() { }  
  30.  
  31. //Destroy must cleanup everything.  
  32. public void destroyApp(boolean unconditional) { }  
  33.  
  34. //Prepare connection and streams then invoke servlet.  
  35. void invokeServlet(String url) throws IOException {  
  36. HttpConnection c = null;  
  37. InputStream is = null;  
  38. StringBuffer b = new StringBuffer();  
  39. TextBox t = null;  
  40. try {  
  41. c = (HttpConnection)Connector.open(url);  
  42. c.setRequestMethod(HttpConnection.GET);  
  43. c.setRequestProperty("IF-Modified-Since", "20 Jan 2001 16:19:14 GMT");  
  44. c.setRequestProperty("User-Agent", "Profile/MIDP-1.0 Configuration/CLDC-1.0");  
  45. c.setRequestProperty("Content-Language", "en-CA");  
  46. is = c.openDataInputStream();  
  47. int ch;  
  48.  
  49. // receive response and display it in a textbox.  
  50. while ((ch = is.read()) != -1) {  
  51. b.append((char) ch);  
  52. }  
  53. t = new TextBox("First Servlet", b.toString(), 1024, 0);  
  54. } finally {  
  55. if(is!= null) {  
  56. is.close();  
  57. }  
  58. if(c != null) {  
  59. c.close();  
  60. }  
  61. }  
  62. display.setCurrent(t);  
  63. }  

The following is the HelloServlet code HelloServlet. java that returns "Servlet Invoked" and date.

 
 
  1. import java.io.*;  
  2. import java.util.*;  
  3. import javax.servlet.*;  
  4. import javax.servlet.http.*;  
  5.  
  6. /**  
  7. * The simplest possible servlet.  
  8. */  
  9.  
  10. public class HelloServlet extends HttpServlet {  
  11. public void doGet(HttpServletRequest request 

The above are two examples of activating Servlet with MIDlet.

  1. Wizard for creating Servlet Filters
  2. Install Servlet and JSP development tools
  3. Extended Future Response Servlet
  4. Servlet Container matching process
  5. User cache Servlet

Related Article

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.