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

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
richardsimnettAsked:
Who is Participating?
 
objectsCommented:
are u sure jstl is installed ok, whet do u get with:

CampaignID: <%= campaignid %><br>
0
 
hongjunCommented:
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
 
objectsCommented:
       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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

Thanks,
Rick
0
 
hongjunCommented:
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
 
richardsimnettAuthor Commented:
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
 
objectsCommented:
means EL isn't working
0
 
richardsimnettAuthor Commented:
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
 
objectsCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.