In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to realize the communication between Servlet and CGI". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to realize the communication between Servlet and CGI.
When surfing the Internet with wireless devices such as mobile phones, we often need to submit some data through the form, such as login operations. Usually, we use Servlet and CGI to perform these operations on the server side.
As the basic configuration of restricted devices, CLDC usually provides a general connection framework for developers and companies to carry out network development. In addition, MIDP also provides an interface to HttpConnection, which is part of javax.microedition.io, which defines the methods and constants required for the most basic HTTP connection.
The principle of HTTP programming
The HTTP protocol is a request-the corresponding application protocol, which stipulates that each parameter must be set in advance before the request is sent. For example, when the user clicks the submit button on the form, the content filled in the form will be sent to the server as part of the request.
The type of method requested
There are currently two ways to submit a request to the server: GET and POST. These two methods determine how the data is delivered to the server.
The value to be submitted in GET mode is sent to the server as part of URL, and the submitted value will become the environment variable QUERY_STRING.
The value submitted in POST is sent to the server as an input stream, and the length of the stream is placed in the CONTENT_LENGTH.
The POST method is more secure than the two methods, and a variety of data can be transferred through the POST method.
An example of submitting information using GET
The following is an example of a HTML that submits a form in 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, and when the user enters a student number such as 123333, the data from the form will be sent to the CGI program as part of URL when the Retrieve Marks button is clicked. The address for submission is http://www.somesite.com/cgi-bin / gergrade.cgi?idnum=123333. The value entered when the data is submitted in POST is sent to the server as a segmented input stream.
In GET mode, these spaces are replaced by (+) when there are spaces in the user's input, and these values are separated by (&) when the user wants to submit multiple values at a time.
Servlet programming principle
Servlet is similar to CGI in that Servlets supports programming of requests and responses. When a client sends a request to the server, the server sends the request to Servlet. Servlet organizes a response to be sent back to the client. Servlet differs from CGI in that Servlet uses a single process for multiple requests.
When the client submits a request, the service method of Servlet is called and the request is passed to request and response. First, Servlet determines whether the request is post or get, and decides whether to use the HttpServlet.doGet or HttpServlet.doPost method to process the request. Both methods will call HttpServletRequest and HttpServletResponse.
Activate the CGI script through MIDlet
Now that you know the basics of HTTP GET,POST and Servlets, let's look at an example. * examples are used to illustrate how to use MIDlet to activate a CGI script through POST.
In the example, when the connection to pgrade.cgi CGI is opened, the output and input streams are opened. The input is sent through the output stream. The resulting response is obtained through the input stream. The CGI script is written in PERL. After getting the student number in the script, look for the record of the student number in the database, and if found, return the relevant information to the requesting client. Because there is no form available for submission in MIDlet, the content is implemented by writing the stream. Here is the code for MIDlet.
Import java.io.*; import javax.microedition.io.*; import javax.microedition.lcdui.*; import javax.microedition.midlet.*; / * An example MIDlet to invoke a CGI script * using the POST method. * / public class PostMidlet extends MIDlet {private Display display; String url = "http://somesite.com/cgi-bin/pgrade.cgi"; public PostMidlet () {display = Display.getDisplay (this);} / / Initialization. Invoked the MIDlet activates. Public void startApp () {try {getGrade (url);} catch (IOException e) {System.out.println ("IOException" + e); e.printStackTrace ();}} / / Pause, discontinue.... Public void pauseApp () {} / / Destroy must cleanup everything. Public void destroyApp (boolean unconditional) {} / / Retrieve a grade. Void getGrade (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", "20 Jan 2001 16:19:14 GMT"); c.setRequestProperty ("User-Agent", "Profile/MIDP-1.0 Configuration/CLDC-1.0") C.setRequestProperty ("Content-Language", "en-CA"); os = c.openOutputStream (); / / send request to the CGI script String str = "name=163748"; byte postmsg [] = str.getBytes (); for (int item0; < postmsg.length;i++) {os.write (postmsg [I]);} os.flush (); / / receive response and display in a text box. Is = c.openDataInputStream (); int ch; while ((ch = is.read ())! =-1) {b.append ((char) ch); System.out.println ((char) ch);} t = new TextBox ("Final Grades", b.toString (), 1024, 0);} finally {if (isometric = null) {is.close ();} if (os! = null) {os.close () } if (c! = null) {c.close ();}} display.setCurrent (t);}} at this point, I believe you have a deeper understanding of "how to realize the communication between Servlet and CGI". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.