Communication between Servlet and CGI

Source: Internet
Author: User

When using wireless devices such as mobile phones to access the Internet, you often need to submit some data through forms, such as login operations. Generally, the server uses Servlet and CGI to perform these operations.

As the basic configuration of restricted devices, CLDC usually provides a general connection framework for developers to connect to the network for development. In addition, MIDP also provides the HttpConnection interface, which is part of javax. microedition. io and defines the methods and constants required for the most basic HTTP connection.

HTTP programming principles
HTTP is a request-corresponding application protocol, which requires that parameters must be set before a request is sent. For example, after you click the submit button of the form, the content in the form will be sent to the server as part of the request.

Request Method Type
There are currently two methods to submit requests to the server: GET and POST. These two methods determine how data is submitted to the server.

The value to be submitted in GET mode is sent to the server as part of the URL. The submitted value will be the environment variable QUERY_STRING.
The value submitted in POST mode is sent to the server as an input stream. The stream length is placed in CONTENT_LENGTH.

The POST method is more secure than the two methods. The POST method can also transmit multiple types of data.
Example of using GET to submit information
The following is an HTML example of submitting a form using GET:

Action = "http://www.somesite.com/cgi-bin/getgrade.cgi"
Method = "GET">
Student #:

The form is submitted to http://www.somesite.com/cgi-bin/getgrade.cgi. when a user inputs a student ID, such as 123333, click the Retrieve Marks button, the data in the form is sent to the CGI program as part of the URL. The submitted address is http://www.somesite.com/cgi-bin/gergrade. cgi? Idnum = 123333. When data is submitted in POST mode, the input value is sent to the server as the input stream of the segment.

When a GET user input contains spaces, these spaces are replaced by (+). When a user submits multiple values at a time, these values are separated.

Servlet programming principles

Servlet is similar to CGI. Servlets supports programming of requests and responses. When a client sends a request to the server, the server sends the request to the Servlet. The Servlet organizes a response and sends it back to the client. The difference between Servlet and CGI is that Servlet uses a process to process multiple requests.

When the client submits a request, the Servlet service method is called and the request is passed to the request and response. The Servlet judges whether the request is post or get, and decides whether to use HttpServlet. doGet or HttpServlet. doPost to process the request. Both methods call HttpServletRequest and HttpServletResponse.

Use MIDlet to activate CGI scripts

After learning about basic http get, POST, and Servlets, let's look at an example. The first example shows how to use the MIDlet POST method to activate a CGI script.

In this example, when the cgi connection pgrade. CGI is enabled, the output and input streams are opened. The input content is sent through the output stream. The response is obtained through the input stream. The CGI script is written in PERL. After obtaining the student ID in the script, you can find the student ID record in the database. If the student ID is found, relevant information is returned to the requesting client. Because no form can be used for submission in the MIDlet, the content must be implemented through the stream writing method. The following is the MIDlet code.

 
 
  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. * using the POST method.  
  9. **/  
  10.  
  11. public class PostMidlet extends MIDlet {  
  12. private Display display;  
  13. String url = "http://somesite.com/cgi-bin/pgrade.cgi";  
  14.  
  15. public PostMidlet() {  
  16. display = Display.getDisplay(this);  
  17. }  
  18.  
  19. //Initialization. Invoked the MIDlet activates.  
  20. public void startApp() {  
  21. try {  
  22. getGrade(url);  
  23. } catch (IOException e) {  
  24. System.out.println("IOException " + e);  
  25. e.printStackTrace();  
  26. }  
  27. }  
  28.  
  29. //Pause, discontinue ....  
  30. public void pauseApp() { }  
  31.  
  32. //Destroy must cleanup everything.  
  33. public void destroyApp(boolean unconditional) { }  
  34.  
  35. //Retrieve a grade.  
  36. void getGrade(String url) throws IOException {  
  37. HttpConnection c = null;  
  38. InputStream is = null;  
  39. OutputStream os = null;  
  40. StringBuffer b = new StringBuffer();  
  41. TextBox t = null;  
  42. try {  
  43. c = (HttpConnection)Connector.open(url);  
  44. c.setRequestMethod(HttpConnection.POST);  
  45. c.setRequestProperty("IF-Modified-Since", "20 Jan 2001 16:19:14 GMT");  
  46. c.setRequestProperty("User-Agent", "Profile/MIDP-1.0 Configuration/CLDC-1.0");  
  47. c.setRequestProperty("Content-Language", "en-CA");  
  48. os = c.openOutputStream();  
  49.  
  50. // send request to the CGI script  
  51. String str = "name=163748";  
  52. byte postmsg[] = str.getBytes();  
  53. for(int i=0; < postmsg.length;i++) {  
  54. os.write(postmsg[i]);  
  55. }  
  56. os.flush();  
  57.  
  58. //receive response and display in a text box.  
  59. is = c.openDataInputStream();  
  60. int ch;  
  61. while((ch = is.read()) != -1) {  
  62. b.append((char) ch);  
  63. System.out.println((char)ch);  
  64. }  
  65. t = new TextBox("Final Grades", b.toString(), 1024, 0);  
  66. } finally {  
  67. if(is!= null) {  
  68. is.close();  
  69. }  
  70. if(os != null) {  
  71. os.close();  
  72. }  
  73. if(c != null) {  
  74. c.close();  
  75. }  
  76. }  
  77. display.setCurrent(t);  
  78. }  
  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.