*
The moose likes JSP and the fly likes Variable defined in one JSP scriptlet not visible in another 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 "Variable defined in one JSP scriptlet not visible in another" Watch "Variable defined in one JSP scriptlet not visible in another" New topic
Author

Variable defined in one JSP scriptlet not visible in another

Matt Terp
Greenhorn

Joined: May 25, 2011
Posts: 4
I'm having an issue when I try to define a variable in a JSP scriptlet, and then in a separate scriptlet on the same page attempt to use the variable. It looks like it goes out of scope.

I also cannot reference a variable from a servlet in a JSP expression tag. So I've had to write the entire page basically in one scriptlet.

Why is this happening? I'm using Tomcat 6.0.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

It could well be the case that the variable goes out of scope. If you want to debug that then you're going to have to figure out the block structure of the Java code which your JSP produces. That isn't an easy thing to do -- which is one of the reasons why it's a bad idea to have scriptlets in a JSP.

However I certainly wouldn't expect anything in a JSP to be able to access a variable from a servlet. That wouldn't even make sense... unless when you said "variable" there you weren't actually talking about an instance variable of the servlet, but some JSTL construct.

Anyway I would recommend getting rid of the scriptlet idea entirely. It's been obsolete for years. Put your logic in a servlet (all of it, not just some of it) and have that servlet put Java-bean objects into the request scope. Then have your JSP include JSTL and EL constructs use that data from the request scope to generate the HTML.
Matt Terp
Greenhorn

Joined: May 25, 2011
Posts: 4
Paul Clapham wrote:It could well be the case that the variable goes out of scope. If you want to debug that then you're going to have to figure out the block structure of the Java code which your JSP produces. That isn't an easy thing to do -- which is one of the reasons why it's a bad idea to have scriptlets in a JSP.

However I certainly wouldn't expect anything in a JSP to be able to access a variable from a servlet. That wouldn't even make sense... unless when you said "variable" there you weren't actually talking about an instance variable of the servlet, but some JSTL construct.

Anyway I would recommend getting rid of the scriptlet idea entirely. It's been obsolete for years. Put your logic in a servlet (all of it, not just some of it) and have that servlet put Java-bean objects into the request scope. Then have your JSP include JSTL and EL constructs use that data from the request scope to generate the HTML.


Just to be clear, I'm not trying to involve servlets in this at all. I'm merely trying to reference a variable that I define in a scriptlet near the top of the page on a separate scriptlet at the bottom.

Here's an example from the horse's mouth itself (http://java.sun.com/products/jsp/tags/11/syntaxref11.fm5.html):

<%
String name = null;
if (request.getParameter("name") == null) {
%>
<%@ include file="error.html" %>
<%
} else {
foo.setName(request.getParameter("name"));
if (foo.getName().equalsIgnoreCase("integra"))
name = "acura";
if (name.equalsIgnoreCase( "acura" )) {
%>

This code will not run on a jsp page in my tomcat instance.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Well, like I said, you're better off not writing that sort of thing in the first place. When you do that, you get into the situation you're in now, where things don't work and you can't explain what's going on. And without an explanation, nobody can even tell what the problem is, let alone fix it. Your guess was something about variable scope -- was there something which led to that guess, or was it just a shot in the dark?
Matt Terp
Greenhorn

Joined: May 25, 2011
Posts: 4
Paul Clapham wrote:Well, like I said, you're better off not writing that sort of thing in the first place. When you do that, you get into the situation you're in now, where things don't work and you can't explain what's going on. And without an explanation, nobody can even tell what the problem is, let alone fix it. Your guess was something about variable scope -- was there something which led to that guess, or was it just a shot in the dark?


Makes sense, can you point me in the right direction to figure out how to render my entire page via a servlet and JSTL/EL? I'll probably stick with the scriptlets for now but refactor to that other model later.

I'm currently using servlets in the ACTION attribute of my forms, that's easy, but I haven't really had a chance to render an entire page from a servlet alone. I realize it's probably a best practice, I'm just under the gun to get a proof of concept out and using JSP scriptlets was what I'm most familiar with and seemed most intuitive.

Matt Terp
Greenhorn

Joined: May 25, 2011
Posts: 4
Matt Terp wrote:
Paul Clapham wrote:Well, like I said, you're better off not writing that sort of thing in the first place. When you do that, you get into the situation you're in now, where things don't work and you can't explain what's going on. And without an explanation, nobody can even tell what the problem is, let alone fix it. Your guess was something about variable scope -- was there something which led to that guess, or was it just a shot in the dark?


Makes sense, can you point me in the right direction to figure out how to render my entire page via a servlet and JSTL/EL? I'll probably stick with the scriptlets for now but refactor to that other model later.

I'm currently using servlets in the ACTION attribute of my forms, that's easy, but I haven't really had a chance to render an entire page from a servlet alone. I realize it's probably a best practice, I'm just under the gun to get a proof of concept out and using JSP scriptlets was what I'm most familiar with and seemed most intuitive.



Do I have to install some kind of Tomcat plugin to use JSTL or EL? What all is involved? I've had a heck of a time trying to use the Java EE version of Eclipse to write JSP pages, and I just gave up and have been doing it all in Notepad (I know, it's super-lame, but like I said, I'm working for a bunch of doctors who just want to see something fast that looks pretty...time is of the essence).
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60804
    
  65

The EL is part of JSP. Just be sure your web.xml is properly declared (e.g. not using the 2.3 DTD).

JSTL is enabled by just dropping the jar files in WEB-INF/lib.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Siddarth Cherukumudi
Greenhorn

Joined: Aug 02, 2013
Posts: 29

Hi Matt,
I have faced the same problem today morning, just playing with the scriptlets, and was getting the same error you described
when i went to the generated servlet java class and checked it, i found out that the variable has gone out of scope

for your information i am pasting you the jsp and generated servlet code

My Jsp




Corresponding generated servlet code



as you can clearly see here the String Variable dogName is going out of scope, thats why i was getting the exception :banghead: , after that i have changed the code in the jsp



the code has worked :thumbup: , and variable did not go out of scope.

so now after understanding the pitfalls of the scriptlets i am going ahead as suggested by the bear and others
i also recommend you to use JSTL for your coding purpose


The reason for posting, is to share my observation to others, so that others can understand where things are going wrong if they face the same issue :)


HIH




With Best Regards,
Siddarth

OCPJP 6
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Variable defined in one JSP scriptlet not visible in another
 
Similar Threads
pageContext cannot be resolved
EL and Variables in JSP
Scope of variables in declarations
local variables getting carried across the request
methods within scriptlets