Java EE learning path-Writing Service Methods

Source: Internet
Author: User

Java EE learning path-Writing Service Methods

The service provided by a servlet is implemented inServiceMethod ofGenericServlet, InDoMethodMethods (whereMethodCan take the valueGet,Delete,Options,Post,Put, OrTrace) OfHttpServletObject, or in any other protocol-specific methods defined by a class that implementsServletInterface. The termService methodIs used for any method in a servlet class that provides a service to a client.

The general pattern for a service method is to extract information from the request, access external resources, and then populate the response, based on that information. for HTTP servlets, the correct procedure for populating the response is to do the following:

  1. Retrieve an output stream from the response.

  2. Fill in the response headers.

  3. Write any body content to the output stream.

    Response headers must always be set before the response has been committed. the web container will ignore any attempt to set or add headers after the response has been committed. the next two sections describe how to get information from requests and generate responses.

    Getting Information from Requests

    A request contains data passed between a client and the servlet. All requests implementServletRequestInterface. This interface defines methods for accessing the following information:

    • Parameters, which are typically used to convey information between clients and servlets

    • Object-valued attributes, which are typically used to pass information between the web container and a servlet or between collaborating servlets

    • Information about the protocol used to communicate the request and about the client and server involved in the request

    • Information relevant to localization

      You can also retrieve an input stream from the request and manually parse the data. To read character data, useBufferedReaderObject returned by the request'sGetReaderMethod. To read binary data, useServletInputStreamReturnedGetInputStream.

      HTTP servlets are passed an HTTP request object,HttpServletRequest, Which contains the request URL, HTTP headers, query string, and so on. An HTTP request URL contains the following parts:

      http://[host]:[port][request-path]?[query-string]

      The request path is further composed of the following elements:

      • Context path: A concatenation of a forward slash (/) With the context root of the servlet's web application.

      • Servlet path: The path section that corresponds to the component alias that activated this request. This path starts with a forward slash (/).

      • Path info: The part of the request path that is not part of the context path or the servlet path.

        You can useGetContextPath,GetServletPath, AndGetPathInfoMethods ofHttpServletRequestInterface to access this information. Could t for URL encoding differences between the request URI and the path parts, the request URI is always comprised of the context path plus the servlet path plus the path info.

        Query strings are composed of a set of parameters and values. Individual parameters are retrieved from a request by usingGetParameterMethod. There are two ways to generate query strings.

        • A query string can explain icitly appear in a web page.

        • A query string is appended to a URL when a form withGETHTTP method is submitted.

          Constructing Responses

          A response contains data passed between a server and the client. All responses implementServletResponseInterface. This interface defines methods that allow you

          • Retrieve an output stream to use to send data to the client. To send character data, usePrintWriterReturned by the response'sGetWriterMethod. To send binary data in a Multipurpose Internet Mail Extensions (MIME) body response, useServletOutputStreamReturnedGetOutputStream. To mix binary and text data, as in a multipart response, useServletOutputStreamAnd manage the character sections manually.

          • Indicate the content type (for example,Text/html) Being returned by the response withSetContentType (String)Method. This method must be called before the response is committed. A registry of content type names is kept by the Internet Assigned Numbers Authority (IANA) athttp: // www.iana.org/assignments/media-types /.

          • Indicate whether to buffer output withSetBufferSize (int)Method. by default, any content written to the output stream is immediately sent to the client. buffering allows content to be written before anything is sent back to the client, thus providing the servlet with more time to set appropriate status codes and headers or forward to another web resource. the method must be called before any content is written or before the response is committed.

          • Set localization information, such as locale and character encoding. See Chapter 17, Internationalizing and Localizing Web Applications for details.

            HTTP response objects,Javax. servlet. http. HttpServletResponse, Have fields representing HTTP headers, such as the following:

            • Status codes, which are used to indicate the reason a request is not satisfied or that a request has been redirected.

            • Cookies, which are used to store application-specific information at the client. Sometimes, cookies are used to maintain an identifier for tracking a user's session (see Session Tracking ).

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.