HTML header
HTML header
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.*;
context = event.getServletContext();
context.log ((String)context.getAttribute("Counter"));
context.removeAttribute("Counter");
}
}