Introduction to Servlet technology
Chapters
A Servlet lifecycle
A Servlet has certain life cycle which includes process from creation till destruction.
The Servlet container manages Servlet life cycle. The life cycle contains below steps:
- Servlet Class should be Loaded.
- Create Instance of Servlet.
- The init() method is used for servlet initialization.
- The service () method is used process clients request.
- The destroy () method is used terminate the servlet.
Finally, Servlet is garbage collected by garbage collector of JVM.
Load Servlet Class
Before a servlet can be invoked servlet container must first load its class definition.
Create Instance of Servlet
When servlet class is loaded, servlet container creates an instance of servlet.
The init() method
The web container calls init method only once after creating servlet instance. The init method is used to initialize servlet. The servlet is created when a user invokes a URL corresponding to servlet. The init() method creates or loads data that are used throughout life of servlet.
public void init() throws ServletException {
// code Initialization.
}
The service () method
The service() method is used to perform actual task. The servlet container calls service() method to handle requests coming from client( browsers) and to write formatted response back to client.
Whenever servlet receives request from server it creates a new thread and call service. The service() method check for HTTP request type (POST, GET, DELETE, PUT, etc.) and call doPut, doGet, doPost, doDelete, etc. methods as appropriate.
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException{
}
The destroy () method
The servlet is unloaded by servlet container, destroy() method is called. This step executed once, since servlet is unloaded only once. After destroy() method is called, servlet object is marked for garbage collection.
public void destroy() {
// Code Finalization.
}
A Hello World Servlet
The servlets can be created as follows:
- Servlet interface Implementation
- GenericServlet class Implementation (or)
- HttpServlet class Implementation
Here is an example of a HttpServlet
import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class HelloWorld extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Hello World!</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Hello World!</h1>");
out.println("</body>");
out.println("</html>");
}
}
Compiling a Servlet
Save the file as HelloWorld.Java
compile the file using
Javac HelloWorld.Java
This will create a file HelloWorld.Class.
Servlet Deployment
Copy HelloWorld.class
into <CATALINA_HOME>/webapps/ROOT/WEB-INF/classes
and create following entries in web.xml
file located in <CATALINA_HOME >/webapps/ROOT/WEB-INF/
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>
Restart apache server and access below link to get output.
http://localhost:8080/HelloWorld
Alright, so that was introduction to a servlet life cycle, in next part we will explore handling client requests.
Description
This guide introduces Servlet technology and we will cover below topics
- What is a Servlet?
- Initial Setup
- Life Cycle
- Various Examples
- Client Request
- Server Response
- Http Status Codes
- Writing Filters
- Exception Handling
- Cookies Handling
- Session Tracking
- Database Access
- Packaging
- Internationalization
This is to the point introduction to the topic to get you started
Prerequisites
Working knowledge of Core Java is essential
Audience
Beginner to Java Servlet technology or students looking to brush up their skills quickly
Learning Objectives
Learn Java Servlets
Author: Subject Coach
Added on: 2nd May 2015
You must be logged in as Student to ask a Question.
None just yet!