21
 <%!  
    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 ?

Deep
  • 461
  • 5
  • 9
  • 17

5 Answers5

24

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}

Abubakkar
  • 15,488
  • 8
  • 55
  • 83
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 9
    Rename `index.html` to `index.jsp` and use `${param.type1}` to get it. – BalusC Mar 27 '11 at 04:43
  • I want to retrive a data from database using rs.getString(1).that value i want to pass it in param – Deep Mar 27 '11 at 04:43
  • Create a DAO class which does the DB interaction job and returns normal Java objects. Import and call the DAO class in a servlet class. Let the servlet put it in request scope and forward to JSP. Finally use servlet URL instead of JSP URL. – BalusC Mar 27 '11 at 04:45
  • actually I think i am not clear...I am diaplaying user topics in my jsp page....there i have retrieved a user id.i am including post a comment jsp page where i need that particular id.that is why i am asking to put java variable in that param tag – Deep Mar 27 '11 at 04:46
  • Well, probably you've read too many roseindia.net articles? :) – BalusC Mar 27 '11 at 04:48
  • <% String str="prafful"; %> is working .thks for the solution – Deep Mar 27 '11 at 05:04
  • is also working – Deep Mar 27 '11 at 05:09
  • kk thks for ur advice but i am new to EL can you tell me the way to pass the java variable....i am not understanding it at all – Deep Mar 27 '11 at 05:12
  • 1
    Just read JSP/Servlet books/tutorials. Do **not** use roseindia.net. That site is cluttered of bad practices that you don't see the difference between good and bad anymore. – BalusC Mar 27 '11 at 05:14
  • 1
    Hover `[jsp]` tag below your question until a box pops, click `info` link on the popbox. It leads to our JSP wiki page. At the bottom you can find links to good tutorials. – BalusC Mar 27 '11 at 05:17
5

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") %>
bluish
  • 26,356
  • 27
  • 122
  • 180
Saurabh Gokhale
  • 53,625
  • 36
  • 139
  • 164
3

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>
borchvm
  • 3,533
  • 16
  • 44
  • 45
2

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>
Sam
  • 239
  • 2
  • 12
0

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>