Web Service working mechanism based on Jave (6)

Source: Internet
Author: User
Tags return string
The Web|web service Parseuri method obtains the URI from the request line. Listing 1.3 shows the use of the Parseuri method. The first and second empty cells in the Parseuri reduction request obtain the URI.

Listing 1.3. The Request class ' Parseuri method

private string Parseuri (string requeststring) {
int index1, INDEX2;
Index1 = Requeststring.indexof (");

if (index1!=-1) {
Index2 = Requeststring.indexof (', index1 + 1);
if (Index2 > Index1)
Return requeststring.substring (index1 + 1, index2);
}

return null;
}

The response class response represents an HTTP response. Its constructor accepts a OutputStream object, such as the following:

Public Response (OutputStream output) {
This.output = output;
}
The Response object is constructed by the await method of the Httpserver class, which is passed a parameter that is the OutputStream object that is obtained from the socket.

The response class has two public methods: Setrequest and Sendstaticresource. The Setrequest method passes a request object to the response object. The code in Listing 1.4 shows this:

Listing 1.4. The Response class ' Setrequest method

public void Setrequest (Request request) {
This.request = Request;
}
The Sendstaticresource method is used to send a static resource, such as an HTML file. Listing 1.5 gives its implementation process:

Listing 1.5. The Response class ' Sendstaticresource method

public void Sendstaticresource () throws IOException {
byte[] bytes = new Byte[buffer_size];
FileInputStream FIS = null;

try {
File File = new file (Httpserver.web_root, Request.geturi ());
if (file.exists ()) {
FIS = new FileInputStream (file);
int ch = fis.read (bytes, 0, buffer_size);

while (Ch!=-1) {
Output.write (bytes, 0, ch);
ch = fis.read (bytes, 0, buffer_size);
}
}
else {
File not found
String errormessage = "http/1.1 404 File Not found\r\n" +
"Content-type:text/html\r\n" +
"Content-length:23\r\n" +
"\ r \ n" +
"Output.write (Errormessage.getbytes ());
}
}
catch (Exception e) {
Thrown if cannot instantiate a File object
System.out.println (E.tostring ());
}
finally {
if (FIS!= null)
Fis.close ();
}
}



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.