This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
I learn that one of the implicit objects passed to jsp (upon it's translation into the corresponding servlet) is the config object, that corresponds to the servletconfig object of the servlets. I try to access a parameter set in the web.xml file but all i am able to print in the jsp is null. Please look at the code:
This is what I do in my web.xml
In my result.jsp, i try to access it as:
i also try:
but i get null both the times. I am new to these topics so i can't possibly fathom the reason. I get the expected results when i try to access something that is my context parameter (using getServletContext() or application object). Please guide.
But i am coming to this jsp via a servlet. i.e by invoking the forward method of the request dispatcher object. This is the code in the servlet that brings me here. result.jsp is my page.
I do not want to directly invoke it , else i would have put a mapping tag in the web.xml. May be this is not the correct approach i am following, but as a beginner i want to try what all is permitted. I read that config is one of the object that is implicitly passed so i was trying to use it. Does it violate any fundamentals. Please help.
Thanks for the help. I did not need to modify the requestdispatcher. Your first suggestion worked. I simply added the servlet mapping.
Now I am able to access the servlet config parameters in the jsp. Even though i come via normal request dispatcher from servlet. But i cannot figure out the reason why it worked. I was under the impression that mapping tag is only required when we directly access something through browser (url pattern gives me that idea). But it seems that even when dispatching occurs from one server component to the other, mapping is required. Can you please elaborate.
I was under the impression that mapping tag is only required when we directly access something through browser (url pattern gives me that idea).
Irrespective of browser url, to invoke a particular servlet (from anywhere) its corresponding mapping is a must and it should be invoked using that particular mapped url.
Indeed i should do it from now onwards. But i hope it is not syntactically compulsory. Because earlier, with no mapping tag (and with no jsp-file tag too) i was able to move to the jsp (and do basic java computations) via forward method of the request dispatcher object. Offcourse, i was not able to retrieve values via config. So is the rule applicable in this context?
N Sahni wrote:
For your JSP to pick up servlet configuration from web.xml, it must be accessed via url pattern declared in servlet mapping.[/list]
This sounds very convincing. From what i changed in my code as asked by you, indeed this is what happened. Configuration parameters were picked by the jsp after there was a servlet mapping and url pattern (even though i did not access the jsp via client side). So i guess even in case of internal dispatching (i.e when we use the view method), container sees this:
as a url and then matches the appropriate servlet in web.xml.
This is just a guess from my side. Feel free to elaborate/correct if you have more to share. Thanks.