ServletConfig interface
ServletConfig interface
When the Web Container initializes a servlet, it creates
a ServletConfig object for the servlet.
ServletConfig object is used to pass information to a servlet
during initialization by getting configuration information
from web.xml(Deployment Descriptor).
Methods of ServletConfig
String getInitParameter(String name):Returns a String value initialized
parameter, or NULL if the parameter does not exist.
Enumaration getInitParameters(): returns the names of the servlet's
initialization parameters as an Enumeration of String objects, or an empty
String getServletName(): return the name of the servlet instance
How to initialize the servlet inside web.xml
How to access Servlet init parameters?
Create the instance of ServletConfig interface by calling
getServletConfig() method.
Ex:
ServletConfig sc=getServletConfig();
ServletContext interface
ServletContext interface
For every Web application a ServletContext object is created by
the web container.
ServletContext object is used to get configuration information
from Deployment Descriptor(web.xml) which will be available to
any servlet or JSPs that are part of the web app.
Differences between ServletContext and
ServletConfig
ServletContext ServletConfig
Available to all servlets and JSPs that are part Available to only servlet for which the
of Web app <init-param> was configured.
Parameters are initialized with in <web-app> Parameters are initialized with in <servlet> for
not within <servlet> each specific servlet
Important Methods of ServletContext
String getInitParameter(String name): returns parameter value for
the specified parameter name, or NULL if the parameter does not
exist
Enumeration getInitParameters(): returns the names of the
context's initialization parameters as an Enumeration of String
objects
RequestDispatcher
What is RequestDispatcher?
RequestDispatcher is an interface, implementation of which defines
an object which can dispatch resource to any request(such as HTML,
Image, JSP, Servlet) from the server.
RequestDispatcher interface provides two important methods
void forward( ServletRequest , ServletResponse)
forward a request from one servlet to another resource (servlet, JSP, or HTML
file) on server
void include( ServletRequest, ServletResponse)
include the content of a resource (servlet, JSP, or HTML file) in the response.
sendRedirect()
The sendRedirect() method of HttpServletResponse interface can
be used to redirect response to another resource, it may be
servlet, jsp or html file.
It accepts relative as well as absolute URL.
It works at client side because it uses the url bar of the browser to
make another request. So, it can work inside and outside the
server.
Differences between forward() and
sendRedirect()
orward() method sendRedirect() method
The forward() method works at server side. The sendRedirect() method works at client
side.
It sends the same request and response It always sends a new request.
objects to another servlet.
It can work within the server only. It can be used within and outside the
server.
Example: Example:
request.getRequestDispacher("servlet2").for response.sendRedirect("servlet2");
ward(request,response);
Syntax of sendRedirect() method
Syntax:
public void sendRedirect(String URL)throws IOException;
Example:
response.sendRedirect("https://www.google.com");