R4R provide basic Servlet Tutorials concept with
Servlet Examples .
Through R4R you can develop
Servlet programming concept. R4R provide
Servlet Interview Questions with answers.R4R provide
Servlet Languages study materials in easy way.
Servlet Examples
2.1 Servlet Basic Example
Servlet Hot Topics
4.1 Servlet Basic
JAVA SERVLET TECHNOLOGY
A Servlet is a Java programming language class used to extend
the capabilities of servers that host applications accessed via a
request-response programming model. Although servlets can respond to any type
of request, they are commonly used to extend the applications hosted by
Web
servers. For such applications,
Java Servlet technology defines HTTP-specific servlet classes. The javax.servlet and
javax.http.servlet are two
packages which provided
interface and classes for writing servlets. All the servlet must implement servlet interface ,which defines
life cycle method
When implementing a generic service, you can use or extend the
GenericServlet class provided with the Java Servlet API. The
HttpServlet class
provides methods, such as doGet and
doPost, for handling HTTP-specific services.
HTTP Servlet typically used to:
-
Provide the dynamically contents which get the result from the server.
-
Process are used to store the data which is submitted by the HTML.
-
Manage information about the state of a stateless HTTP. e.g. an online
shopping car manages request for multiple concurrent customers.
GENERIC SERVLET CLASS CONTAINS FIVE
METHODS
The generic servlet is the base class of servlet which contains
five methods
1.init() method:- Init()
is
called only once by the servlet container in its whole lofecycle the life of a servlet. The init() method takes a ServletConfig object that contains the
initialization parameters and servlet's configuration and throws a
ServletException if an exception has occurred.
2.Service() method :- This method
defined as public void service as (ServletRequest
req, ServletResponse res) which throws ServletException, as IOException
.If once the servlet starting getting the requests, the service()
method is called by the servlet container to respond.
3.getservlet config():- This method is defined as public ServletConfig
getServletConfig() this method contain initialization and startup of the servlet
and returns the Servlet Config object
4.getservlet info():- This
method is defined as public String getServletInfo()
The information about the servlet is returned by this method like version,
author etc. This method returns a string which should be in the form of plain text and not any kind of markup.
5.destroy():- This
method is defined as public destroy().this method is called when want to close the servlet.
LIFE CYCLE OF SERVLET
The life cycle of a servlet is controlled by the container in
which the servlet has been deployed. The life cycle of a servlet can be
categorized into four parts:
-
Loading and instantiation:-The servlet container
loads the servlet during startup or when the first request is made .After
loading of servlet container create the instance of the servlet
-
Initialization:-When creating the instances,after that
the servlet container calls the init() method .then
passes the servlet initialization parameters to the init()
method. init() method is called once through out the life cycle of servlet
-
Servicing the request :-After successfully
initialization ,servlet container call the service method for servising
any request. The service() method determines the kind of request and calls the appropriate method
(doGet() or doPost()) for handling the request and sends response to the
client using the methods of the response object
-
The servlet Destroying :-If the servlet is no
longer required for servicing any request, the servlet container will calls the
destroy() method .
ADVANTAGES OF JAVA SERVLET
Java servlet provide various advantages to build a server side
programming. Following are the advantages of java
-
Portable:- As servlets are written in java and
follow APIs of java so they are very high portable over operating
systems and serve implementations.
-
Efficient:- As compared to CGI applications
servlet is more efficient
-
Extensibility:-The servlet API is designed in such a way that it can
be easily extensible. As for now the servlet API
support Http Servlets, but in later it can be extended for another type of
servlet
-
Safety:- as java is safe servlets are also safe
,garbage collection prevent the leakage of memory and exception handling throws
the exceptions for the errors.
BASIC SERVLET STRUCTURE
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Servletname extends HttpServlet
{
public void doGet (HttpRequest request,HttpResponse response) throws ServletException, IOException
{
//code for business logic
//use request object to read client request
//user response object to throw output back to the client
}//close do get
}
Servlet life cycle include mainly four steps init()--->service()--->doGet() or doPost()--->destroy
A SERVLET PROGRAM (using its life
cycle)
//servlet program using its life-cycle
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Testservlet extends HttpServlet
{
int i;
public void init() trows ServletException
{
i=0; //initializing i value
}
//incrementing i value in doGet method
public void doGet(HttpServletRequest request, HttpServletReponse response)
throws IOEXception ,ServletException
{
response.setContentType("text/html");
PrintWriter out= resonse.getWriter():
if (i==0)
{
out.println("<html>");
out.prinln ("<head>");
out.println ("<title> Hello world </title>");
out.println ("</head>");
out.println ("<body>");
out.println ("<h1> value of i is initialized in init method </h1>"+"<h1>" + i + "</h1>"
out.println("</body>");
out.println ("</html>");
}
i = i+;
if ((i == 10)
{
out.println("<html>");
out.println("<head>");
out.println ("<title> HEllo world</title>");
out.println ("</head>");
out.println ("<body>");
out.println ("<h1> i's value reaches 10 hence calling destroy method to reset it </h1>' + "<h1>" + "</h1>");
out.println ("</body>")
out.println ("<html>");
destroy(); //call destroy method if i=10
}
if (i<10) //display increment value of i
{
out.prinln("<html>");
out.prinln("<head>");
out.prinln("<title> Hello world </title>");
out.prinln("<head>");
out.prinln("<body>");
out.prinln("<h1> value of i incremented in doGet </h1>" + "<h1>" + i + "<h1>");
out.prinln("</body>");
out.prinln("</html>");
}
}
public void destroy()//reset i value here
{
i=0;
}
}
RUNNING SERVLET
To run servlet first install and configure the server on your
system. web server is required to install to run the servlet.
To compileAnd Run:
javac ExampServlet.java
Servlets can be called directly by typing their uniform resource locator
(URL) into a browser's location window after you've started the server.
Servlets can also be invoked from an HTML form by specifying their URL in the
definition for a Submit button, for example.
Servlets can be called by any program that can open an hypertext transfer
protocol (HTTP) request.
What is web.xml file?
Web. xml file also called "web Deployment descriptor" allows us
to configure our web application inside the Servlet containerHere we can specify the name of our web application, define our
Java Servlets, specify initialization parameters for Servlets, define tag
libraries, and a whole lot more. This file is placed inside the /WEB-INF folder. we use two of the features of 'web.xml' file; name of
Servlet and the Servlet mapping to a URL. This will allow us to access our
Servlet using a URL like /TestServlet.
<?xml version="1.0"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>servlets.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/TestServlet</url-pattern>
</servlet-mapping>
</web-app>
To run a Servlet
Now we need to start Tomcat server. Type the following command
at DOS prompt and hit Enter to start the Tomcat server:
C:\apache_tomcat_6.0.14\bin\startup
Now open your browser and point to this address:
http://localhost:8080/star/Testservlet. You should a get response like following
image in your browser window:
SUMMARY
We began with the introduction to Java Servlets and learned that
Serlvets are simple Java classes that implement javax.servlet.Servlet
interface. In practice, we will most probably do that by extending
javax.servlet.GenericServlet or javax.servlet.http.HttpServlet classes.
By above discussions we get know that servlet is efficient and extensible as it
store once .
Tolal:1 Click:
1
Username :venkat
Comments :helo,
in generic servlet service method cant be implemented..
this is wrong ..
rectify that..
URL :http://r4r.co.in/java/servlet/
1
Show All Comments