Serv Lets
Serv Lets
Serv Lets
SERVLET
• Servlet technology is used to create a web application (resides at server side and generates a
dynamic web page).
• Servlet technology is robust and scalable because of java language.
• Servlet is a technology which is used to create a web application.
• Servlet is an API that provides many interfaces and classes including documentation.
• Servlet is an interface that must be implemented for creating any Servlet.
• Servlet is a class that extends the capabilities of the servers and responds to the incoming requests.
It can respond to any requests.
• Servlet is a web component that is deployed on the server to create a dynamic web page.
WEB APPLICATION
• Web application (Web app) is an application program that is stored on a remote server and
delivered over the Internet through a browser interface.Web services are Web apps by definition
and many, although not all, websites contain Web apps. According to Web.AppStorm editor Jarel
Remick, any website component that performs some function for the user requests qualifies as a
Web app.
• Web applications can be designed for a wide variety of uses and can be used by anyone; from an
organization to an individual for numerous reasons. Commonly used Web applications can include
webmail, online calculators, or e-commerce shops. Some Web apps can be only accessed by a
specific browser; however, most are available no matter the browser.
HOW DOES WEB APPLICATION WORKS
• Web applications do not need to be downloaded since they are accessed through a network. Users can access a
Web application through a web browser such as Google Chrome, Mozilla Firefox or Safari.
• For a web app to operate, it needs a Web server and a database. Web servers manage the requests that come from
a client. A database can be used to store any needed information.
• Web applications typically have short development cycles and can be made with small development teams. Most
Web apps are written in JavaScript, HTML5, or Cascading Style Sheets (CSS). Client-side programming typically
utilizes these languages, which help build an applications front-end. Server-side programming is done to create
the scripts a Web app will use. Languages such as Python, Java, and Ruby are commonly used in server-side
programming.
WORK FLOW OF SERVLET
WEB SERVER
WEB CONTAINER
REQUEST
CLIENT
SERVLET
RESPONSE
WEB CONTAINER
• A web container is the component of a web server that interacts with the java servlet.
• A web container manages the life cycle of servlets and it maps a URL to a particular
servlet while ensuring that the requester has relevant access rights.
• Java servlet do not have main method, so a container is required to load them. The servlet
gets deployed on the web container.
SERVLET LIFE CYCLE
• The web container maintains the life cycle of a servlet instance. Let's see the life cycle of the
servlet:
1. Servlet class is loaded.
2. Servlet instance is created.
3. init method is invoked.
4. service method is invoked.
5. destroy method is invoked.
SERVLET INTERFACE
• Generic servlet implements Servlet, ServletConfig and Serializable interfaces. It provides the
implementation of all the methods of these interfaces except the service method.
• You can create a servlet by inheriting the GenericServlet class and providing the implementation of
the service method.
DISADVANTAGES OF GENERIC SERVELET
• Working with Generic Servlet is not that easy because we don’t have convenient methods
such as doGet(), doPost(), doHead() etc in Generic Servlet that we can use in Http Servlet.
• In Http Servlet we need to override particular convenient method for particular request, for
example if you need to get information then override doGet(), if we want to send information
to server override doPost(). However in Generic Servlet we only override service() method
for each type of request which is not convinient.
In dopost(), on the other hand, will send the information through socket back to the
In doget() parameters are appended to URL and sent with header information. webservers and it won’t show in the URL bar.
In doget() only 1024 characters limit. In dopost() doest not have limit.
DEPLOYMENT DESCRIPTOR
• The deployment descriptor is the file used by the servlet container to define which servlets match
up with which URLs. It also defines which servlet or resource provides the landing page for the
root of the service.
• <web-app> • <servlet>
• <servlet> • <servlet-name>servlet2</servlet-name>
• <servlet-name>servlet1</servlet-name> • <servlet-
class>com.javatpoint.SecondServlet</servlet-
• <servlet- class>
class>com.javatpoint.FirstServlet</servlet-
class> • <load-on-startup>1</load-on-startup>
• <load-on-startup>0</load-on-startup> • </servlet>
• </servlet> • </web-app>
PRINT WRITER
• Prints formatted representations of objects to a text-output stream. This class implements all of
the print methods found in PrintStream. It does not contain methods for writing raw bytes, for
which a program should use unencoded byte streams.
• Unlike the PrintStream class, if automatic flushing is enabled it will be done only when one of
the println, printf, or format methods is invoked, rather than whenever a newline character
happens to be output. These methods use the platform's own notion of line separator rather than
the newline character.
• Methods in this class never throw I/O exceptions, although some of its constructors may. The client
may inquire as to whether any errors have occurred by invoking checkError().
REQUEST DISPATCHER
• The RequestDispatcher interface provides the facility of dispatching the request to another
resource it may be html, servlet or jsp. This interface can also be used to include the content of
another resource also. It is one of the way of servlet collaboration.
• An object of ServletContext is created by the web container at time of deploying the project. This
object can be used to get configuration information from web.xml file. There is only one
ServletContext object per web application.
• If any information is shared to many servlet, it is better to provide it from the web.xml file using
the <context-param> element.
• Advantage of ServletContext
• Easy to maintain if any information is shared to all the servlet, it is better to make it available for
all the servlet. We provide this information from the web.xml file, so if the information is changed,
we don't need to modify the servlet. Thus it removes maintenance problem.
SERVLET CONFIG
• An object of ServletConfig is created by the web container for each servlet. This object can be used
to get configuration information from web.xml file.
• If the configuration information is modified from the web.xml file, we don't need to change the
servlet. So it is easier to manage the web application if any specific content is modified from time
to time.
• Advantage of ServletConfig
• The core advantage of ServletConfig is that you don't need to edit the servlet file if information is
modified from the web.xml file.
DIFFERENCE BETWEEN SERVLET CONFIG AND SERVLET CONTEXT
SESSION TRACKING
• Cookies.
• HttpSession.
COOKIE IN SERVLET
• A cookie is a small piece of information that is persisted between the multiple client requests.
• A cookie has a name, a single value, and optional attributes such as a comment, path and domain
qualifiers, a maximum age, and a version number.
• By default, each request is considered as a new request. In cookies technique, we add cookie with
response from the servlet. So cookie is stored in the cache of the browser. After that if request is
sent by the user, cookie is added with request by default. Thus, we recognize the user as the old
user.
DRAWBACKS
• Any kind of object can be stored into a session, be it a text, object etc.
• Usage of sessions is not dependent on the client’s browser.
• Sessions are secure and transparent
DISADVANTAGES