• 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

Unable to access config value in jsp

 
Ranch Hand
Posts: 41
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 55
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rajat Sharmanaive
Ranch Hand
Posts: 41
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 55
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rajat,

Try modifying your RequestDispatcher as:

And servlet mapping as :
 
Rajat Sharmanaive
Ranch Hand
Posts: 41
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 55
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 41
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 55
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
    Posts: 41
    Eclipse IDE Firefox Browser Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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.
     
    no wonder he is so sad, he hasn't seen this tiny ad:
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic