Link to home
Start Free TrialLog in
Avatar of richardsimnett
richardsimnett

asked on

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
Avatar of hongjun
hongjun
Flag of Singapore image

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);
        }
%>
Avatar of Mick Barry
       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);
        }
Avatar of richardsimnett
richardsimnett

ASKER

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

Thanks,
Rick
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
And also are you sure work.length is == 3?
If not, then the value will be blank and you won't see anything.

hongjun
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
means EL isn't working
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
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.