getting context value from web.xml

need to getting context value from web.xml in java class, not in servlet class
Shashidhar_ShenoyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

a_bCommented:
Try - getServletContext().getInitParameter("ParamName");
0
a_bCommented:
whoops....my bad....did not read the question clearly....
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Shashidhar_ShenoyAuthor Commented:

I write this line of  code

import javax.servlet.*;
import javax.servlet.http.HttpServlet;

public class Contextest extends HttpServlet  {
      public static void main(String args[])
      {
            ServletContext context = getServletContext();
            String a = getContexValue(context);
      }
      public static String getContexValue(ServletContext context)
    throws Exception {
    String KPAXWebServices = context.getInitParameter("KPAXWebServices");
    System.out.println(KPAXWebServices);
    return KPAXWebServices;
    }

}

please find out the bug
0
a_bCommented:
Does this compile for you ?
0
Shashidhar_ShenoyAuthor Commented:
error in this line
ServletContext context = getServletContext();
0
a_bCommented:
the issue is that main is static
0
a_bCommented:
This will compile -
 public static ServletContext context;
    public static void main (String args[]) throws ServletException
    {
        context = null;
    }
   
    public void init() throws ServletException {
        context = getServletContext();
       
      }

I have not tested, whether it would work or not. Please let me know.
0
Shashidhar_ShenoyAuthor Commented:
web.xml  is

<context-param>
  <param-name>KPAXWebServices</param-name>
  <param-value>https://t2.kotakseconline.com/KPAX/services/KPAXWebServices</param-value>
 </context-param>
 <context-param>
  <param-name>keystore</param-name>
  <param-value>C:\\java\\jre\\lib\\security\\</param-value>
 </context-param>

code :

public static ServletContext context;
    public void init() throws ServletException {
        context = getServletContext();
      }
    public static void main (String args[]) throws ServletException
    {
        //context = null;
        String KPAXWebServices = context.getInitParameter("keystore");
        System.out.println("test"+KPAXWebServices);
    }

it throws an error

Exception in thread "main" java.lang.NullPointerException
      at com.kotak.framework.webservices.Contextest.main(Contextest.java:42)
0
Gibu GeorgeChief Technology OfficerCommented:
You will not be able to get the ServletContext from a main method, you will get it only when you are running the servlet in a ServletContainer. In that case you wil be able get the parameter using context.getInitParameter(java.lang.String name) .
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.