Re: Accessing context parameters from web.xml in java class
- From: Lew <lew@xxxxxxxxxxxxx>
- Date: Sat, 29 Sep 2007 08:39:25 -0400
Sameer wrote:
public class DBUtils {
But then i [sic] have to depend on the external parameters from JSP.
Can i [sic] access the context parameters in the java [sic] class?
As i [sic] checked ServletContext is not getting resolved in the java [sic] class
Any way to access the data from web.xml in java [sic] class?
Please revert.
Interesting use of the term "revert". I'm guessing it's a regional variant usage.
You cannot access the context parameters directly from any class; you need to inject something that references them. Servlets do it by injecting ServletRequest and ServletResponse objects into the service() method. You can do it by injecting the parameters into your custom-class method invocations, either individually, as a parameter map of some sort, or via the request object entire.
protected void doPost(
HttpServletRequest request, HttpServletResponse response )
throws ServletException, IOException
{
NonServlet handler = new NonServlet();
handler.processRequest(request, response);
}
or
protected void doPost(
HttpServletRequest request, HttpServletResponse response )
throws ServletException, IOException
{
ServletContext ctx = getServletContext();
String dbDriver = ctx.getInitParameter( DBDRIVER );
logger.debug( "dbDriver "+ dbDriver );
Dao.loadDriver( dbDriver ); // give Dao class access to dbDriver parm
}
--
Lew
.
- Follow-Ups:
- References:
- Prev by Date: Re: data transport
- Next by Date: Re: Accessing context parameters from web.xml in java class
- Previous by thread: Accessing context parameters from web.xml in java class
- Next by thread: Re: Accessing context parameters from web.xml in java class
- Index(es):
Relevant Pages
|
|