Servlets are Java programs that run on a web server to process incoming HTTP requests and generate HTTP responses. Servlets are more efficient than other web technologies like CGI and JSP because they run in the same process as the web server, resulting in faster response times.
Table of Contents
Servlet Lifecycle
The lifecycle of Servlet consists of four stages: initialization, service, destroy, and garbage collection. We will discuss each stage of the lifecycle in detail and provide code examples to illustrate the different stages.
Initializing
public class MyServlet extends HttpServlet {
public void init(ServletConfig config) throws ServletException {
// Perform initialization tasks here
}
}
Service
public class MyServlet extends HttpServlet {
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Process incoming request and generate response here
}
}
Handling HTTP Requests
Servlets handle HTTP requests using a service method that processes incoming requests and generates HTTP responses. We will explain the different HTTP request methods and their use, and provide code examples to demonstrate handling of HTTP requests.
Handling a GET request
public class MyServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Handle GET request here
}
}
Handling a POST request
public class MyServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Handle POST request here
}
}
Generating HTTP Responses
Servlets generate HTTP responses using a response object that contains the HTTP response headers and content. We will explain the different HTTP response codes and their use, and provide code examples to demonstrate the generation of HTTP responses.
Setting response headers
public class MyServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().println("<html><body>Hello World</body></html>");
}
}
Setting response status codes
public class MyServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
}
}
Working with Session Management
Session management is an important aspect of web development that enables the tracking of user sessions across multiple HTTP requests. We will explain how to create, access, and manage sessions in servlets, and provide code examples to illustrate session management.
Creating a session
public class MyServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
session.setAttribute("username", "johndoe");
}
}
Accessing session attributes
public class MyServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
String username = (String) session.getAttribute("username");
}
}
Managing session timeout
public class MyServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
session.setMaxInactiveInterval(60); // session will expire after 60 seconds of inactivity
}
}
Servlet Filters
Servlet filters enable the interception and modification of incoming HTTP requests and outgoing HTTP responses. We will explain the role of servlet filters in web development, and provide code examples to demonstrate how to create and use servlet filters. write code examples in each heading
Creating a filter
public class MyFilter implements Filter {
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
// Perform filter tasks here
chain.doFilter(request, response); // Continue with the filter chain
}
}
Mapping a filter to a servlet
<filter>
<filter-name>MyFilter</filter-name>
<filter-class>com.example.MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>MyFilter</filter-name>
<servlet-name>MyServlet</servlet-name>
</filter-mapping>
Conclusion
Servlets are important for creating dynamic, server-side web applications. We have covered the topics related to servlets, including their definition, benefits, and development environment setup.