This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Servlet init parameter from JSP ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Servlet init parameter from JSP ?" Watch "Servlet init parameter from JSP ?" New topic

Servlet init parameter from JSP ?

Dumitru Husleag

Joined: Oct 01, 2003
Posts: 22
I made a simple page that reads some information. And I set in web.xml a 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 ?

Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3255
How are you accesing this "servlet"?

Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
Should you use the implicit variable config to get the parameter, instead of using this?

SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
Sumitro Palit
Ranch Hand

Joined: Dec 13, 2003
Posts: 37
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.
<url-pattern>/jspGetParams.jsp </url-pattern>
(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

Joined: Oct 01, 2003
Posts: 22
Your answer is corect Ortimus
Servlet mapping solved the problem:
and a call of this form
<form method="POST" action="./jspParamsTest">
Thanx alot Ortimus
Subject can be closed
I agree. Here's the link:
subject: Servlet init parameter from JSP ?
Similar Threads
implicit object doubt
Why is there no EL implicit object for Servlet Init Params?
Why this code to get Init Param in JSP not work?
jsp init parameters