Assignment of ADVANCED JAVA: Title:-Rmi
Assignment of ADVANCED JAVA: Title:-Rmi
SCHOOL OF INFORMATICS
TITLE :-RMI
1
Servlets
Java Servlets are programs that run on a Web or Application server and act as a middle layer between a
requests coming from a Web browser or other HTTP client and databases or applications on the HTTP
server. Using Servlets, you can collect input from users through web page forms, present records from a
database or another source, and create web pages dynamically. Java Servlets often serve the same
purpose as programs implemented using the Common Gateway Interface (CGI). But Servlets offer
several advantages in comparison with the CGI.
Servlets Architecture
The following diagram shows the position of Servlets in a Web Application.
Servlets Tasks
Servlets perform the following major tasks:
Read the explicit data sent by the clients (browsers). This includes an HTML form on a Web page
or it could also come from an applet or a custom HTTP client program.
Read the implicit HTTP request data sent by the clients (browsers). This includes cookies, media
types and compression schemes the browser understands, and so forth.
Process the data and generate the results. This process may require talking to a database,
executing an RMI or CORBA call, invoking a Web service, or computing the response directly.
Send the explicit data (i.e., the document) to the clients (browsers). This document can be sent
in a variety of formats, including text (HTML or XML), binary (GIF images), Excel, etc.
Send the implicit HTTP response to the clients (browsers). This includes telling the browsers or
other clients what type of document is being returned (e.g., HTML), setting cookies and caching
parameters, and other such tasks.
2
Servlets Packages
Java Servlets are Java classes run by a web server that has an interpreter that supports
the Java Servlet specification. Servlets can be created using the javax.servlet and javax.servlet.http
packages, which are a standard part of the Java's enterprise edition, an expanded version of the Java
class library that supports large-scale development projects.
Java servlets have been created and compiled just like any other Java class. After you install the servlet
packages and add them to your computer's Classpath, you can compile servlets with the JDK's Java
compiler or any other current compiler.
In functionality, servlets lie somewhere between Common Gateway Interface (CGI) programs and
proprietary server extensions such as the Netscape Server API (NSAPI) or Apache Modules.
Servlets have the following advantages over other server extension mechanisms:
They are generally much faster than CGI scripts because a different process model is used.
They use a standard API that is supported by many Web servers.
They have all the advantages of the Java programming language, including ease of development
and platform independence.
They can access the large set of APIs available for the Java platform.
The init method is called only once. It is called only when the servlet is created, and not called for any
user requests afterwards. So, it is used for one-time initializations, just as with the init method of
applets. The servlet is normally created when a user first invokes a URL corresponding to the servlet, but
you can also specify that the servlet be loaded when the server is first started.
When a user invokes a servlet, a single instance of each servlet gets created, with each user request
resulting in a new thread that is handed off to doGet or doPost as appropriate. The init() method simply
creates or loads some data that will be used throughout the life of the servlet.
// Initialization code...
3
The service() Method
The service() method is the main method to perform the actual task. The servlet container (i.e. web
server) calls the service() method to handle requests coming from the client( browsers) and to write the
formatted response back to the client. Each time the server receives a request for a servlet, the server
spawns a new thread and calls service. The service() method checks the HTTP request type (GET, POST,
PUT, DELETE, etc.) and calls doGet, doPost, doPut, doDelete, etc. methods as appropriate.
ServletResponse response)
The service () method is called by the container and service method invokes doGe, doPost, doPut,
doDelete, etc. methods as appropriate. So you have nothing to do with service() method but you
override either doGet() or doPost() depending on what type of request you receive from the client. The
doGet() and doPost() are most frequently used methods with in each service request.
A GET request results from a normal request for a URL or from an HTML form that has no METHOD
specified and it should be handled by doGet() method.
HttpServletResponse response)
// Servlet code
A POST request results from an HTML form that specifically lists POST as the METHOD and it should be
handled by doPost() method.
HttpServletResponse response)
// Servlet code
4
The destroy() Method
The destroy() method is called only once at the end of the life cycle of a servlet. This method gives your
servlet a chance to close database connections, halt background threads, write cookie lists or hit counts
to disk, and perform other such cleanup activities. After the destroy() method is called, the servlet
object is marked for garbage collection.
// Finalization code...
Example
package itso.servjsp.servletapi;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
res.setContentType("text/html");
out.println("<HTML><TITLE>SimpleHttpServlet</TITLE><BODY>");
out.println("</BODY><HTML>");
out.close();
5
Java Server Pages (JSP)
JSP simply puts Java inside HTML pages. You can take any existing HTML page and change its extension
to ".jsp" instead of ".html". Scripting elements are used to provide dynamic pages
JavaServer Pages (JSP) is a technology for developing Webpages that supports dynamic content. This
helps developers insert java code in HTML pages by making use of special JSP tags, most of which start
with <% and end with %>. A JavaServer Pages component is a type of Java servlet that is designed to
fulfill the role of a user interface for a Java web application. Web developers write JSPs as text files that
combine HTML or XHTML code, XML elements, and embedded JSP actions and commands.
Using JSP, you can collect input from users through Webpage forms, present records from a database or
another source, and create Webpages dynamically. JSP tags can be used for a variety of purposes, such
as retrieving information from a database or registering user preferences, accessing JavaBeans
components, passing control between pages, and sharing information between requests, pages etc.
JavaServer Pages often serve the same purpose as programs implemented using the Common Gateway
Interface (CGI). But JSP offers several advantages in comparison with the CGI.
Performance is significantly better because JSP allows embedding Dynamic Elements in HTML
Pages itself instead of having separate CGI files.
JSP are always compiled before they are processed by the server unlike CGI/Perl which requires
the server to load an interpreter and the target script each time the page is requested.
JavaServer Pages are built on top of the Java Servlets API, so like Servlets, JSP also has access to
all the powerful Enterprise Java APIs, including JDBC, JNDI, EJB, JAXP,etc.
JSP pages can be used in combination with servlets that handle the business logic, the model
supported by Java servlet template engines.
Finally, JSP is an integral part of Java EE, a complete platform for enterprise class applications. This
means that JSP can play a part in the simplest applications to the most complex and demanding.
Advantages of JSP
Following is the list of other advantages of using JSP over other technologies:
The advantages of JSP are twofold. First, the dynamic part is written in Java, not Visual Basic or other MS
specific language, so it is more powerful and easier to use. Second, it is portable to other operating
systems and non-Microsoft Web servers.
It is more convenient to write (and to modify!) regular HTML than to have plenty of println statements
that generate the HTML.
6
JSP vs. Server-Side Includes (SSI)
SSI is really only intended for simple inclusions, not for "real" programs that use form data, make
database connections, and the like.
JavaScript can generate HTML dynamically on the client but can hardly interact with the web server to
perform complex tasks like database access and image processing etc.
Each JSP page is turned into a Java servlet, compiled and loaded. This compilation happens on
the first request. After the first request, the file doesn't take long to load anymore.
Every time you change the JSP file, it will be re-compiled again.
You can examine the source code produced by the JSP translation process.
There is a directory called generated in Sun Java J2EE Application Server where you can find the
source code.
Note that the _jspServicemethod corresponds to the servlet service method (which is called by
doGetor doPost)
7
JSP ─ LIFE CYCLE
A JSP life cycle is defined as the process from its creation till the destruction. This is similar to a servlet
life cycle with an additional step which is required to compile a JSP into servlet. The four major phases of
a JSP life cycle are very similar to the Servlet Life Cycle. The four phases have been described below:
JSP Compilation
When a browser asks for a JSP, the JSP engine first checks to see whether it needs to compile the page.
If the page has never been compiled, or if the JSP has been modified since it was last compiled, the JSP
engine compiles the page.
JSP Initialization
When a container loads a JSP it invokes the jspInit() method before servicing any requests. If you need
to perform JSP-specific initialization, override the jspInit() method:
// Initialization code...
Typically, initialization is performed only once and as with the servlet init method, you generally
initialize database connections, open files, and create lookup tables in the jspInit method.
JSP Execution
This phase of the JSP life cycle represents all interactions with requests until the JSP is destroyed.
Whenever a browser requests a JSP and the page has been loaded and initialized, the JSP engine invokes
the _jspService() method in the JSP. The _jspService() method takes an HttpServletRequest and an
HttpServletResponse as
HttpServletResponse response)
The _jspService() method of a JSP is invoked on request basis. This is responsible for generating the
response for that request and this method is also responsible for generating responses to all seven of
the HTTP methods, i.e., GET, POST, DELETE, etc.
8
JSP Cleanup
The destruction phase of the JSP life cycle represents when a JSP is being removed from use by a
container. The jspDestroy() method is the JSP equivalent of the destroy method for servlets. Override
jspDestroy when you need to perform any cleanup, such as releasing database connections or closing
open files.
Simple example
<html>
<head><title>Hello World</title></head>
<body>
Hello World!<br/>
%>
</body>
</html>
NOTE: Assuming that Apache Tomcat is installed in C:\apache-tomcat-7.0.2 and your environment is
setup as per environment setup tutorial. Let us keep the above code in JSP file hello.jsp and put this file
in C:\apache-tomcat-7.0.2\webapps\ROOT directory. Browse through the same using URL
http://localhost:8080/hello.jsp. This would generate the following result: