This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
Win a copy of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 this week in the Programmer Certification forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

Regarding init parameters

 
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?
 
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....
 
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 ?!?!
 
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.
 
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.
 
Ranch Hand
Posts: 640
1
  • 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.
 
Well don't expect me to do the dishes! This ad has been cleaned for your convenience:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!