• 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

Why are the implicit objects not available in declarative part of JSP?

 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are the implicit objects not available in declarative part of JSP? For example, following JSP code will fail to compile successfully:

<html>
<%! String name=request.getParameter(``name``); %>
Name of person is <%=name %>
</html>

 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JSP pages are translated into servlets and then they are compiled before they can be viewed. Declaring a variable in a JSP page means that the variable will become a (non static) class attribute in the corresponding servlet. At the time when the servlet is compiled, there is no such thing as a request because a request can't come in until after the servlet is compiled and made available by the container.
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In other words, request is a input parameter for the method _jspService therefore can only be accessed within _jspService.
 
Saeed Amer
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great!

Thank you so much both!
 
Saeed Amer
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and it worked OK when I changed the code to:

<html>
<% String name=request.getParameter("name"); %>
Name of person is <%=name %>
</html>

bcoz now the variable declaration for "name" would become part of the _jspService() method which has access to the implicit object "request" (and that is exactly what you two said above)!

Thanks again!
 
Story like this gets better after being told a few times. Or maybe it's just a 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