Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regarding init parameters

 
Ranch Hand
Posts: 445
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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....
 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

String ss=getInitParameter("name");


Don't we need to call getInitParameter in a ServletConfig Object ?!?!
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 647
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry ,ranchers.My mistake.Got it working,Many Thanx to u all.
 
a fool thinks himself to be wise, but a wise man knows himself to be a fool - shakespeare. foolish tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic