This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Saeed Amer
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • 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>

 
Anthony Watson
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • 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.
 
Rovas Kram
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Great!

Thank you so much both!
 
Saeed Amer
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • 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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic