• Post Reply Bookmark Topic Watch Topic
  • New Topic

request.getParameter scope issue  RSS feed

 
Bill Brasskey
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when i do this:
<%! String appyear = request.getParameter("appyear"); %>
i get the error that "request is either a misplaced pkg name or non-existent entity".
I need to use this variable in other scriptlets in my page, so i need it to be "global"
i know i'm missing something here in my understanding, i just dont know what
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not 100% sure, but I do not think you can have a request parameter in a declaration tag.

Eric
 
Johnson Abraham
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Bill!
JSP implicit objects like request,session....etc are not visible in the declaration tag <%! %>.so it is throwing out the error like undefined variable. well i think u'v got the misconception that the variabls defined in the declaration tags r global.
Actually any variables defined in the declaration are instance variables in the jsp page implementation class.So parallel threads processing the page can see the variable n use it.
u can directly declare the variable in scriptlet as
<% String appyear = request.getParameter("appyear"); %>
before any other JSP scriptlet so that it will b visible to all the scriptlets.
try it out. n lemme know if it worked
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnson Abraham,

JavaRanch is a community of people from all over the world, many of who are not native English speakers. While using abbreviations like "u" instead of spelling out "you" is convenient when text messaging your friends on a cell phone or in a chat room, it presents an extra challenge to those that are already struggling with English. Additionally, such shortcuts may confound automated translation tools that patrons of the Ranch may be making use of.

I would like to ask for your help in making the content of JavaRanch a little easier to read for everybody that visits here by not using such abbreviations.

thanks,
bear
JSP Forum Bartender
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Declarations made with the <%! notation become class members of the servlet generated for the JSP by the container. As such they are not part of the service() method for the JSP. So, as Johnson pointed out, the request parameters and other run-time implicit objects are not available.

What are you trying to do? Especially these days when loads of Java in the pages is looked down upon as a poor practice, there's very little cause to use declarations in a JSP.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two additional suggestions:

1) Limit the use of Java in your pages; the best case being none. Investigate the use of the JSTL and the creation of custom actions instead.

2) Find where your container is writing the java files that it creates from your JSP pages and study how the various constructs that you put into a JSP are translated to Java code. Doing so would show you why your variable doesn't need to be "global" in order to be referenced by the various scriplets in the page.

Also be aware that any variable you make "global" by putting in a declaration are shared by all threads serving that JSP page. So what happens if two people hit the page at the same time?
 
Clerk Kent
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why not try to use <%..%>,I think it will be ok...
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"bone eater",

There aren't many rules here on the Ranch, but one is that proper names are required. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks,
bear
Forum Bartender
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!