Java Tutorial: Sudip Maity
Java Tutorial: Sudip Maity
Java Tutorial: Sudip Maity
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.
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.
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
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.
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.
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.
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 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.
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.
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.
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
history. history.