The following is some example code that is very common when working with Servlets.
To obtain the session object where request is the HttpServletRequest passed to the doGet and doPost methods,
HttpSession session = request.getSession();
To obtain the ServletContext object from the above session object,
ServletContext application = session.getServletContext();
The following code is equivalent to the include action,
RequestDispatcher dispatcher = application.getRequestDispatcher("page.jsp");
if(dispatcher == null)
{
response.sendError(response.SC_NO_CONTENT);
}
else
{
dispatcher.include(request, response);
}
The following code is equivalent to the forward action,
RequestDispatcher dispatcher = application.getRequestDispatcher("page.jsp");
if(dispatcher == null)
{
response.sendError(response.SC_NO_CONTENT);
}
else
{
dispatcher.forward(request, response);
}