File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Context param values from web.xml

 
Sai Priya
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3830
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check ServletConfig.getInitParameter
 
ankur rathi
Ranch Hand
Posts: 3830
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
..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:
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2874
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are we discussing Config parameters or COntext parameters?
 
Sai Priya
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic