Nothing Special   »   [go: up one dir, main page]

Java Tutorial: Sudip Maity

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Java Tutorial [MODULE5]

SUBMODULE 5.3: Web Application

5.3.1 Web Application:

a) Web application: A web application is built of web components that perform specific
tasks and are able to expose their services over the Web. Example of web application would be
e-commerce web site, web banking, stock exchange on the web, web games and many others.
A web application is built of web components that perform specific tasks and are able to expose
their services over the Web. For example, the HelloWorldServlet is a web component.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorldServlet extends HttpServlet{
public void service(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
PrintWriter pw = response.getWriter();
pw.println("<html>");
pw.println("<head>");
pw.println("</head>");
pw.println("<body>");
pw.println("<h3>Hello World!</h3>");
pw.println("</body>");
pw.println("</html>");
}
}
Since it is complete in itself, it is also a web application. In real life, a web application consists of
multiple servlets, JSP pages, HTML files, image files, and so forth.

b) Web resource:
i) Active resource: Active objects have their own processing capabilities. Active
resources in a web application provide dynamic content to users and enable them to execute
business logic via their browsers.
For example, when a browser sends a request for www.myserver.com/reportServlet, the web
server at myserver.com forwards the request to reportServlet, an active resource.
The servlet generates the HTML text on the fly and gives it to the web server. The web server,
in turn, forwards it to the browser.
ii) Passive resource: A resource is passive when it does not have any processing of its
own. A passive resource is also called a static resource, since its contents do not change with
requests.

SUDIP MAITY mail: amiable_sudip@rediff.com| 2009 1


Java Tutorial [MODULE5]

For example, when a browser sends a request for www.myserver.com/myfile.html, the


web server at myserver.com looks for the myfile.html file, a passive resource, and returns it to
the browser.

c) Web server and container, uri(Unified Resource Identifier), url(Unified Resource


Locator):

Web server and container: A Web server is a program that, using the client/server model and
the World Wide Web's Hypertext Transfer Protocol (HTTP), serves the files that form Web
pages to Web users. Every computer on the Internet that contains a Web site must have a Web
server program. Two leading Web servers are Apache, the most widely-installed Web server,
and Microsoft's Internet Information Server (IIS). Every server that provides services to remote
clients has two main responsibilities. The first is to handle client requests; the second is to
create a response to be sent back. The first task involves programming at the socket level,
extracting information from request messages, and implementing client-server protocols, such
as FTP and HTTP. The second task, creating the response, varies from service to service. For
example, in the case of FTP servers that serve file transfer requests, response creation is as
simple as locating a file on the local machine. On the other hand, HTTP servers that host full-
fledged web applications are required to be more sophisticated in the way they generate output.
They have to create the response dynamically, which may involve complicated tasks, such as
retrieving data from the database, applying business rules, and presenting the output in the
formats desired by different clients.
The specialized module in the web server which is dedicated to servlet management is
called servlet container. The servlet container is a compiled, executable program. The container
is the intermediary between the Web server and the servlets in the container. The container
loads, initializes, and executes the servlets. When a request arrives, the container maps the
request to a servlet, and then passes the request to the servlet. The servlet processes the
request and produces a response. The container translates the response into the network
format, then sends the response back to the Web server.

Unified Resource Identifier: A URI is a string that identifies any resource such as HTML files,
image files, servlets etc. Identifying the resource may not necessarily mean that we can retrieve
it. URI is a superset of Unified Resource Locator(URL) and Uniform Resource Name (URN).
For example, files/sales/report.html is a URI, because it identifies some resource.

Unified Resource Locator: URIs that specify common Internet protocols such as HTTP, FTP
etc are also called URLs. URL is an informal term and is not used in technical specifications.
For example, http://www.manning.com/files/sales/report.html is a URL because it also specifies
how to retrieve the resource.

SUDIP MAITY mail: amiable_sudip@rediff.com| 2009 2


Java Tutorial [MODULE5]

d) Http: Hypertext Transfer Protocol is a request-response–based stateless protocol. A client


sends an HTTP request for a resource and the server returns an HTTP response with the
desired resource. A client opens a connection to the server and sends an HTTP request
message. The client receives an HTTP response message sent by the server and closes the
connection. It is stateless because once the server sends the response it forgets about the
client. In other words, the response to a request does not depend on any previous requests that
the client might have made. From the server’s point of view, any request is the first request from
the client.
In the case of the Internet, the web browser is an HTTP client, the web server is an
HTTP server, and the resources are HTML files, image files, servlets, and so forth. Each
resource is identified by a unique Uniform Resource Identifier (URI). You will frequently hear
three terms used interchangeably: URI, URL, and URN

5.3.2: Structure of Http message: request and response.

Structure of an Http message:


An HTTP message is any request from a client to a server, or any response from a server to a
client.

The parts of an HTTP message:

Message part Description

The initial line  specifies the purpose of the request or response message
The header section  specifies the meta-information, such as size, type, and encoding,
about the content of the message
A blank line 
An optional message body  the main content of the request or response message

The structure of an HTTP request:

An HTTP message sent by a client to a server is called an HTTP request.


The initial line for an HTTP request has three parts, separated by spaces:
• A method name
• The local path of the requested resource (URI)
• The version of HTTP being used

A GET request as generated by clicking on a hyperlink:


A typical request line is GET /selectBeerTaste.jsp?color=dark&taste=malty HTTP/1.1

SUDIP MAITY mail: amiable_sudip@rediff.com| 2009 3


Java Tutorial [MODULE5]

Here, GET is the method name, /selectBeerTaste.jsp is the resource URI, and HTTP/1.1 is the
HTTP version of the request.
The path to the resource, and any parameters added to the URL are all included at “request
line”.

The rest of the message consists of a set of name/value pairs, known as headers.

A POST request as generated by a form submission:

A typical request line is POST /advisor/selectBeerTaste.do HTTP/1.1


Here POST is the method name, / advisor/selectBeerTaste.do is the resource URI, and
HTTP/1.1 is the HTTP version of the request.

The rest of the message consists of a set of name/value pairs, are headers.
In POST, the parameters are sent in the message body.

SUDIP MAITY mail: amiable_sudip@rediff.com| 2009 4


Java Tutorial [MODULE5]

The structure of an HTTP response:

An HTTP message sent by a server to a client is called an HTTP response. The initial line of an
HTTP response is called the status line. It has three parts, separated by spaces:
 The HTTP version
 A response status code that tells the result of the request
 An English phrase describing the status code.
HTTP defines many status codes; common ones that you may have noticed are 404 and 500.
Here are two examples of a status line that could be sent in the response:
HTTP/1.1 404 Not Found
HTTP/1.1 500 Internal Error
When the browser receives a status code that implies a problem, it displays an appropriate
message to the user. If some data is associated with the response, headers like Content-Type
and Content-Length that describe the data may also be present.

A typical HTTP response looks like this:

SUDIP MAITY mail: amiable_sudip@rediff.com| 2009 5


Java Tutorial [MODULE5]

5.3.3: Http methods: The HTTP protocol supports various types of request method, such as
GET, POST, PUT, HEAD, DELETE, TRACE, and OPTIONS that are used by clients to request
data.

The GET method

The GET method is used to retrieve web pages from the server. It is a part
of the request line from the client and tells the server to pass a copy of the
document back to the browser or run a CGI program.

GET is also used to send user information from an online form on a web
page to the server. Through this, the data is sent as a part of the URL in
'name-value' pairs.

The Post Method

Data from the POST method is sent by the client as a part of the request
body. The server receives this data and sends it to a CGI program along with
other variables. The METHOD attribute inside the <FORM> tag will carry
POST as its value. The big advantage in using POST over GET is that the
data is not open to prying eyes. Also, via GET, you can transfer only a
limited amount of data; POST exerts no such limitations.

The Head METHOD

SUDIP MAITY mail: amiable_sudip@rediff.com| 2009 6


Java Tutorial [MODULE5]

This method is used to find information about a document located on the


server not retrieve it. It is very similar to the GET method except no data is
returned by the server.

DELETE

The DELETE method requests that the origin server delete the resource
identified by the Request-URI. This method MAY be overridden by human
intervention (or other means) on the origin server. The client cannot be
guaranteed that the operation has been carried out, even if the status code
returned from the origin server indicates that the action has been completed
successfully. However, the server SHOULD NOT indicate success unless, at
the time the response is given, it intends to delete the resource or move it to
an inaccessible location.

5.3.4: Difference between get and post/post and put.

Difference between GET and POST request:


GET POST

Target resource type active or passive. Target resource type active.

Sends only text data. Sends text as well as binary data.

Data is part of the URL and is visible to the Data is not a part of the URL and is sent as
user in the URL field of the browser. the request message body. It is not visible to
the user in the URL field of the browser.

Data can be cached in the browser’s URL Data is not cached in the browser’s URL

SUDIP MAITY mail: amiable_sudip@rediff.com| 2009 7


Java Tutorial [MODULE5]

history. history.

Difference between post and put:


POST means we are sending some data to a resource for processing. On the other hand, a
PUT request means we are sending some data that we want to be associated with a URI.

SUDIP MAITY mail: amiable_sudip@rediff.com| 2009 8

You might also like