?
Solved

How do I get variables to be available outside of <% %>?

Posted on 2007-08-04
9
Medium Priority
?
182 Views
Last Modified: 2010-07-27
Hello,
Im new to JSP trying to figure some things out. How do I get variables that I set in code blocks <% %>, to be usable on a regular page? See my example below: I set the campaignid,listid,and userid values based on the querystring... it outputs fine in the block, but the variables arent avaialbe to the rest of the page?


<!---- first split apart the query string ----->
<%
        String[] work = request.getQueryString().split("_");
        if (work.length == 3)
        {
                String campaignid = work[0];
                String listid = work[1];
                String userid = work[2];
                out.println(campaignid + " | " + listid + " | " + userid);
        }
%>

CampaignID: ${campaignid}<br>
ListID: ${listid}<br>
UserID: ${userid}<br><br>


Thanks,
Rick
0
Comment
Question by:richardsimnett
  • 4
  • 3
  • 2
9 Comments
 
LVL 33

Expert Comment

by:hongjun
ID: 19633351
try declaring it outside the brackets {.. }

<!---- first split apart the query string ----->
<%
        String[] work = request.getQueryString().split("_");
        String campaignid = "", listid = "", userid = "";
        if (work.length == 3)
        {
                campaignid = work[0];
                listid = work[1];
                userid = work[2];
                out.println(campaignid + " | " + listid + " | " + userid);
        }
%>
0
 
LVL 92

Expert Comment

by:objects
ID: 19633352
       String[] work = request.getQueryString().split("_");
        String campaignid = "";
        String listid = "";
        String userid = "";
        if (work.length == 3)
        {
                campaignid = work[0];
                listid = work[1];
                userid = work[2];
                out.println(campaignid + " | " + listid + " | " + userid);
        }
0
 

Author Comment

by:richardsimnett
ID: 19633359
Tried both out guys. Neither worked... still dont get any output from the ${campaignid} tokens outside of the <% %>.

Thanks,
Rick
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 19633369
are u sure jstl is installed ok, whet do u get with:

CampaignID: <%= campaignid %><br>
0
 
LVL 33

Expert Comment

by:hongjun
ID: 19633371
And also are you sure work.length is == 3?
If not, then the value will be blank and you won't see anything.

hongjun
0
 

Author Comment

by:richardsimnett
ID: 19633381
Here is what I am getting...

The Code:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>

<!---- first split apart the query string ----->
<%
        String campaignid = "";
        String listid = "";
        String userid ="";
        String[] work = request.getQueryString().split("_");
        if (work.length == 3)
        {
                campaignid = work[0];
                listid = work[1];
                userid = work[2];
                out.println(campaignid + " | " + listid + " | " + userid + "<br>");
        }
%>

CampaignID: <%= campaignid %><br>
CampaignID: ${campaignid}<br>



The output:

1 | 1 | 1000
CampaignID: 1
CampaignID:


Why does this work for the first <%= %> but not for ${campaignid}? it makes no sense.

Thanks,
Rick
0
 
LVL 92

Expert Comment

by:objects
ID: 19633403
means EL isn't working
0
 

Author Comment

by:richardsimnett
ID: 19635140
objects,
any suggestions on how to get that to work.... it works in other places.. I have ${2+2} to test it out and that works....

Thanks,
Rick
0
 
LVL 92

Expert Comment

by:objects
ID: 19635891
Actually I take that back, think its a scope issue instead.
el cannot see local vars, only session/request vars. If you really want to use el, then try adding values to request scope.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Moore’s Law has proven itself time and time again since it was first introduced. So what’s next? Will Moore’s law continue to remain relevant, or will new technology take over and bring us the next big advancement in computing?
With more and more companies allowing their employees to work remotely, it begs the question: What are some of the security risks involved with remote employees and what actions should we take to secure them?
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Screencast - Getting to Know the Pipeline
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question