Servlet in Java
HTTP is the protocol that allows web servers and browsers to
exchange data over the web. It is a request and response protocol. HTTP uses
reliable TCP connections—by default on TCP port 80. The client requests a file
and the server responds to the request. It's always the client who initiates a
transaction by establishing a connection and sending an HTTP request. The
server is in no position to contact a client or make a callback connection
tothe client. Either the client or the server can prematurely terminate a
connection.
We Can normally adopt two main architectures when developing web
applications in Java.
The first architecture utilizes servlets and JSP in the middle
tier to serve clients and process the business logic. Small to medium-size
applications use this design model.Java Training in Bangalore
The second architecture includes the use of J2EE server and
Enterprise JavaBeans (EJB) and this is especially useful for large enterprise
applications that need scalability.
Add caption |
Creation process of servlet:
A servlet can be created by inheriting from the GenericServlet or HttpServlet class in a user defined class(The GenericServlet class is available in javax.servlet package and the HttpServlet class is defined in javax.servlet.http package ).
Creation of servlet by using GenericServlet class:
1.
Cerate a class by inheriting
from GenericServlet class, the user defined class must be public, bcoz the
servletr will be accessed by web server.
2.
Override the methods of
GenericServlet class in user defined class to provide logic for the servlet.
These methods are as follows.public void init() : This
executes during initialization of servlet class. It is used to initialize
resources that the servlet might use. This method gets called only once during
the servlet's lifetime. Generally this method will not be overrien by the
programmer bcoz the web server will excute this methd.
·
public abstract void service(ServletRequest req,ServletResponse res)
throws Servlet-Exception, IOException : This method will be called by the
web server only when a client will make the http request. This method is also
responsible to produce the http response back to the client after processing
the client’s requet. This method generally overridden by the programmer.
·
public
void destroy() :
this executes during removal of servlets instance from the servlet
container. It is also invoked by the web server.
3.
Create an object of
java.io.PrintWriter class by calling getWriter() of ServerletResponse
interface, so that response can be given back to client in html format to the
user within the println().
Life Cycle of Servlet:
·
init()
: This method executes during
initialization of servlet within the servlet container. Whenever the first user
of servlet comes to access the servlet then an instance of the servlet is being
created and this method executes. This method is required for performing any
common job for all users. Ex. Opening connection with DBMS.
·
service()/doGet()/doPost()
: This method executes to provide
response by accepting request from the users. Whenever any user comes to access
the servlet then the web- server creates new thread and the thread executes
this method. Hence this method executes once for each user by using one
instance of the servlet.
·
destroy() : This
method executes during removal of servlet instance from the web server. If a
servlet is not being accessed by any user for a period then the instance of the
servlet is being removed and this method executes. This method may perform any
task which is common for all users.
Why
ZenRays:
§ Hands-on-Training
§ 100%
job support
§ Live
Project
§ Powered
By IITians
ZenRays provide the following to make you expert:
1) Fully practical and project based sessions
from first class.
2) Training by Experienced Consultants, not
regular Trainers
3) Friendly and enthusiastic faculty to clear
your doubts 24X7
4) Free Live project after the training to get
you industry experience
If
you want more details please visit us:
Zenrays.com Reach us at trainings@zenrays.com
ABOUT THE AUTHOR
Hello We are OddThemes, Our name came from the fact that we are UNIQUE. We specialize in designing premium looking fully customizable highly responsive blogger templates. We at OddThemes do carry a philosophy that: Nothing Is Impossible
0 comments:
Post a Comment