0

I'm unable to pass variables from a.jsp to b.jsp, following is what I tried and the output. Can someone please help? Thanks.

a.jsp

start including
<jsp:include page="b.jsp">
<jsp:param name="somevar" value="zzzz" />
</jsp:include>
 stop including

b.jsp

${somevar}
${param.somevar}
<%=request.getParameter("somevar")%>

output

start including


null
stop including
hixhix
  • 771
  • 7
  • 23

2 Answers2

1

Try the following code:

home.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page session="false"%>
<html>
<head>
<title>Home</title>
</head>
<body>
    <h1>Hello world!</h1>

    <jsp:include page="b.jsp">
        <jsp:param name="something" value="something" />
    </jsp:include>

    <h1>Bye World</h1>
</body>
</html>

In the b.jsp page use the following code.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSP Param example</title>
</head>
<body>
    <h1>
        <c:out value="${param.something}" />
    </h1>
</body>
</html>

The output must be

Hello world!
something 
Bye World

Try to avoid java code in JSP pages as it is not advisable. For more details, have a look at this answer How to avoid Java Code in JSP-Files?

Community
  • 1
  • 1
Sai Upadhyayula
  • 2,404
  • 3
  • 23
  • 35
0

You have to set your param before include.

start including
<jsp:params>
    <jsp:param name="somevar" value="zzzz" />
</jsp:params>

<jsp:include page="b.jsp">
</jsp:include>
stop including

And can u try this in b.jsp:

<%=request.getParameter("somevar");%>

What is output?

hurricane
  • 6,521
  • 2
  • 34
  • 44
  • I got a compile error " The jsp:param action must not be used outside the jsp:include, jsp:forward, or jsp:params elements" with that. – hixhix Jan 28 '15 at 22:37