Java EE learning path-writing Service Methods

Source: Internet
Author: User


The service provided by a servlet was implemented in theServiceMethod of aGenericservlet, in thedoMethod Methods (whereMethodcan take the valueGet,Delete,Options,Post,Put, orTrace) of anHttpServletobject, or in any and protocol-specific methods defined by a class that implements theServletInterface. The termService Methodis used for any method in a Servlet class this provides a service to a client.

The general pattern for a service method was to extract information from the request, access external resources, and then P Opulate the response, based on that information. For HTTP Servlets, the correct procedure for populating the response are 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 is set before the Response has been committed. The web container would ignore any attempt to set or add headers after the response have been committed. The next 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 Implement the ServletRequest interface. This interface defines methods for accessing the following information:

    • Parameters, which is typically used to convey information between clients and Servlets

    • object-valued attributes, which is 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 a input stream from the request and manually parse the data. To read character data with the BufferedReader object returned by the request ' s getreader method. To read binary data with the ServletInputStream returned by getInputStream.

HTTP Servlets is 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, the corresponds to The component alias that is activated this request. This path starts with a forward slash (/).

    • Path Info : The part of the request Path which is not PA RT of the context path or the servlet path.

you can use THE getcontextpath ,  getservletpath , and  GetPathInfo  methods of the  HttpServletRequest  interface to access this information. Except for URL encoding differences between the request URI and the path parts, the request URI was always comprised of the Context path plus the servlet path plus the path info.

Query strings is composed of a set of parameters and values. Individual parameters is retrieved from a request by using the getparameter method. There is ways to generate query strings.

    • A query string can explicitly appear in a Web page.

    • A query string is the appended to a URL when a form with a GET HTTP method is submitted.

Constructing responses

A response Contains data passed between a server and the client. All responses implement THE servletresponse   Interface. This interface defines methods the allow you to

  • Retrieve an output stream to use for send data to the client. To send character data, use THE printwriter   Returned by the response ' s getwriter  method. To send binary data in a multipurpose Internet Mail Extensions (MIME) body response, use THE servletoutputstream  returned by s Ervletoutputstream  and Manage the character sections manually.

  • Indicate the content type (for EXAMPLE, text/html ) being returned by the response with THE 

  • Indicate whether to buffer output with the setbuffersize (int) method. By default, any content written to the output stream was immediately sent to the client. Buffering allows content to be written before anything are sent back to the client, thus providing the servlet with more Ti Me to set appropriate status codes and headers or forward to another Web resource. The method must be called before any content are written or before the response is committed.

  • Set localization information, such as locale and character encoding. See Chapter, internationalizing and localizing Web applications for details.

HTTP response objects, javax.servlet.http.HttpServletResponse, with fields representing HTTP headers, such as th E following:

    • Status codes, which was used to indicate the reason a request was not satisfied or that a request had been redirected.

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

Java EE learning path-writing Service Methods

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.