0% found this document useful (0 votes)
85 views9 pages

Assignment of ADVANCED JAVA: Title:-Rmi

This document provides information about Java servlets and Java Server Pages (JSP). It defines servlets as Java programs that run on a web or application server and act as a middle layer between requests and databases/applications. Servlets offer advantages over CGI like better performance and platform independence. The document describes the servlet lifecycle including initialization, processing requests via doGet/doPost methods, and destruction. It also provides details on JSP, which allows embedding Java code in HTML pages to generate dynamic web content.

Uploaded by

Tizazu Selemon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views9 pages

Assignment of ADVANCED JAVA: Title:-Rmi

This document provides information about Java servlets and Java Server Pages (JSP). It defines servlets as Java programs that run on a web or application server and act as a middle layer between requests and databases/applications. Servlets offer advantages over CGI like better performance and platform independence. The document describes the servlet lifecycle including initialization, processing requests via doGet/doPost methods, and destruction. It also provides details on JSP, which allows embedding Java code in HTML pages to generate dynamic web content.

Uploaded by

Tizazu Selemon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

WOLAITA SODO UNIVERSITY

SCHOOL OF INFORMATICS

DEPARTMENT OF INFORMATION TECHNOLOGY

Assignment of ADVANCED JAVA

TITLE :-RMI

Name: Tizazu selemon


Program: Weekend in MSc IT
ID: PGW/48803/13

SUB TO:- Professor Dr.Natarajan Sivanandam,

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.

 Performance is significantly better.


 Servlets execute within the address space of a Web server. It is not necessary to create a
separate process to handle each client request.
 Servlets are platform-independent because they are written in Java.
 Java security manager on the server enforces a set of restrictions to protect the resources on a
server machine. So, servlets are trusted.
 The full functionality of the Java class libraries is available to a servlet. It can communicate with
applets, databases, or other software via the sockets and RMI mechanisms that you have seen
already.

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.

Servlets – Life Cycle


A servlet life cycle can be defined as the entire process from its creation till the destruction.

The following are the paths followed by a servlet

 The servlet is initialized by calling the init () method.


 The servlet calls service() method to process a client's request.
 The servlet is terminated by calling the destroy() method.
 Finally, servlet is garbage collected by the garbage collector of the JVM.

The init() Method

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.

The init method definition looks like this:

public void init() throws ServletException {

// 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.

Here is the signature of this method:

public void service(ServletRequest request,

ServletResponse response)

throws ServletException, IOException{

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.

Here is the signature of these two methods.

The doGet() Method

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.

public void doGet(HttpServletRequest request,

HttpServletResponse response)

throws ServletException, IOException {

// Servlet code

The doPost() Method

A POST request results from an HTML form that specifically lists POST as the METHOD and it should be
handled by doPost() method.

public void doPost(HttpServletRequest request,

HttpServletResponse response)

throws ServletException, IOException {

// 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.

The destroy method definition looks like this:

public void destroy() {

// Finalization code...

Example

package itso.servjsp.servletapi;

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class SimpleHttpServletextends HttpServlet {

protected void service(HttpServletRequest req, HttpServletResponse res)

throws ServletException, IOException {

res.setContentType("text/html");

PrintWriter out = res.getWriter();

out.println("<HTML><TITLE>SimpleHttpServlet</TITLE><BODY>");

out.println("<H2>Servlet API Example - SimpleHttpServlet</H2><HR>");

out.println("<H4>This is about as simple a servlet as it gets!</H4>");

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.

Why Use JSP?

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:

JSP vs. Active Server Pages (ASP)

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.

JSP vs. Pure Servlets

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.

JSP vs. JavaScript

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.

JSP vs. Static HTML

Regular HTML, of course, cannot contain dynamic information.

JSP and Servlets

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

JSP elements (overview)

 Directives of the form <%@ ... %>


 Scripting elements
 Expressions of the form <%= expr%>
 Scriptlets of the form <% code%>
 Declarations of the form <%! code%>
 JSP Comments <%--... --%>
 Standard actions
o Example: <jsp:useBean>... </jsp:useBean>
 Implicit variables like request, response, out

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.

The compilation process involves three steps:

o Parsing the JSP.


o Turning the JSP into a servlet.
o Compiling the servlet.

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:

public void jspInit(){

// 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

its parameters as follows:

void _jspService(HttpServletRequest request,

HttpServletResponse response)

// Service handling code...

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.

The jspDestroy() method has the following form:

public void jspDestroy()

// Your cleanup code goes here.

Simple example

<html>

<head><title>Hello World</title></head>

<body>

Hello World!<br/>

<%out.println("Your IP address is " + request.getRemoteAddr());

%>

</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:

You might also like