<< Chapter < Page Chapter >> Page >

Most (but not all) HTML elements, such as the body element, require an opening tag and a closing tag.Thus, most tag types come in pairs.

An HTML page is pure text with the various elements specifying how the browser is to interpret and display that text. (The text can also refer to other files such as image files and cause the content of those files to be included in the rendered output produced by the browser.)

The Java code in this servlet simply prints the requisite HTML text to the output stream referred to by out . Recall from Listing 4 that the reference to the output stream was obtained by calling the getWriter method on the HttpServerResponse object. That object takes care of transporting the text back to the client browser.

But wait, there's more

Usually in most modules, when I complete the discussion of the last sample program, that is the end of the module. In this module, however, there are severalother important topics that I want to discuss before ending the module.

The ServletRequest object

One of the incoming objects to the servlet is of type ServletRequest . This is not the name of a class. Instead, this is a type defined by an interface of thesame name. This interface declares more than 30 methods by which the servlet can extract incoming information from the object. This framework provides theservlet's only access to incoming data.

The data provided by the ServletRequest o object includes parameter names and values, attributes, and an input stream.

Interfaces that extend ServletRequest can provide additional protocol-specific data. For example, HTTP data is provided by the interface HttpServletRequest , which extends ServletRequest . (Recall from Listing 3 t that the doGet method receives a reference to an HttpServletRequest object as an incoming parameter.)

Suffice it to say that it is possible for the servlet to obtain a great deal of information in order to carry out its duties. (I will refer you to the online documentation for more information about the available methods.)

The ServletResponse object

The other incoming object to the servlet is an object of type ServletResponse . This is also a type defined by an interface of the same name. This interfacedeclares more than 15 methods by which the servlet can return data to the client.

The content type

Recall that Listing 4 calls the setContentType m method on the response object to set the type of the data being returned by this servlet to "text/html". It is also possible to setthe character encoding in a call to setContentType with something like

text/html;charset=UTF-8

The rules for doing that are fairly complicated, so you would do well to consult the Java documentation for the setContentType method before trying to deal with that possibility.

Persistence

We have discussed the classes and interfaces that make up a basic Servlet. HTTP servlets have additional objects that provide session-tracking capabilities. The servlet writer can use these APIs to maintain state between the servlet and the client that persists across multiple connections during some time period.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask