Wednesday, 26 November 2008

Core JSP by Damon Hougland and Aaron Tavistock - Part3

Couple of Days back i started reading Core JSP by Damon Hougland and Aaron Tavistock.

Nice book written by Damon Hougland and Aaron Tavistock.

Few important quotations i wanted to share from next two chapters ( 5-6).

1) An HTTP request is a simple data structure. It is composed of a block of text that is separated by newline characters. The first line is the request itself. The request line is made up of three sets of data. The first section is the HTTP request type, of which two common types are GET and POST. Next is part of the URL of the document requested, and finally the version of HTTP. An example request line might be:

GET /index.html HTTP/1.0

2) Using the Response Object

A client requests a page by building an HTTP request and the results are sent back in an HTTP response. The HTTP response can contain the data returned from the request or an error indicating that the request was not successful. Typically a successful request will include the data and information describing the data in the HTTP response.

Normally the HTTP response is managed entirely by the server. All information passed through the server gets treated the same; a standard response is built, and then sent back to the client.

In order to send data that is not managed entirely by the server requires manipulating the HTTP response. Carefully controlling the HTTP response is the most important aspect of sending complex information. JSP provides an interface into manipulating the response through the HttpServletResponse object, which is exposed to the JSP author through the implicit response object.

3) Content-type is very important header that defines the MIME type of the document being sent. A method has been made to allow easier access to changing the MIME type of the document:

4) Setting Cookies

Quite often a server will send cookies to a client in order to store little bits of information. This information is managed by the client and may be requested by the server at a later time.

5) Building the Cookie

JSP represents cookies with a Cookie object. This object is created with the constructor:

public Cookie (String name, String Value)

A cookie is normally composed of several attributes. The most important data is the name/value pair, which is the data the server would retrieve from the cookie. But the cookie also contains information on when to expire the cookie and when to give out the cookie.

The name/value pair that is stored in the cookie is established when the cookie is first instantiated using new Cookie (String name, String Value). Other attributes default to standard values determined by the server, but they can be changed using methods of the cookie object.

void Cookie.setMaxAge (int expiry)

By using the setMaxAge method the JSP author can set the lifespan of a cookie. By default, the maximum age is set to a negative value. This makes the cookie "temporary"; the cookie will be destroyed when the browser exits.Cookies can also be made psuedo-permanent by setting a positive integer value. A cookie has a lifespan that is measured in seconds since it was received. These kind of cookies are discarded by the client when they get too old.Cookies can also be destroyed by setting the setMaxAge method. By setting the value to zero, the browser will destroy the cookie immediately.

6) In reality, cookies are just a special kind of HTTP header. This special header contains a comma-separated list of values that describes a name/value pair, an expiration date, and some control information.

The response object also has some methods, which do not deal with just the HTTP status or the HTTP headers. Some of these methods involve merging the status and header information, and others are convenience methods that have no direct relation to the HTTP response.


About the Authors

DAMON HOUGLAND is a Program Manager and Technical Architect for eFORCE, Inc., where he designs and develops Java enterprise applications. Previously Damon led the Web Application Infrastructure team at Lawrence Berkeley National Laboratory, which specialized in reporting scientific data through web-enabled databases and applications. He has worked as a software developer and technical manager over the last nine years.


AARON TAVISTOCK has over 10 years of experience with UNIX systems and application development. He has used his extensive knowledge of Java, JSP, and taglibs to create a leading edge web application compiler. Aaron is currently the Web Compiler Architect for Snapshop.com, Inc., a provider of B2B2C (business-to-business-to-consumer) web applications.

No comments: