• 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

Servlet init parameter from JSP ?

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I made a simple page that reads some information. And I set in web.xml a servlet:
....
<servlet>
<servlet-name>ParamsTest</servlet-name>
<jsp-file>/jspGetParams.jsp</jsp-file>
<init-param>
<param-name>initParam</param-name>
<param-value>initValue</param-value>
</init-param>
</servlet>
...
and in the page I call this:
...
Servlet init param : <%= getInitParameter("initParam")%>
...
wich gives me this
...
"Servlet init param : null "
I works right if I use a servlet class instead of a jsp page.
Question: What am I missing ?
 
Enthuware Software Support
Posts: 4810
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How are you accesing this "servlet"?
 
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Should you use the implicit variable config to get the parameter, instead of using this?
Nick
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dumitru,
I am assuming that you are accessing the page as a jsp and seeing the result that you described.
You must create a servlet mapping to map the servlet instance that you have defined to the url for your jsp. Then when you try to access the jsp page with that mapped url pattern, the corresponding mapped servlet will be called and the init params defined for the servlet will come into play.
<servlet-mapping>
<servlet-name>ParamsTest</servlet-name>
<url-pattern>/jspGetParams.jsp </url-pattern>
</servlet-mapping>
(Note: If you access it as the named servlet, you do get the init params correctly. Its only when you access the jsp directly without having this mapping, the container just creates another servlet instance for the jsp page without any init params.)
Hope this explains the behavior,
- ortimuS
[ May 03, 2004: Message edited by: ortimus tilap ]
[ May 03, 2004: Message edited by: ortimus tilap ]
 
Dumitru Husleag
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your answer is corect Ortimus
Servlet mapping solved the problem:
...
<servlet-mapping>
<servlet-name>ParamsTest</servlet-name>
<url-pattern>/jspParamsTest</url-pattern>
</servlet-mapping>
...
and a call of this form
...
<form method="POST" action="./jspParamsTest">
...
Thanx alot Ortimus
Subject can be closed
reply
    Bookmark Topic Watch Topic
  • New Topic