tags:

views:

374

answers:

3

If we are coding a JSP file, we just need to use the embedded "application" object. But how to use it in a Servlet?

+1  A: 

The application object references javax.servlet.ServletContext and you should be able to reference that in your servlets.

To reference the ServletContext you will need to do the following:

// Get the ServletContext
ServletConfig config = getServletConfig();
ServletContext sc = config.getServletContext();

From then on you would use the sc object in the same way you would use the application object in your JSPs.

scheibk
+1  A: 

Try this:

ServletContext application = getServletConfig().getServletContext();
John Topley
+1  A: 

The application object in JSP is called the ServletContext object in a servlet; so you can do this to hold a reference to it:

public class MyServlet extends HttpServlet
{
    private ServletContext ctx = null;

    @Override
    public void init(ServletConfig config) throws ServletException { 
         ctx = config.getServletContext(); 
    } 
}
Boiler Bill
thanks for all of your quick answer! I've get it!
fwoncn