Deep
Deep

Reputation: 471

pass java variable in jsp:param

 <%!  
    String str = "prerna";  
  %>  

 <jsp:include page="index.html">
      <jsp:param name="type1" value=<%=str%> >
      </jsp:param>  
 </jsp:include>

I want to pass a java variable in the param tag,but i am not sure how to do it.

I also want to access it in index.html.
Can anyone suggest me the way to do it ?

Upvotes: 21

Views: 81584

Answers (6)

Priyan E
Priyan E

Reputation: 1

Add double quotes for your value "<%=str%>" in <jsp:param .... value="<%=str%>"/>

<%
  String str = "prerna";
%>

<jsp:include page="index.html">
    <jsp:param name="type1" value="<%=str%>">
    </jsp:param>
</jsp:include>

Upvotes: 0

Sergio Figueiredo
Sergio Figueiredo

Reputation: 2007

Using request.setAttribute() you can pass the Java variable to the JSP.

 <%  
    String str = "prerna";

    request.setAttribute("myVar",str);
  %>  

 <jsp:include page="index.html">
      <jsp:param name="type1" value="${myVar}" >
      </jsp:param>  
 </jsp:include>

Upvotes: 0

Sam
Sam

Reputation: 250

just but the <%=str%> in double quotes this should work , i hope this is an answer to your question.

<%!  
    String str = "prerna";  
%>  

<jsp:include page="index.html">
      <jsp:param name="type1" value="<%=str%>" />  
</jsp:include>

Upvotes: 2

BalusC
BalusC

Reputation: 1108722

Just put it in value directly.

<jsp:include page="index.html">
    <jsp:param name="type1" value="prerna" />
</jsp:include>

Or use JSTL <c:set> to set it and EL ${} to get it.

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
...
<c:set var="type1" value="prerna" />
...
<jsp:include page="index.html">
    <jsp:param name="type1" value="${type1}" />
</jsp:include>

And if your included page is a jsp, then you can use it as ${param.type1}

Upvotes: 24

Saurabh Gokhale
Saurabh Gokhale

Reputation: 46395

Request parameters can be passed by using <jsp: param>
One can pass the parameter names and values to the forwarded file by using a <jsp: param> tag

Sample e.g :

HTML :

<html>
<head>
<title></title>
</head>
<body>
<jsp:forward page="ssParameters.jsp">
  <jsp:param name="myParam" value="Amar Patel"/>
  <jsp:param name="Age" value="15"/>
</jsp:forward>
</body>
</html>   

<jsp:param> tag is used to pass the name and values to the targeted file. These parameters will be retrieved by the targeted file by using request.getParameter() method. In this way one can pass and retrieve the parameters.

This page had a parameter forwarded to it:<br>
  <b>Name:</b> <%= request.getParameter("myParam") %><br>
  <b>Age:</b> <%= request.getParameter("Age") %>

Upvotes: 5

Borja
Borja

Reputation: 3610

To pass parameters to a jsp jstl:

/* JSP PARENT */

<jsp:include page="../../templates/options.jsp">                    
    <jsp:param name="action" value="${myValue}"/>       
</jsp:include>


/* JSP CHILD (options.jsp)*/

<div id="optionButtons left">       
    <span>${param.action}</span>
</div>

Upvotes: 3

Related Questions