How to call a jsp from the servlet?
You can invoke a JSP web page from a servlet via the functionality of the popular javax.servlet.RequestDispatcher interface. Complete the following steps in your code to use this mechanism:
- Get a servlet context instance from the servlet instance:
ServletContext sc = this.getServletContext();
- Get a request dispatcher from the servlet context instance, specifying the page-relative or application-relative course of the goal JSP page as enter to the getRequestDispatcher() method:
RequestDispatcher rd = sc.getRequestDispatcher("/jsp/mypage.jsp");
Prior to or in the course of this step, you can optionally make facts on hand to the JSP page thru attributes of the HTTP request object. See the next section, "Passing Data Between a JSP Page and a Servlet", for information.
- Invoke the include() or forward() approach of the request dispatcher, specifying the HTTP request and response objects as arguments. The performance of these techniques is similar to that of jsp:include and jsp:forward actions. The include() approach solely briefly transfers control; execution returns to the invoking servlet afterward.
BY Best Interview Question ON 13 Dec 2019