0% found this document useful (0 votes)
8 views

HTML header

The document discusses various aspects of servlets in Java, including how to handle HTTP requests and responses, and the importance of ServletContext and ServletContextListener. It explains the methods available for retrieving request information and managing application-wide context data. Additionally, it outlines the implementation of a listener to manage application startup and shutdown events, particularly for database connections.

Uploaded by

Dhruv Jaradi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

HTML header

The document discusses various aspects of servlets in Java, including how to handle HTTP requests and responses, and the importance of ServletContext and ServletContextListener. It explains the methods available for retrieving request information and managing application-wide context data. Additionally, it outlines the implementation of a listener to manage application startup and shutdown events, particularly for database connections.

Uploaded by

Dhruv Jaradi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

• GET /intro.html HTTP/1.

0
• User-Agent: Mozilla/4.0 (compatible; MSIE
4.0; Windows 95) Accept: image/gif,
image/jpeg, text/*, */*
• req.getServerName()
• getServletContext().getServerInfo()
• req.getProtocol()
• req.getServerPort()
• req.getMethod()
• req.getPathInfo()
• req.getServletPath()
• req.getQueryString()
• public String getRemoteAddr()
• public String getRemoteHost()
• req.getRemoteHost()
• req.getRemoteAddr()
• req.getAuthType()
• req.getRemoteUser()
• req.getContentType()
• req.getContentLength()
• req.getHeader("Accept")
• req.getHeader("User-Agent")
• req.getHeader("Referer")
• public void
ServletResponse.setContentType(String type)
• public ServletOutputStream
ServletResponse.getOutputStream() throws
IOException
• setContentLength(int len)
What determine whether browser send
GET or POST request?
<servlet>
<servlet-name>
ps
</servlet-name>
<servlet-class>
PrimeSearcher
</servlet-class>
<load-on-startup/>

</servlet>
ServletContext. ServletContext is a interface
which helps us to communicate with the
servlet container. There is only one
ServletContext for the entire web application
and the components of the web application
can share it. The information in the
ServletContext will be common to all the
components. Remember that each servlet will
have its own ServletConfig. The ServetContext
is created by the container when the web
application is deployed and after that only the
context is available to each servlet in the web
application.
• Two different way to get ServletContex
ServletContex ob =
getServletConfig().getServletContext()

ServletContex ob = getServletContext();
• ServletContextListener is a interface which contains
two methods:
• public void contextInitialized(ServletContextEvent
event)
• public void contextDestroyed(ServletContextEvent
event)
• When we implement any interface then we have to
implement its all methods. This listener will help a
application to start and shutdown the events.
• How the ServletContextListener is useful:
• 1. ServletContextListener is notified when the
context is initialized.
• a). ServletContextListener gets the context init
parameters from the ServletContext.
• b). It stores the database connection as an attribute,
so that the other components in the web
application can access it.
• 2. It will be notified when the context is destroyed.
It closes the database connection.
We need 3 class and one DD
• ServletContexListener
• Attribute class
• Servlet class
• import javax.servlet.*;
import javax.servlet.http.*;

public class AppListener extends HttpServlet implements ServletContextListene


r{

private ServletContext context = null;

public void contextInitialized(ServletContextEvent event) {


context = event.getServletContext();
context.setAttribute("Counter", new Integer(0));
}

public void contextDestroyed(ServletContextEvent event) {

context = event.getServletContext();
context.log ((String)context.getAttribute("Counter"));
context.removeAttribute("Counter");
}
}

You might also like