This example code of servlet forwards the request and response to hello.jsp

servletToJsp.java

import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;
public class servletToJsp extends HttpServlet {

  public void doGet(HttpServletRequest request, HttpServletResponse response){
    request.setAttribute("servletName""servletToJsp");

    try {
      getServletConfig().getServletContext().getRequestDispatcher(
        "/JSP/Demo.hello.jsp").forward(request,response);

    catch (ServletException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

}

hello.jsp which display the servlet name passed by servletToJsp servlet.

<html>
<body bgcolor="white">
<h1> I have been invoked by 
<% 
out.print(request.getAttribute("servletName").toString());
%>
Servlet
</body>
</html>

Output:


{mosimage}