Dynamic web servers A request arrives Parse request parameters Is CGI? Generate content (say read a static file) Spawn CGI child process Read CGI process output Send results to the user Execute an external program External program generates response Web server Child Process Yes No Figure 1.2 Executing a CGI program from within the web server means that the per-request burden on the hosting computer can be quite taxing. A lot of memory and processor overhead is involved in creating, executing, and cleaning up after a new process. With CGI, this overhead is incurred for each and every request to the web server, and naturally affects performance as the number of requests increases. When a busy site meant a few thousand requests per day, the performance challenge associated with CGI was acceptable. Today, busy sites serve thousands of concurrent requests and the degradation in performance associated with CGI cannot be overlooked. The process-per-request policy of CGI hurts performance in other ways as well. These processes often end up performing the same processing (i.e., opening a database connection) over and over again with no way to share or cache results from one request to another. This is hardly acceptable for applications that rely on database access or other time-consuming operations that may need to repeat themselves. There are other disincentives to using CGI, but the two we ve mentioned were probably the most pressing catalysts for the web community s development of new approaches to serving dynamic content. As we ll see in chapter 2, custom tags don t suffer from these drawbacks. But even before custom tags were introduced (or the
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost JSP Web Hosting services
CHAPTER 1 The big picture only trying to find out about custom JSP tags? The answer is two-fold. First, since custom JSP tags function in the same environment (the Web) as these technologies, learning other approaches is helpful for understanding how tags work. Second, discussing the shortcomings of these earlier technologies and how they could be improved helps us understand Sun s reasons for introducing JSP custom tags. We will present the extension methods more or less in the order of their births, starting with CGI followed by Server API, Server pages, and ColdFusion. 1.3.1 Common Gateway Interface CGI was the earliest extension mechanism that web servers had and, even today, it serves as the workhorse of many sites. Figure 1.2 shows how CGI operates. It is a very simple mechanism that spawns background processes in the same manner as a UNIX command-line interpreter (not surprising, as CGI was invented by UNIX folks). In a CGI-served request: 1 A user sends a request to the web server. 2 The web server analyzes the request and determines (based on some part of the request URI) that it should execute an external program to handle it. 3 The web server spawns a child process which executes the external program. 4 The external program reads the parameters sent by the user as well as the request parameters via its command-line arguments, environment variables, and standard input. The program processes these parameters and generates output to be seen by the user. 5 The web server grabs the output from the child process and sends it to the user. 6 The child process dies. Implementing CGI as part of your web server is relatively simple, as is developing external programs that work with your web server. You can code your external programs in the desired language, generate an executable program, and the web server will take its output and send it over to the client. Its ease of use and support for known languages helped CGI become the technology of choice for creating dynamic web sites. CGI, in fact, still powers a sizable number of dynamic sites, though that percentage is declining as newer, faster solutions become available. CGI drawbacks If CGI is so great, why have other extension techniques been introduced? One of the major disappointments with CGI is that it requires a process per request. This
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost JSP Web Hosting services
CHAPTER 1 The big picture Table 1.2 Important HTTP response headers and their roles Header name Role Sample value Content-Type Indicates the media type of the response body (if available). text/html Content-Length Indicates the length of the response body (if available). 10 Set-Cookie Sets a cookie into the browser. A server can set cookies into the browsers and, as a result, the browser saves these cookies and later echoes them back to the server using the (request) Cookie header. This way the server can keep track of the clients visiting it and save per-client data. Part =”Rocket_Launcher”; Path=”/acme” Server Identifies the server version returning the response (i.e., Apache, Netscape, IIS, etc.). Apache/1.3.9 (UNIX) WWW-Authenticate Specifies to the browser how to authenticate its user to the server. Basic realm=”MyWorld” Location Instructs the browser to redirect itself to the location indicated by the header value. http://some.other.host/ index.html HTTP request, the response usually has some body text. Responses to the HEAD method should not include any content. NOTE Once the browser receives the response from the server, the TCP/IP connection between the browser and the server can be closed. A user may connect from the same browser to the same server and have the request served each time on a different TCP/IP socket. This HTTP feature is one of the reasons that HTTP is considered a stateless protocol. A sample HTTP session Let s take a look at a hypothetical request-response pair. Assume that a user directs the browser to http://www.name.com:8080/some/file.html. What will happen, and what will the request and response look like? In our example we will be using Netscape Navigator 4.7 (HTTP/1.1) to submit the request to an Apache web server. First we open a TCP/IP connection from the browser to the server. The browser will analyze the URL entered by the user and see that the user is asking
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Java Web Hosting services
Dynamic web servers for information located on the host www.name.com and on port 8080. The browser subsequently opens a TCP/IP connection (socket) to this host and port. The next step is to send an HTTP request to the server, which may look some thing like: GET /some/file.html HTTP/1.1 Host: www.name.com:8080 User-Agent: Mozilla/4.7 [en] (WinNT; I) Accept: text/*, text/html Note that the request URI was extracted from the URL specified by the user. The server will return the requested content to the browser. The response sent by the server should look something like the following (assuming that the response is OK and that the server returns 100 bytes of type text/html): HTTP/1.1 200 OK Server: Apache/1.3.9 (UNIX) Content-Type: text/html Content-Length: 100 And now some 100 bytes of text/html Now both server and browser can close their sockets and we have finished serving this request. Although we presented only a small portion2 of HTTP in this section, it was enough to serve a file. As demonstrated in the sample, all we need to do to serve an HTML file is parse incoming requests that follow the HTTP protocol, read the file, and return its content to the browser using HTTP. In general, the core web server only knows how to use HTTP to return static content. Serving static content was fine in the old days when all you wanted from the Web was to read information, but now that ecommerce is a mantra, many sites cannot get along with static content alone. If your web server only knows how to serve static files, how will you save form data in a database or search a catalogue for a specific product? You just can t. To solve this problem, all web servers come with an extension mechanism. The next section explores the most common of these. 1.3 Dynamic web servers There are many methods for executing code on a web server, each with its own merits. Why should you concern yourself with these other mechanisms if you re 2 HTTP/1.1 is more complex than the simplified protocol we have just presented, and includes complex content negotiation as well as many performance-related options.
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Java Web Hosting services
HTTP review Table 1.1 Important HTTP request headers and their roles (continued) Header name Role Sample value Host Specifies the Internet host and port number of the resource being requested, as obtained from the original URI given by the user. This header is extremely important for virtual hosting. www.site.com After sending the headers, the browser may send content (also known as request body). The content is an arbitrary set of bytes as defined by the request headers. Note that in most cases the browser does not send any content to the server in this way. The HTTP response The HTTP response has a status line (similar to the request line) and response headers, as well as an optional response body. Nonetheless, there are differences in the headers used and in the status line. The response starts with a status line that informs the browser of (1) the HTTP version used to send this response and (2) the status for this service request. The syntax for the response line is http-version status-code reason-phrase and, as is typical in HTTP, the line is terminated with the carriage return-line feed sequence. The httpversion in the status line is the same as in the request line; the other two portions of the status line are new. The status code is a number whose value can be one of a set of codes as defined in the HTTP specification. For example, the HTTP specification defines the value 200 as representing a successful service. Following the response code, the server can send an optional reason phrase for the code: 200 will usually mean OK, but 400 can indicate Bad Request. Exact reason phrase values are not defined in the HTTP specification, and servers can append their own values. After returning the status line, the server can add response headers. The response headers syntax is identical to that used by the request headers, yet the actual headers used through the response may differ from those used in the request. For example, the User-Agent header does not have a place in the response, but there is a Server header that the server can use to identify its version. Table 1.2 lists important response headers and, as you can see, it contains a few that can only be part of the response. The server can then position the response body after the response headers. This body is the content the browser will show to the user. Be aware that, unlike the
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Tomcat Web Hosting services
CHAPTER 1 The big picture HEAD Similar to GET, but the server only returns the response line and response headers. By using this information, the browser maintains a cache and reloads files only when needed. Following the HTTP request method, the browser should specify a request URI, which references the resource serviced by the server. In many cases1 the request URI starts with a / and references a static file resource located relative to the web server s root directory, but the request URI can reference more than just static HTML files. It can also reference Java servlets, CGI scripts, and other dynamic entities, as we will soon see. The versions of the HTTP protocol used by the client come after the request URI. The current supported versions of the protocol are HTTP/1.0 and HTTP/1.1, and thus the server expects to see one of these in the request line. After sending the request line, the browser may send a few headers which provide information about the request, its content, the browser which sent the request, and so forth. The headers appear in consecutive lines of the form header- name: header-value. Each header line is terminated with the carriage return-line feed characters, and the entire set of request headers is terminated with a line containing only carriage return-line feed. Some important request headers are presented in table 1.1. Table 1.1 Important HTTP request headers and their roles Header name Role Sample value User-Agent Informs the server of the type of browser that sent the request (i.e., Navigator, Explorer, etc.). Mozilla/4.7 [en] (WinNT; I) Content-Type Indicates the media type of the request body (if available). text/html Content-Length Indicates the length of the request body (if available). 10 Authorization Contains the values of user credentials (if sent by the user). Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ== Cookie Echoes a cookie from the browser to the server. Name=value Accept Specifies certain media types which are acceptable for the response. text/*, text/html 1 When the browser connects to the server through a proxy, the request URI received by the proxy does not start with a / , but we will not be discussing proxies in this book.
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Tomcat Web Hosting services
HTTP review business logic from presentation logic. We ll discuss this separation in detail in section 1.3.4. We present a discussion of the benefits of using JSP custom tags in chapter 15. 1.2 HTTP review We begin this chapter with a brief discussion of Internet fundamentals and basic web development that provides a grounding for exploring the complexities of JSP custom tag development. The Web is a client/server application on a huge scale. The client (a browser) connects to the server (also known as a web server or an HTTPserver) using a protocol called HyperText Transfer Proto- Get col (HTTP). The server then returns content to the Browser Reply browser which presents this content (for example, as a GIF image or an HTML page) to the user. Each client/server connection is initiated by the Figure 1.1 An HTTP client and Web Server browser and the browser alone. This procedure server begins by opening a TCP/IP connection to the server and sending an HTTP request. The server then processes the incoming request and returns an HTTP response. These requests and responses follow a very specific, yet simple, syntax as specified by the HTTP protocol. 1.2.1 HTTP protocol Since HTTP is a pull technology, meaning that a connection starts when a client requests a document, we start our discussion with the request. The HTTP request An HTTP request begins with a request line whose structure is http-method request uri http-version, and is terminated with the carriage return-line feed characters. The http-method portion of the request line should be one of the methods defined in the HTTP protocol specification: GET This asks the server to serve a resource as referenced in the request-uri. Request parameters should be coded in the request-uri. This is the method your web browser uses when you type in a URL for it to retrieve. POST Similar to GET, except that POST contains a body wherein the request parameters are encoded. A web browser most often uses this method to submit HTML forms, such as those you fill out when making an online purchase.
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost JSP Web Hosting services
CHAPTER 1 The big picture 1.1 The JSP custom tags solution Building data-driven, dynamic web sites is a problem as old as the Internet. Developers have progressed from Common Gateway Interface (CGI), server-side Java- Script, and web server plug-ins to Java servlets to build sites. As with any technology, something newer and greater, bigger and better is always just around the corner. Today s newer and greater, bigger and better technology is JavaServer Pages (JSP) custom tags. Although custom tags (and the servlet technology from which custom tags are derived) are much easier to develop and learn than some of their predecessors, they still require a solid understanding of the environment in which they run, namely, the Internet. Since JSP custom tags represent a way to serve dynamic content in a web site, you ll need a strong working knowledge of basic web concepts before you begin. Before exploring JSP custom tags, familiarity with the Web and developing dynamic web sites is strongly recommended. If you are thoroughly versed in this, you may skip to the next chapter where we discuss the basics of servlets and JSPs. If you are new to web development we suggest you read this chapter to obtain an overview of fundamental topics that will prove useful later in this book: Basic Internet programming concepts such as HTTP Existing techniques to extend a web server to serve dynamic content How tag-based techniques like custom JSP tags work. This chapter is not meant to replace a book dedicated to any of these topics. It will, however, explain the fundamentals of Internet development and discuss existing web development platforms that explain the basis for using JSP custom tags and the environment in which they function. We finish this chapter with a brief discussion of alternative web clients, such as WAP phones/devices, and we cover the growing trend to extend web development to nontraditional devices, such as phones and pagers, and how this has created an even greater demand for custom tags. Before learning JSP custom tags, you may be asking yourself Why should I? There is, after all, no shortage of technologies available to anyone who wishes to build a dynamic web application. The question is best answered by reading through this chapter and learning about web development techniques, their shortcomings, and how JSP custom tags compare to these techniques. You ll learn how JSP custom tags offer a way to create flexible web solutions that fit several bills: they are built on industry standards, they enjoy Java s cross-platform feature, and they solve one of the most troubling problems of web development cleanly separating
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost JSP Web Hosting services
The language of tags C hapters 1 through 5 set the stage for tag development with an introduction to JSP tags, the world in which they exist, and a look at the rules by which they are developed and used. In this section, you ll learn what tags are, why they are needed, and the basic ground rules for building custom JSP tags. This introduction prepares you for part II, which will teach you to enhance your skills by learning common tag building techniques.
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Tomcat Web Hosting services
1 The big picture In this chapter The web environment Dynamic web servers Tag-based programming Web clients and WAP 3
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Tomcat Web Hosting services