File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Context param values from web.xml Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Context param values from web.xml " Watch "Context param values from web.xml " New topic
Author

Context param values from web.xml

Sai Priya
Greenhorn

Joined: Jul 06, 2006
Posts: 7
Hi,
Can any one help me to get the context-param values from web.xml to custom tag java files?
I want to place the Database connection in <Context-param> in web.xml
and want to retrieve that to build a Java class for Custom tag libraries?

for eg.
I have given like
<context-param>
<param-name>db</param-name>
<param-value>jdbcdbc:db,uid,pwd</param-value>
</context-param>

In the java file,
I tried with pagecontext.getServletContext(), But it didnt work
Can any one tell me?

Thanks in Advance,
Saipriya
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Originally posted by Sai Priya:
Hi,
Can any one help me to get the context-param values from web.xml to custom tag java files?
I want to place the Database connection in <Context-param> in web.xml
and want to retrieve that to build a Java class for Custom tag libraries?

for eg.
I have given like
<context-param>
<param-name>db</param-name>
<param-value>jdbcdbc:db,uid,pwd</param-value>
</context-param>

In the java file,
I tried with pagecontext.getServletContext(), But it didnt work
Can any one tell me?

Thanks in Advance,
Saipriya


I think, this should work:

pagecontext.getServletContext().getInitParameter("key");

Are you getting any error??
Sai Priya
Greenhorn

Joined: Jul 06, 2006
Posts: 7
thanks Ranjit,
I got the values in JSP file thru pagecontext object.

but i want to know whether we can retrieve the context-param values in JAVA file.
eg. I have used like this
package database;
import java.sql.*;
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;


public class DBConnection extends HttpServlet
{
private Connection con=null;
String com;

public Connection getConn(String str)
{

try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
ServletContext context = pageContext.getServletContext();
com = context.getInitParameter("com");

}

catch(Exception ex)
{
System.out.println("DBConnection"+ex);
return(null);
}
}
}
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Check ServletConfig.getInitParameter


[My Blog]
All roads lead to JavaRanch
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Originally posted by Sai Priya:
thanks Ranjit,
I got the values in JSP file thru pagecontext object.

but i want to know whether we can retrieve the context-param values in JAVA file.
eg. I have used like this
package database;
import java.sql.*;
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;


public class DBConnection extends HttpServlet
{
private Connection con=null;
String com;

public Connection getConn(String str)
{

try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
ServletContext context = pageContext.getServletContext();
com = context.getInitParameter("com");

}

catch(Exception ex)
{
System.out.println("DBConnection"+ex);
return(null);
}
}
}


Thanks for new name.

;)

Are you sure, you are setting context parameters in web.xml, correctly.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

..but i want to know whether we can retrieve the context-param values in JAVA file.

Not sure what you mean by "JAVA file".
In your first example you pasted code for a custom tag.

Now you are posting code for a servlet.
In a servlet, you don't have the implicit varible 'pageContext' but you do inherit the getServletContext method.

So, instead of:

in a servlet, you would use:


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by Sai Priya:
but i want to know whether we can retrieve the context-param values in JAVA file.


Yes you can, if have the access to context object.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Are we discussing Config parameters or COntext parameters?
Sai Priya
Greenhorn

Joined: Jul 06, 2006
Posts: 7
rathi, ( I 've used correct name)
<context-param>
<param-name>db</param-name>
<param-value>jdbcdbc:db,uid,pwd</param-value>
</context-param>

This is how i've given in web.xml,
I got the param values when i use [/B] in JSP page

But as Ben told, ( in servlet) i've used
It thrown compiler error,

and Ben
Now i tried with in servlet, it is throwing Null pointer exception.

This servlet i'm using for Custom tag libraries.

Wht is the way to get the context-param value in servlet file?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Sai Priya:

This servlet i'm using for Custom tag libraries.


???
Are you writing a servlet or a tag lib?

I'm starting to think there may be some misunderstandings about the lifecycle of the different components you're writing.
How are you instanciating this Java (servlet/tag) class?
[ July 12, 2006: Message edited by: Ben Souther ]
Sai Priya
Greenhorn

Joined: Jul 06, 2006
Posts: 7
Yes Ben,
I expressed the question in the wrong way,
Any how thanks for u all,
I got the value by using pageContext
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Context param values from web.xml