Servlet in Java - Tutorials
In this post, we are going to get the basic knowledge of servlet important for developing any web application.
What is Servlet?
- A servlet can be defined as a technology which is used to develop the dynamic web application in Java.Servlet is an interface between Application Server or Web Server and database.The Servlet receives the request and generates the dynamic response.
- A web container provided by Web Server is responsible for managing the lifecycle of the servlet.It maps the incoming request URL with particular servlet if the user has proper access right.
- Servlet executes on web container which provides multithreading environment thus a servlet can handle multiple request same time.
- In our application, Servlet often uses Hypertext Transfer Protocol (HTTP) by extending HttpServlet.
- Servlet sends the document to the browser by defining the file type.
Deployment Descriptor-This is web.xml file present in webapp --WEB-INF in maven project or webcontent-WEB-INF folder in the dynamic web project.
Servlet mapping is defined in deployment descriptor.
- With Java EE annotations, the standard web.xml deployment descriptor is optional.
- The Deployment descriptor can be configured using annotation also.
Sample web.xml file
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>Archetype Created Web Application</display-name>
<welcome-file-list>
<welcome-file>home.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>LoginServlet</display-name>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.javainstance.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
</web-app>
servlet-class - is the fully-qualified class name of the servlet.
url-pattern - it used to map servlet with URL.
Servlet API
HttpServlet extends GenericServlet and implements Serializable while Generic Servlet implements Servlet, ServletConfig and Serializable interface.
- GenericServlet provides implementation to all the methods of except the service method.
- HttpServlet confined to Hypertext Transfer Protocol (HTTP) and provides method such as doGet(); doPost();
There are many other interface and class available under servlet API which we are not going to discuss as we are mainly concerned about these.
Servlet Life-Cycle
Servlet Life-Cycle can be defined as the process of Servlet initialization till Servlet destruction.
Below methods are the parts of servlet life cycle.
init() - This method get invoked method when servlet gets initialized.It executes only once in the complete cycle.Session independent data such as database connection pool can be initialized which will be used throughout the life cycle.
service() - This method gets invoked each time when servlet gets the request by creating a new thread.When service method get called by container it invokes appropriate doGet() or doPost() method.Thus we have to override doGet() and doPost() methods properly.
destroy()-which will be used throughout the life cycle.Session independent data such as database connection pool can be cleaned up here.
In next post, we will discuss the difference between doGet() and do Post().
You may be interested in Java Object Oriented Principal
Give you suggestion in comments.If you like the post Share with your friends on social network.
You may be interested in Java Object Oriented Principal
Give you suggestion in comments.If you like the post Share with your friends on social network.
Servlet in Java - Tutorials
Reviewed by JavaInstance
on
2:53:00 AM
Rating:
Reviewed by JavaInstance
on
2:53:00 AM
Rating:


No comments: