servlets page redirect
In Servlets, you can redirect the client to a different page using the sendRedirect method of the HttpServletResponse object. Here's an example of a Servlet that redirects the client to a different page:
@WebServlet("/redirect")
public class RedirectServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.sendRedirect("https://www.example.com/");
}
}Sow:ecruww.theitroad.comIn this example, the Servlet redirects the client to https://www.example.com/ using the sendRedirect method of the HttpServletResponse object.
You can also redirect the client to a different Servlet or JSP page in your own application by specifying the relative path to the resource. Here's an example of a Servlet that redirects the client to a JSP page in the same application:
@WebServlet("/forward")
public class ForwardServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
RequestDispatcher dispatcher = request.getRequestDispatcher("/page.jsp");
dispatcher.forward(request, response);
}
}
In this example, the Servlet forwards the client to a JSP page named page.jsp using the RequestDispatcher object. The getRequestDispatcher method of the HttpServletRequest object is used to obtain a RequestDispatcher object for the specified JSP page. The forward method of the RequestDispatcher object is then called to forward the request and response objects to the specified page.
