SESSION – 8
By→ Anuj Kumar Singh
SendRedirect in Servlet
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.
Difference between forward() and SendRedirect() method
forward() SendRedirect()
The forward() method works at server side. The SendRedirect() method works on client
Side.
It sends the same request and response objects to
another servlet.
It always sends new request.
It works within the server only. It can be used within and outside the server.
Example : request.getRequestDispatcher(“Servlet2”);
foward(request,response);
Example:
response.sendRedirect(“servlet2”);
Example of response.sendRedirect()
index.html
NewServlet.java
Welcome.html
Output
ServletConfig Interface
1. An object of ServletConfig is created by web container for each
servlet. This object can be used to get configuration information from
web.xml file.
2. If the configuration information is modified in the web.xml file, we don't
need to change the servlet. So it is easier to manage the web
application if any specific content is modified from time to time.
Advantages of ServletConfiguration
The core advantage of ServletConfig is that you don't need to edit the
servlet file if information is modified from the web.xml file.
Methods of ServletConfig interface
1. public String getInitParameter(String name):Returns the parameter
value for the specified parameter name.
2. public Enumeration getInitParameterNames():Returns an enumeration
of all the initialization parameter names.
3. public String getServletName():Returns the name of the servlet.
4. public ServletContext getServletContext():Returns an object of
ServletContext
How to get the object ServletConfig
1. getServletConfig() method of Servlet interface returns the object of
ServletConfig.
public ServletConfig getServletConfig();
2. Example of getServletConfig() method
ServletConfig config=getServletConfig();
//Now we can call the methods of ServletConfig interface
String driver =config.getInitParameter(“driver”);
3. Code in web.xml file
<init-param>
<param-name>driver</param-name>
<param-value>com.mysql.jdbc.Driver</param-value>
</init-param>
Example
a. NewServlet.java
b. web.xml
Output
Servlet   session 8

Servlet session 8

  • 1.
    SESSION – 8 By→Anuj Kumar Singh
  • 2.
    SendRedirect in Servlet ThesendRedirect() 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.
  • 3.
    Difference between forward()and SendRedirect() method forward() SendRedirect() The forward() method works at server side. The SendRedirect() method works on client Side. It sends the same request and response objects to another servlet. It always sends new request. It works within the server only. It can be used within and outside the server. Example : request.getRequestDispatcher(“Servlet2”); foward(request,response); Example: response.sendRedirect(“servlet2”);
  • 4.
  • 5.
  • 6.
  • 7.
    ServletConfig Interface 1. Anobject of ServletConfig is created by web container for each servlet. This object can be used to get configuration information from web.xml file. 2. If the configuration information is modified in the web.xml file, we don't need to change the servlet. So it is easier to manage the web application if any specific content is modified from time to time. Advantages of ServletConfiguration The core advantage of ServletConfig is that you don't need to edit the servlet file if information is modified from the web.xml file.
  • 8.
    Methods of ServletConfiginterface 1. public String getInitParameter(String name):Returns the parameter value for the specified parameter name. 2. public Enumeration getInitParameterNames():Returns an enumeration of all the initialization parameter names. 3. public String getServletName():Returns the name of the servlet. 4. public ServletContext getServletContext():Returns an object of ServletContext
  • 9.
    How to getthe object ServletConfig 1. getServletConfig() method of Servlet interface returns the object of ServletConfig. public ServletConfig getServletConfig(); 2. Example of getServletConfig() method ServletConfig config=getServletConfig(); //Now we can call the methods of ServletConfig interface String driver =config.getInitParameter(“driver”); 3. Code in web.xml file <init-param> <param-name>driver</param-name> <param-value>com.mysql.jdbc.Driver</param-value> </init-param>
  • 10.
  • 11.