What is the difference between response.sendRedirect() and RequestDispatcher.forward(request,response) method?
Forward
- When we use the
forward
method, the request is transferred to another resource within the same server for further processing. - In the case of
forward
, the web container handles all processing internally and the client or browser is not involved. - When
forward
is called on the requestDispatcher
object, we pass the request and response objects, so our old request object is present on the new resource which is going to process our request. - Visually, we are not able to see the forwarded address, it is transparent.
- Using the
forward()
method is faster than sendRedirect
. - When we redirect using forward, and we want to use the same data in a new resource, we can use
request.setAttribute()
as we have a request object available. - When we forward it goes to a mapping within the server container
- The URL does not change.
- The page resubmit on reload.
forward
method, the request is transferred to another resource within the same server for further processing.forward
, the web container handles all processing internally and the client or browser is not involved.forward
is called on the requestDispatcher
object, we pass the request and response objects, so our old request object is present on the new resource which is going to process our request.forward()
method is faster than sendRedirect
.request.setAttribute()
as we have a request object available.SendRedirect
- In case of
sendRedirect
, the request is transferred to another resource, to a different domain, or to a different server for further processing. - When you use
sendRedirect
, the container transfers the request to the client or browser, so the URL given inside the sendRedirect
method is visible as a new request to the client. - In case of
sendRedirect
call, the old request and response objects are lost because it’s treated as new request by the browser. - In the address bar, we are able to see the new redirected address. It’s not transparent.
sendRedirect
is slower because one extra round trip is required, because a completely new request is created and the old request object is lost. Two browser request are required.- But in
sendRedirect
, if we want to use we have to store the data in session or pass along with the URL. - When we do redirect, the new page gets loaded from new URL.
- The URL changes in browser.
- The page does not resubmit on reload.
sendRedirect
, the request is transferred to another resource, to a different domain, or to a different server for further processing.sendRedirect
, the container transfers the request to the client or browser, so the URL given inside the sendRedirect
method is visible as a new request to the client.sendRedirect
call, the old request and response objects are lost because it’s treated as new request by the browser.sendRedirect
is slower because one extra round trip is required, because a completely new request is created and the old request object is lost. Two browser request are required.sendRedirect
, if we want to use we have to store the data in session or pass along with the URL.What is the difference between ServletContext and ServletConfig?
- Servlet context is shared by all the servlets.
- Servlet config is servlet specific
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0">
<display-name>spring-starter</display-name>
<servlet>
<servlet-name>abc</servlet-name>
<servlet-class>com.springimplant.MyServlet</servlet-class>
<init-param>
<param-name>name</param-name>
<param-value>Manu</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>abc</servlet-name>
<url-pattern>/home</url-pattern>
</servlet-mapping>
<context-param>
<param-name>name</param-name>
<param-value>Gaurav</param-value>
</context-param>
<context-param>
<param-name>phone</param-name>
<param-value>Apple</param-value>
</context-param>
</web-app>
Servlet
package com.springimplant;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 607162626915397781L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PrintWriter out = resp.getWriter();
out.print("Hi ");
ServletContext ctx=getServletContext();
String strName = ctx.getInitParameter("name");
out.println(strName);
String strPhone = ctx.getInitParameter("phone");
out.println(strPhone);
ServletConfig ctg = getServletConfig();
String configName= ctg.getInitParameter("name");
out.println(configName);
}
}
asdas
No comments:
Post a Comment