• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding init parameters

 
Vedhas Pitkar
Ranch Hand
Posts: 445
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I was trying to figure out that if I configure init parameters to a servlet in web.xml ,how do I retrieve them?I'm doing the following :
web.xml--->
<servlet>
<servlet-name>
test
</servlet-name>
<servlet-class>
TestServlet
</servlet-class>
<init-param>
<param-name>name</param-name>
<param-value>bar</param-value>
</init-param>
</servlet>
Servlet----->
String ss=this.getServletConfig().getInitParameter("name");
But I am getting ss as null .WHY?
 
See El
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vedhas Pitkar:
Hi,
I was trying to figure out that if I configure init parameters to a servlet in web.xml ,how do I retrieve them?I'm doing the following :
web.xml--->
<servlet>
<servlet-name>
test
</servlet-name>
<servlet-class>
TestServlet
</servlet-class>
<init-param>
<param-name>name</param-name>
<param-value>bar</param-value>
</init-param>
</servlet>
Servlet----->
String ss=this.getServletConfig().getInitParameter("name");
But I am getting ss as null .WHY?

This is strange !!
Are you sure you are using getInitParameter from ServletConfig object and not from ServletContext ?
If would be clearer if you could post the whole servlet code.
[ November 13, 2002: Message edited by: See El ]
 
Vedhas Pitkar
Ranch Hand
Posts: 445
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, here goes :
servlet-->
import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorldExample extends HttpServlet {
public void init(ServletConfig conf) throws ServletException
{
super.init(conf);
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
String s = request.getParameter("name");
String ss=getInitParameter("name");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.print("name is"+s);
String ss=getInitParameter("name");
out.print("<br> initt name is "+ss);
}
}
---------------------------
web.xml as prev mentioned.
 
See El
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vedhas Pitkar:
Ok, here goes :
servlet-->
import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorldExample extends HttpServlet {
public void init(ServletConfig conf) throws ServletException
{
super.init(conf);
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
String s = request.getParameter("name");
String ss=getInitParameter("name");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.print("name is"+s);
String ss=getInitParameter("name");
out.print("<br> initt name is "+ss);
}
}
---------------------------
web.xml as prev mentioned.

This code doesn't compile as you have duplicate declaration of "ss". After correcting this, it gaves me the output
with http://localhost/webapp/servlet/HelloWorldExample
name isnull
initt name is bar
with
http://localhost/webapp/servlet/HelloWorldExample?name=John
name isJohn
initt name is bar
Is that what you get too ?
Can't see any problem here....
 
Siva Jagadeesan
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String ss=getInitParameter("name");

Don't we need to call getInitParameter in a ServletConfig Object ?!?!
 
k space
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sivakumar Jagadeesan:
Don't we need to call getInitParameter in a ServletConfig Object ?!?!

No, your HelloWorldExample servlet extends HttpServlet, which extends GenericServlet. The GenericServlet has several helper methods, like
public java.lang.String getInitParameter(java.lang.String name)
Returns a String containing the value of the named initialization parameter, or null if the parameter does not exist. See ServletConfig.getInitParameter(java.lang.String).
This method is supplied for convenience. It gets the value of the named parameter from the servlet's ServletConfig.
 
Hafizur Rahman
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually the GenericServlet implements the ServletConfig. So you can get the method directly for any subclass of HttpServlet as HttpServlet is a GenericServlet.
 
trupti nigam
Ranch Hand
Posts: 627
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vedhas,
Have you configured the servlet in your deployment descriptor?
To get the servlet init parameter you have to explicitly map the servlet with some name.
I hope this helps..
Thanks,
Trupti
 
Vedhas Pitkar
Ranch Hand
Posts: 445
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok sorry bout the duplicate declaration of ss,copy -pasted it twice,I think.
Trupti,I have configured the servlet in web.xml,but am still not getting it.
 
Vedhas Pitkar
Ranch Hand
Posts: 445
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry ,ranchers.My mistake.Got it working,Many Thanx to u all.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic