Servelets A
Servelets A
Servelets A
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.
Applications of Servlet
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.
Execution of Servlets basically involves six basic
steps:
1.The clients send the request to the webserver.
2.The web server receives the request.
3.The web server passes the request to the
corresponding servlet.
4.The servlet processes the request and generates
the response in the form of output.
5.The servlet sends the response back to the
webserver.
6.The web server sends the response back to the
client and the client browser displays it on the
screen.
HTTPS Secure
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.
These classes implement the Java Servlet and JSP
specifications. At the time of writing this tutorial, the
versions are Java Servlet 2.5 and JSP 2.1.
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.
1. public void init(ServletConfig config) throws ServletException
4) service method is invoked
The web container calls the service method each time when
request for the servlet is received. If servlet is not initialized, it
follows the first three steps as described above then calls the
service method. If servlet is initialized, it calls the service method.
Notice that servlet is initialized only once. The syntax of the
service method of the Servlet interface is given below:
1. public void service(ServletRequest request, ServletResponse response)
2. throws ServletException, IOException
1. public void destroy()
There are many elements in the web.xml file. Here is given some
necessary elements to run the simple servlet program.
1. <web-app>
2. A Web application (Web app) is an application program that is stored on
a remote server and delivered over the Internet through a browser
interface.
3. <servlet>
4. A servlet is a Java programming language class that is used
to extend the capabilities of servers that host applications
accessed by means of a request-response programming
model.
6. <servlet-class>DemoServlet</servlet-class>
7. </servlet>
8.
9. <servlet-mapping>
10. <servlet-name>sonoojaiswal</servlet-name>
11. <url-pattern>/welcome</url-pattern>
12. </servlet-mapping>
13.
14. </web-app>
<url-pattern> is sub element of <servlet-mapping>. This pattern is used at client side to invoke the
servlet.
1. http://localhost:9999/demo/welcome
client’s request.
Invokes the CGI application within the process
So, in CGI server has to create and destroy the process for every request.
It’s easy to understand that this approach is applicable for handling few
clients but as the number of clients increases, the workload on the server
increases and so the time is taken to process requests increases.
CGI(Common Gateway
Servlet Interface)
javax.servlet.http(Advance)
A Java Server Page life cycle is defined as the process that started with
its creation which later translated to a servlet and afterward servlet
lifecycle comes into play. This is how the process goes on until its
destruction.
Translation of JSP page to Servlet
Compilation of JSP page(Compilation of JSP into test.java)
Classloading (test.java to test.class)
Instantiation(Object of the generated Servlet is created)
Initialization(jspInit() method is invoked by the container)
Request processing(_jspService()is invoked by the container)
JSP Cleanup (jspDestroy() method is invoked by the container)
Translation of JSP page to Servlet :
This is the first step of the JSP life cycle. This translation phase deals with
the Syntactic correctness of JSP. Here test.jsp file is translated to
test.java.
Compilation of JSP page :
Here the generated java servlet file (test.java) is compiled to a class file
(test.class).
Classloading :
Servlet class which has been loaded from the JSP source is now loaded
into the container.
Instantiation :
Here an instance of the class is generated. The container manages one or
more instances by providing responses to requests.
Initialization :
jspInit() method is called only once during the life cycle immediately after
the generation of Servlet instance from JSP.
Request processing :
_jspService() method is used to serve the raised requests by JSP. It takes
request and response objects as parameters. This method cannot be
overridden.
JSP Cleanup :
In order to remove the JSP from the use by the container or to destroy the
method for servlets jspDestroy()method is used. This method is called
once, if you need to perform any cleanup task like closing open files,
releasing database connections jspDestroy() can be overridden.
Writing code for servlet is harder than JSP is easy to code as it is java in
JSP as it is HTML in java. HTML.
Servlet plays a controller role in the JSP is the view in the MVC approach
hasMVC approach. for showing output.
It does not have inbuilt implicit objects. In JSP there are inbuilt implicit objects.