• No results found

10. Java Servelet. Introduction

N/A
N/A
Protected

Academic year: 2021

Share "10. Java Servelet. Introduction"

Copied!
9
0
0

Loading.... (view fulltext now)

Full text

(1)

Introduction

• JavaTM 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, platform-independent 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.

(2)

HTTP Server Internet HTML Files Client Computer/Web Browser HTTP HTTP 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

(3)

Computer Web Browser SQL Database HTTP Web Server Servlets Request Response

Java Web Server Using Servlets

JDBC

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.

(4)

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.

(5)

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 platform-independent 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.

(6)

• 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)

(7)

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

(8)

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.

(9)

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

References

Related documents

Life Cycle of a HTML Page HTTP Request Client Web Server Internet Information Services (IIS) HTTP Response.?. Life Cycle of ASP Page HTTP Request Client Web Server Internet

– The server finds the web page (a HTML document) – Sends a copy back over the Internet to the client.. • Again HTTP is used for communication between the client and

The original web architecture HTTP Get Cgi-bin Query SQL Data Client Browser Server File System... Evolution of the

Figure 2 -1 PROFINET Industrial Ethernet CPU 1214C DC/DC/DC as web server SIMATIC STEP 7 V11 HTML editor - HTML file Client with web browser S7 program -Blocks - … HTML file

HTTP Response: web content (HTML file) Client-side code: HTML, CSS, JavaScript (dynamically generated by

Browser HTTP Web Server File Store Response Corporate RDBMS CGI Interface Security Management State Management HTML Page Generation User Credentials?..

Client Browser Server Web server Database server HTTP request HTTP response Servlet/JSP engine Java Development Kit (JDK)... An introduction to

Request Response Browser Web Server HTTP HTTP HTML HTML CSS CSS JavaScript JavaScript Rails Rails Ruby Ruby.?. Computer Science and Engineering  The Ohio