Chapter 10 Java Servlets 227 10. Java Servelet Introduction Java TM Servlet provides Web developers with a simple, consistent mechanism for extending the functionality of a Web server and for accessing existing business systems. Servlets are the Java platform technology of choice for extending and enhancing Web servers. Servlets provide a component-based, platformindependent method for building Web-based applications, without the performance limitations of CGI programs. Servlets have access to the entire family of Java APIs, including the JDBC API to access enterprise databases. Servlets can also access a library of HTTP-specific calls and receive all the benefits of the mature Java language, including portability, performance, reusability, and crash protection. Servlets are a popular choice for building interactive Web applications. Third-party servlet containers are available for Apache Web Server, iplanet Web Server, Microsoft IIS, and others. Servlet containers can also be integrated with Web-enabled application servers, such as BEA WebLogic Application Server, IBM WebSphere, iplanet Application Server, etc. Java Server Page (JSP) technology is an extension of the servlet technology created to support authoring of HTML and XML pages. It makes it easier to combine fixed or static template data with dynamic content. Even if you're comfortable writing servlets, there are several compelling reasons to investigate JSP technology as a complement to your existing work.
Chapter 10 Java Servlets 228 Client Computer/Web Browser HTTP Internet HTML Files HTTP HTTP Server CGI Program Server Computer Figure 1. CGI and HTTP Server Types of Servlets: Internal Servlets Admin Servlet CGI Servlet File Servlet Imagemap Servlet Invoker Servlet: invoke user Servlet Server-Side Include Servlet
Chapter 10 Java Servlets 229 Web Service Request HTTP Web Server Computer Web Browser Response Servlets JDBC SQL Database Java Web Server Using Servlets Basic Flow within the Servlet 1. Load Servlet 2. Initialize the Servlet: init() 3. HTML POST request: depost() 4. The Servlet perform some processing and return the response via an output stream Writing Servlets 1. Create a new Servlet class that extends javax.servlet.http.httpservlet to give javax.servlet.http.httpservletrreuest class 2. Override one or both doget() and dopost() methods How Does Servlet benefits Over CGI? As we know CGI program is not secure enough to send credit card information. Servlets are effective for developing Web-based solutions that help provide secure access to a Web site, that interact with database on
Chapter 10 Java Servlets 230 behalf of a client and maintain unique session information for each client. How does HTML page interact with Servlets? The request-response model of communication is the foundation for Java servlets. Request-response model: when client requests that some action be performed and the server performs the action and responds to the client. The servlets communicate between clients and the servers via the HTTP protocol of the World Wide Web. A client sends an HTTP request to the server. The server receives the request and directs it to be processed by appropriate servlets. The servlets do their processing (which often includes interacting with a database). Then return their results to the --in the form of HTML documents to display in a browser. How Does HTML page connect to Servlet? By using GET and POST Methods, the two most common HTTP request methods that can send data to the server. Get Request Method The primary purpose of an HTTP Get request is to retrieve the content of a specified URL normally the content is an HTML document (e.g. a Web page or an image). Post Request Method Often used to post data from an HTML form to a server-side form handler that processes the data.
Chapter 10 Java Servlets 231 On the data base query applications, the servlet stores the result of the update form (may be student base: student name and the social security number in a file on the server). When user responds to the update form, the servlet HTTPPostServlet sends an HTML document to the client summarizing the results of the form to this point. When user enters the information (student name and the social security number) and presses Submit, the browser sends an HTTP POST request to the servlet. The servlet reading the previous update results from a file on the server Updating the results, Writing the update results back to the file on the server and sending a Web page to the client indicating the cumulative results of the update form. Common Gateway Interface The Common Gateway Interface (CGI) is a standard for interfacing Web applications with information servers such as HTTP or Web servers in a platformindependent manner. Some common Web-based client/server tasks that performed by CGI scripts (programs) include Query database Perform calculations Solicit and interpret user-supplied data Retrieve requested information Produced customized content A CGI program is an executable program that resided in a special directory such as /cgi-bin. It can be written in any language: C/C++, Fortran, Perl, TCL, any UNIX shell, Visual Basic, and AppleScript. The CGI has been in use by the World-Wide Web since 1993. The CGI specification can be found at the following Web sites:
Chapter 10 Java Servlets 232 CGI/1.1 Draft Specification ftp://ftp.ietf.org/internet-drafts/draft-coar-cgi-v11-0x.txt The CGI RFC Project Home Page www.golux.com/coar/cgi/ The Unofficial FastCGI Home Page www.fastcgi.com mod_perl: The Apache/Perl Integration Project perl.apache.org Apache:Session www.perl.com/cpan/modules/by-module/apache/ Velocigen (Binary Evolution) www.velocigen.com Features of CGI Scripting Dynamic Web applications Small programs that communicate with Web servers, which reply the requests from Web browsers Add additional capabilities to the HTTP server Work as a gateway to handle complex tasks for the HTTP server Generate documents on the fly that are capable of incorporating information which changes or which cannot be determined in advance CGI scripts are distinguished from HTML files through o The most common extension for scripts is a suffix of.cgi with the scripts being placed in a separate directory (/cgi-bin) o Windows servers use the extension.exe or.pl Only a HTTP server can interact with a script. A client program cannot directly run a CGI script. It will likely to display the script's source code HTTP Transaction of a Web Browser/Server 1. Web client specify an URL for obtaining a file (for example: www.microsoft.com/docs/index.html)
Chapter 10 Java Servlets 233 2. The Web browser interprets the URL and sends a "GET" request of the HTTP protocol to the server located at www.microsoft.com, asks for the desired file docs/index.html. 3. The server receives and parse the request using CGI protocol 4. The servers then sends the file, with a header to indicate the content type as "Content -type: text/html", back to the browser 5. The "Content -type: text/html" header tells the browser that the data is HTML, so the browser formats and renders the text appropriately CGI Environment Variables and HttpServletRequest Methods AUTH_TYPE getauthtype() If the server supports user authentication, and the script is protects, this is the protocol-specific authentication method used to validate the user. CONTENT_TYPE getcontenttype() It specifies the media type of the data for queries, which have attached information, such as HTTP POST and PUT, this is the content type of the data. CONTENT_LENGTH getcontentlength() The length (number of bytes) of information passed to the script. GATEWAY_INTERFACE The name and version of the protocol being used by the server to communicate with the script. Format: CGI/revision HTTP_ACCEPT Gives a comma-separated list of MIME types that the client can accept. HTTP_REFERER Provides the URL address of the page where the request originated. HTTP_USER_AGENT Specifies the name of the client program and version used to make the request. PATH_INFO getpathinfo() It provides any extra path information, as given in the URL, for accessing this script. The extra information is sent as PATH_INFO to be decoded by the server before it is passed to the CGI script. PATH_TRANSLATED getpathtranslated() It gives the absolute filesystem path for access the script. The server provides a translated version of PATH_INFO, which takes the path and does any virtual-to-physical mapping to it.
Chapter 10 Java Servlets 234 QUERY_STRING getquerystring() Any additional information passed to the script after the? mark in the URL which referenced this script is called the query information. It should not be decoded in any fashion. REMOTE_HOST getremotehost() It contains a fully qualified domain name of the client computer. If the host name cannot be determined, it should set REMOTE_ADDR to hold the IP address of the host and leave this variable unset. REMOTE_ADDR getremoteaddr() The IP address of the remote client computer making the request. REMOTE_IDENT The client machine's username. Usage of this variable should be limited to logging only. SCRIPT_NAME getservletname() A virtual path to the script being executed, used for self-referencing URLs. REMOTE_USER getremoteuser() The name used to authenticate the user for accessing the script. SERVER_SOFTWARE The name and version of the information server software answering the request (and running the gateway). # Format: name/version SERVER_NAME getservername() The server's hostname, DNS alias, or IP address as it would appear in self-referencing URLs. SERVER_PROTOCOL getprotocol() The name and revision of the information protocol this request came in with. Format: protocol/revision SERVER_PORT getserverport() The port number to which the request was sent. REQUEST_METHOD getmethod() The method with which the request was made. For HTTP, this is "GET", "HEAD", "POST", etc. HTTP Status Codes HTTP Working Group: http://www.w3.org/protocols/http RFC 2616(HTTP 1.1):http://www.cis.ohio-state.edu/htbin/rfc/rfc2616.html 100 Continue 101 Switching Protocols 199 Misc.
Chapter 10 Java Servlets 235 Successful Codes 200 OK, the request was fulfilled 201 Created, following a POST command, indicates the URI by which the newly created document should be known 202 Accepted, the request has been accepted for processing, but the processing has not been completed 203 Non-Authoritative Information 204 No Content 205 Reset Content 206 Partial Content 299 Miscellaneous information Redirection Codes 300 Multiple Choices, multiple documents available 301 Moved Permanently 302 Moved temporarily 303 See other document 304 Not modified since last retrieval 305 Use Proxy 306 Switch proxy 307 Document moved temporarily 399 Misc. Client Errors Code 400 Bad Request 401 Unauthorized 402 Payment Required 403 Forbidden 404 Not Found 405 Method Not Allowed 406 Not Acceptable 407 Proxy Authentication Required 408 Request Timeout 409 Conflict 410 Gone 411 Length Required 412 Precondition Failed 413 Request Entity Too Large 414 Request-URI Too Long 415 Unsupported Media Type 416 Requested range not valid 417 Failed 418 Failed Server Error 500 Internal Server Error 501 Not Implemented 502 Bad Gateway 503 Service Unavailable 504 Gateway Timeout 505 HTTP Version Not Supported 506 Redirection failed