File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Unable to access config value in 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 » Java » JSP
Bookmark "Unable to access config value in jsp" Watch "Unable to access config value in jsp" New topic
Author

Unable to access config value in jsp

Rajat Sharmanaive
Ranch Hand

Joined: Jun 30, 2011
Posts: 41

Hi
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.


Rajat Sharma
N Sahni
Ranch Hand

Joined: Jul 07, 2011
Posts: 55

Hi Rajat,

If you will try to invoke jsp directly, it will print null, as it won't get the servlet initialization parameters from web.xml.

You need to add servlet mapping, for example:

And then try to invoke the servlet using the url pattern provided like http://<ip>:<port>/<web-app-name>/myPage

Thanks and Regards,
Nilesh Sahni | nsahni@infocepts.com | www.infocepts.com
Rajat Sharmanaive
Ranch Hand

Joined: Jun 30, 2011
Posts: 41

Hi N Sahni

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.
N Sahni
Ranch Hand

Joined: Jul 07, 2011
Posts: 55

Hi Rajat,

Try modifying your RequestDispatcher as:

And servlet mapping as :
Rajat Sharmanaive
Ranch Hand

Joined: Jun 30, 2011
Posts: 41

N Sahni

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.
N Sahni
Ranch Hand

Joined: Jul 07, 2011
Posts: 55

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.
Rajat Sharmanaive
Ranch Hand

Joined: Jun 30, 2011
Posts: 41

N Sahni wrote:
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
Ranch Hand

Joined: Jul 07, 2011
Posts: 55

That's correct syntactically its not compulsory. You were able to move to JSP via forward method of the request dispatcher object, because JSP itself compiles into a servlet.

There could be several things:
  • If you have declared a servlet/JSP in web.xml without servlet mapping, syntactically it won't give any error, but its of no use.
  • In case of JSP, since, JSP itself compiles into a servlet, you can access it with its name without servlet mapping (then it wont pick up configuration from web.xml)
  • For your JSP to pick up servlet configuration from web.xml, it must be accessed via url pattern declared in servlet mapping.
  • Rajat Sharmanaive
    Ranch Hand

    Joined: Jun 30, 2011
    Posts: 41

    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.
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: Unable to access config value in jsp
     
    Similar Threads
    JSP - Problem in reading InitParameter
    problem with accessing JSP init parameters
    JSP init parameters.
    Problem with Jsp Init parameter
    Cannot retrieve init parameter through JSP file