We help IT Professionals succeed at work.

How to get result from JSTL c:import

CIPL-Senthil
CIPL-Senthil asked
on
1,789 Views
Last Modified: 2013-11-24
Hi,

Please see the below code for clear understanding
The following code is there in my jsp page

<c:import url="http://localhost:8080/Bizpiper/flashdb.do?method=PromotionInfo" var="result" scope="request">
      <c:param name="proid" value="<%=p%>"></c:param>
      <c:param name="yname" value="<%=name%>"></c:param>
      <c:param name="ymail" value="<%=email%>"></c:param>
      <c:param name="ymsg" value="<%=text%>"></c:param>
      <c:param name="loginid" value="<%=loginid%>"></c:param>
      <c:param name="eName" value="<%=eName%>"></c:param>
</c:import>
<c:set var="retresult" value="{result}"/>
</body>
<%
String result=request.getParameter("retresult");
System.out.println("result "+result);
result="&status="+result;
out.print("&status=yes");
%>

obviously the url will get executed. in ation class i am setting teh variable in request scope as follows.
request.setAttribute("result","yes"). in jsp page i am assuming that the result will get stored in defined variable of c:import tag(i.e., var="result") and using <c:set tag i have set in one more variable retresult. later inside code snippets i am getting the result using request.getParameter("retresult"). But it is giving null value.

Please help me in this regard. I want to know how to get result back from action to jsp page while using c:import

Regards,
Malathi
Comment
Watch Question

CERTIFIED EXPERT

Commented:

try to set like this

value="${result}"/>

<c:set var="retresult" value="${result}"/>

Author

Commented:
sorry here only i missed teh dollar. but actually i kept $ in my code. it has given null.

Commented:
As I can see, "retresult" is an attribute, not parameter of the request object. You would better try:

String result=(String)request.getAttribute("retresult");

Commented:
And add the attribute "scope" to the tag:

<c:set var="retresult" value="{result}" scope="request"/>
CERTIFIED EXPERT

Commented:
Can u do onethig dont set the value to anything
just display !!

<c:out value="${result}"/> and u know the rquest scope having value or not ??  

CERTIFIED EXPERT

Commented:
<c:import url="http://localhost:8080/Bizpiper/flashdb.do?method=PromotionInfo" var="result" scope="request">

<c:out value="${result}"/>
      <c:param name="proid" value="<%=p%>"></c:param>
      <c:param name="yname" value="<%=name%>"></c:param>
      <c:param name="ymail" value="<%=email%>"></c:param>
      <c:param name="ymsg" value="<%=text%>"></c:param>
      <c:param name="loginid" value="<%=loginid%>"></c:param>
      <c:param name="eName" value="<%=eName%>"></c:param>
</c:import>
rrzstudent
CERTIFIED EXPERT

Commented:
I think ioanton was on the right track.
 
instead of using  
>request.getParameter("result");  
you should use  
String result = (String)request.getAttribute("result");  
System.out.println("result "+result);

instead of using  
request.getParameter("retresult").  
you should use  
String retresult = (String)pageContext.getAttribute("retresult");
System.out.println("retresult " + retresult);

Author

Commented:
I tried everything. sorry nothing is working. it is not returning any value
CERTIFIED EXPERT

Commented:
do u think <c:import url="http://localhost:8080/Bizpiper/flashdb.do?method=PromotionInfo" var="result" scope="request"> having the value ??
rrzstudent
CERTIFIED EXPERT

Commented:
For a test, you could use the following code.
<%
      // define  the six scripting variables here
%>
<c:import url="http://localhost:8080/Bizpiper/flashdb.do" >
      <c:param name="method" value="PromotionInfo" />
      <c:param name="proid" value="<%=p%>" />
      <c:param name="yname" value="<%=name%>" />
      <c:param name="ymail" value="<%=email%>" />
      <c:param name="ymsg" value="<%=text%>" />
      <c:param name="loginid" value="<%=loginid%>" />
      <c:param name="eName" value="<%=eName%>" />
</c:import>

Open in new window

Author

Commented:
i did the same. its not working. i am having a doubt. in action class i am setting request.setAttribute("result") and after returning to teh jsp page i am expecting to store the set value in request scope in action to get populated in the vatiable defined in c:import tag.

Will it actually store the value in this manner. Is this teh corerct way to do?

Author

Commented:
-- do u think <c:import url="http://localhost:8080/Bizpiper/flashdb.do?method=PromotionInfo" var="result" scope="request"> having the value ??  --

This is what my doubt. please see my above comment. Is it the way to do to get the value into jsp c:import variable.
CERTIFIED EXPERT

Commented:
it wont be there bcz u have chnaged in to another jsp.... no request scope wont work

Author

Commented:
even if i am keeping in session scope also it is not working. Then how i can get the result back to jsp page. Is there any way to get it.
CERTIFIED EXPERT

Commented:
do one thing ...
u have set the value in ur action class right ....

print the value after u have set check whether the value is set or not ??

Commented:
The value you set in the action class cannot get in the c:import variable. Note that the value of the c:import variable is the content of the resource found at the specified url.

Author

Commented:
the if i want to return some result back to jsp from action what i need to do

Commented:
You can set the attribute in the action class using request.setAttribute() and get the value in jsp using request.getAttribute(). You can also access the value of the attribute via the construct ${attributeName}.
rrzstudent
CERTIFIED EXPERT

Commented:
>i did the same. its not working.
If nothing has worked yet, then try the following code.  Use just those two lines in a JSP.  It should give some response.  

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:import url="http://localhost:8080/Bizpiper/flashdb.do" />

Open in new window

Author

Commented:
-- You can set the attribute in the action class using request.setAttribute() and get the value in jsp using request.getAttribute(). You can also access the value of the attribute via the construct ${attributeName}. --

I tried keeping a variable in action in session scope and tried to get the same from session scope. it is returning null value. i think with c:import we can not do this even

Commented:
I was thinking of the following scenario:

// action class
request.setAttribute("attrName", someValue);

// jsp - one of the variants below:

<% String s = (String) request.getAttribute("attrName"); %>

<c:out value="${attrName}"/>

<c:if test="${attrName == 'valueTest'}">
    <%--do something--%>
</c:if>

<c:choose>
    <c:when test="${not empty attrName}">
        <%--do something--%>
    <c:when>
    <c:otherwise>
            <%--do something else--%>
    </c:otherwise>
</c:choose>

Using c:import to get the value of this attribute is out of the question.

Author

Commented:
I tried the same. please see below code that i have tried

in action class

session.setAttribute("result",al); or request.setAttribute("result",al);

in jsp page

session.getAttribute("result") or request.getAttribute("result")

But it giving null value


Commented:
Make sure there is not another attribute having the same name ("result") in the request (session). Even the var of c:import tag might overwrite the value you set in the action class. You can better change the name of the attribute and try again.

Author

Commented:
sorry...i tried it out..its not working

Author

Commented:
Any other suggestions
rrzstudent
CERTIFIED EXPERT

Commented:
I suggest you start over. You can request that this question be deleted since no solution was found.
Ask a new question and try to explain your problem more clearly.
CERTIFIED EXPERT

Commented:
Can u mention the solution !!
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT

Commented:
gd  :)
South ModModerator
CERTIFIED EXPERT

Commented:
Accepting author solution instead

SouthMod
Community Support Moderator
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.