2

Web.xml

    <param-name>driver</param-name>
    <param-value>com.mysql.jdbc.Driver</param-value>
</context-param>
<context-param>
    <param-name>url</param-name>
    <param-value>jdbc:mysql:///gts_user</param-value>
</context-param>
<context-param>
    <param-name>user_name</param-name>
    <param-value>root</param-value>
</context-param>
<context-param>
    <param-name>password</param-name>
    <param-value>gts</param-value>
</context-param>

i want get all value from web.xml using a java class file

3
  • No. The context will not be there in normal java file. You need the context to read param. Commented May 2, 2014 at 6:28
  • what i need ? plz specify Commented May 2, 2014 at 6:30
  • pass the servletcontext object to your java object constructor. Make sure that it won't affect the performance... Commented May 2, 2014 at 6:32

1 Answer 1

2

You'll be able to get access to all those context-param elements if you have access to the ServletContext. You have access to the ServletConfig in a number of places, namely, Servlet#init(..), Filter#init(..), and most of the listener types.

The method you are looking for is ServletContext#getInitParameterNames().

Otherwise, you will have to parse the web.xml yourself.

Sign up to request clarification or add additional context in comments.

6 Comments

ServleContext.getInitParameterNames() is not working in java class file
@AshishSrivastava Please re-read my answer. If you have access to the ServletContext provided by the Servlet container, you can use it. Otherwise, you will need to parse the web.xml yourself.
ohh sorry tell me how can i get the path of web.xml file using java for parse it
@AshishSrivastava You can always get it relative to your current working directory. I'll just say that that is a very bad idea and you shouldn't do it. There is absolutely no reason you should.
@AshishSrivastava No. The only way you should do it is through the Servlet container provided ServletContext.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.