What is the difference between ServletConfig().getInitParameter() and ServletContext().getInitParameter()? I don't know when to use ServletContext().getInitParameter(). From API of Sun, this fn returns a String containing the value of the named context-wide initialization parameter. What is the meaning context-wide initialization parameter? Many thanks!!
hi dh chau, the getInitParameter of servletConfig is used to get the parameter values that were defined in the <init-param> tag of the <servlet> in web.xml e.g.
This params are available only inside the <servlet-class> specified i.e. com.DailyOvertimeServlet. So inside this servlet, you can retrieve this initialization parameters using ... String yourName = getServletConfig().getInitParameter("yourName"); ... For the getInitParameter of servletContext, it it used to get the parameter values that were defined in the <context-param> tag under <web-app> in web.xml
This context parameters are available to all servlets in your web-app i.e. context wide. Thus, in ANY servlet in your web app, you can retrieve the context parameters using ... String companyName = getServletContext().getInitParameter("companyName"); ... But don't just take my word on this, write some code, run, and see it happen.