Accessing context parameters from web.xml in java class

S

Sameer

Dear All,
My web.xml is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID">
<display-name>Reports</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>db_driver</param-name>
<param-value>oracle.jdbc.driver.OracleDriver</param-value>
</context-param>
<context-param>
<param-name>db_server</param-name>
<param-value>192.168.31.41</param-value>
</context-param>
</web-app>

I able to acess it in the JSP using
ServletContext context = getServletContext();
String driver = context.getInitParameter("db_driver");
String server = context.getInitParameter("db_server");

I have also written a Java class in the Web App as:

import java.sql.*;
public class DBUtils {
public DBUtils() {
}
public static Connection getDBConnection(String driver, String
conn_url,
String db_username, String db_password) throws Exception {
Class.forName(driver).newInstance();
Connection con = DriverManager.getConnection(conn_url, db_username,
db_password);
return con;
}
}

I can get DB connection for the JSP using this:
con = DBUtils.getDBConnection(driver, conn_url, username,
db_password);

But then i have to depend on the external parameters from JSP.

Can i access the context parameters in the java class?
As i checked ServletContext is not getting resolved in the java class
Any way to access the data from web.xml in java class?

Please revert.

-Sameer
 
L

Lew

Sameer said:
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
}
 
S

Sameer

Sameer said:
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
}

Thanks I modified the procedure as:

import java.sql.*;
import javax.servlet.*;

public class DBUtils {
public static Connection getDBConnection(ServletContext context)
throws Exception {
String driver = context.getInitParameter("db_driver");
String server = context.getInitParameter("db_server");
String port = context.getInitParameter("db_port");
String sid = context.getInitParameter("db_sid");
String conn_url = "jdbc:eek:racle:thin:mad:" + server + ":" + port + ":"
+ sid;
String db_username = context.getInitParameter("db_username");
String db_password = context.getInitParameter("db_password");
Class.forName(driver).newInstance();
Connection con = DriverManager.getConnection(conn_url, db_username,
db_password);
return con;
}

}
 
L

Lew

Sameer said:
import java.sql.*;
import javax.servlet.*;

public class DBUtils {
public static Connection getDBConnection(ServletContext context)
throws Exception {
String driver = context.getInitParameter("db_driver");
String server = context.getInitParameter("db_server");
String port = context.getInitParameter("db_port");
String sid = context.getInitParameter("db_sid");
String conn_url = "jdbc:eek:racle:thin:mad:" + server + ":" + port + ":"
+ sid;
String db_username = context.getInitParameter("db_username");
String db_password = context.getInitParameter("db_password");
Class.forName(driver).newInstance();
Connection con = DriverManager.getConnection(conn_url, db_username,
db_password);
return con;
}

}

The happy-path part of this is more or less correct there, but for a few
points of note.

You don't need the throwaway instance of the DB driver, so Class.forName()
alone is enough, and you only ever need to load the driver once, not with
every connection. Consider using the generic version of Class.

You absolutely must not omit exception handling in production code. Never
declare a method simply "throws Exception"; use a custom exception. Always
handle and log exceptions. Log them at the point of occurrence. It even sets
a bad example to post Usenet code that violates these precepts unless you
include a disclaimer.

Use spaces, not TABs, to indent Usenet posts.
 
S

Sameer

The happy-path part of this is more or less correct there, but for a few
points of note.

You don't need the throwaway instance of the DB driver, so Class.forName()
alone is enough, and you only ever need to load the driver once, not with
every connection. Consider using the generic version of Class.

You absolutely must not omit exception handling in production code. Never
declare a method simply "throws Exception"; use a custom exception. Always
handle and log exceptions. Log them at the point of occurrence. It even sets
a bad example to post Usenet code that violates these precepts unless you
include a disclaimer.

Use spaces, not TABs, to indent Usenet posts.

Thanks for the suggestions.
What do you mean by Generic version of class? Accepting data types as
parameters?
Please clarify.
 
L

Lew

Sameer said:
What do you mean by Generic [sic] version of class [sic]? Accepting data types as
parameters?

<http://java.sun.com/docs/books/tutorial/java/generics/index.html>

It's a major feature of the Java language, arguably among the most major.

And I said "the generic version of Class", not "the Generic version of class".
It matters; I was speaking of the class Class specifically.

How about you read the Javadocs for Class? That will explain what I meant.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top