How to get init parameter in a java file

Hai

I hava an init parameter described in my deployment discriptor. i using this init paramater in the servlet. the same init object i want to use in a javabean which is used by a jsp.

And also i want to load this servlet loaded when my application is loaded in to the container.

i was trying this using <load-on-startup>1</load-on-startup> tag in web.xml

i have mentioned the value as 1 because that is the first servlet discribed in my <servlet> tag in the web.xml

is this process right, what exactly does the value in <load-on-startup> do.

Please help me soon

[629 byte] By [Raja_Abilasha] at [2007-11-26 13:09:58]
# 1

The load on startup specifies the order in which servlets will start up.

If one servlet depends on another already being initialized, you can set it up so they get started in the right order.

If you don't care about the order that much, they can all be 1.

With regards to the init parameter - is it for the servlet, or for the application in general?

You can get Servlet init parameters from the ServletConfig object.

They are most easily accessed in the init method of the Servlet.

To make the values accessible to other jsps, the easiest way is to store the value as an application scoped attribute in the ServletContext object.

That way any resource in the web app can access it at will.

evnafetsa at 2007-7-7 17:23:25 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Thanks for the reply.

i know that load on start up will load the servlets in sequence of the way wanted.

Now How can i order the servlet number i mean where do we mention that this is my first(1) servlet anf\d this is my second. Is it the order we mention the servlet name in the <servlet > tag?

Coming to init parameter

I want access the init parameters in a javabean, what iam doing is

->i wrote a servlet with init parameters as username and password

->in the init method if the servlet iam calling the init parameters which are accessable

-> I was trying to set the accessed parameters as context attribute so that can be available for the bean.

-> Iam using this bean as model. which connects the database.

->but when i run the servlet in browser it is giving me a Null Pointer Exception while seeting the Context attribute.

the following is the sample code

public void init(ServletConfig servletConfig) throws ServletException

{

try

{

super.init(servletConfig);

config=servletConfig;

userName = config.getInitParameter("name");

password = config.getInitParameter("password");

}

catch(Exception ex)

{

ex.printStackTrace();

}

}

public void doGet(HttpServletRequest servletRequest,HttpServletResponse servletResponse) throws ServletException,IOException

{

PrintWriter out = servletResponse.getWriter();

out.println("The UserName in The Init Parameters Is: "+userName);

out.println("The Password in The Init Parameters Is: "+password);

System.out.println("The UserName in The Init Parameters Is: "+userName);

System.out.println("The Password in The Init Parameters Is: "+password);

this.doPost();

}

public void doPost() throws ServletException,IOException

{

context.setAttribute("databaseUser",userName);

System.out.println("Added the Context Attribute :"+userName);

context.setAttribute("databasePassword",password);

System.out.println("Added the Context Attribute :"+password);

}

Raja_Abilasha at 2007-7-7 17:23:25 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

public void init(ServletConfig servletConfig) throws ServletException

{

try

{

super.init(servletConfig);

ServletContext cnt=servletConfig.getServletContext();

config=servletConfig;

userName = config.getInitParameter("name");

password = config.getInitParameter("password");

cnt.setAttribute("userName",userName);

cnt.setAttribute("password",password);

}

catch(Exception ex)

{

ex.printStackTrace();

}

}

public void doGet(HttpServletRequest servletRequest,HttpServletResponse servletResponse) throws ServletException,IOException

{

PrintWriter out = servletResponse.getWriter();

out.println("The UserName in The Init Parameters Is: "+userName);

out.println("The Password in The Init Parameters Is: "+password);

System.out.println("The UserName in The Init Parameters Is: "+userName);

System.out.println("The Password in The Init Parameters Is: "+password);

this.doPost();

}

public void doPost() throws ServletException,IOException

{

context.setAttribute("databaseUser",userName);

System.out.println("Added the Context Attribute :"+userName);

context.setAttribute("databasePassword",password);

System.out.println("Added the Context Attribute :"+password);

}

this was the method which my fellow poster is refering to

Try to get the value of that attribute in JSP either by

<jsp:useBean> tag or use <% application.getAttribute("userName"); >

RahulSharnaa at 2007-7-7 17:23:25 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
Friend I want use the attibute in the bean where i have a database connection
Raja_Abilasha at 2007-7-7 17:23:25 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
Put it into to session when you get from Init method. Then use it and pass it to bean from jsp and wherever you want.
harishotya at 2007-7-7 17:23:25 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

Even When adding a session Attribute its giving me a Null Pointer Exception, and to other I want to create database connection using this parameters. So i call the a getConnection() method wherever required. Either it may be jsp or a servlet or any other .java file. Now i Don't want to touch the source code to be modified.

So is my model independent. If i give the parameter from my source it is again a dependance problem.

Raja_Abilasha at 2007-7-7 17:23:25 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...