CS6320:  SW Engineering of Web Based Systems

 

   Servlet:  Calling other Servlets or JSPs, etc.
  • Idea from a Servlet call another Servlet or JSP IN THE SAME CONTAINER (otherwise you will have to get another ServletContext associated with the appropriate Servlet Container).

Step 1: Get the Servlet Context

ServletContext cx = getServletContext();

This is the object we use to communicate with the Servlet container.

Note: If you are doing this inside of a JSP you will already have the servlet Context in a predefined variable named "application".

Version Servlet 2.3 and on

 

//this is if you are invoking another servlet

 

//if same web application
String otherServletPath = "otherServlet";

 

RequestDispatcher mydisp = cx.getRequestDispatcher( otherServletPath);

 

//Now you can add any elements you want to
//the current request (req) object and invoke the
//servlet

mydisp.forward(req,res);

 

//now controll returned to this servlet

© Lynne Grewe