• Post Reply Bookmark Topic Watch Topic
  • New Topic

including jsp within jsp

 
arpit singla
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my1.jsp includes my2.jsp. I need to include my2.jsp if certain parameters are true(which i need to check in my1.jsp) which are passed from a servlet to my2.jsp and then need to pass to my1.jsp. How can i check those variables in my1.jsp.
class MyServlet
{
// passing the value of myvar to my2.jsp
req.setAttribute("myvar",myvar);
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(my2.jsp);
}
my2.jsp
// retieving the value of myvar in my2.jsp
String myvar = (String)request.getAttribute("myvar");
my1.jsp
if(myvar) // how can i pass the value of myvar to this jsp
<%@ include file="my2.jsp" %>
thanks for the help
arpit
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you include (or forward) jsp2 then they share the same request and response objects. ie jsp2 should be able to read the Attribute as well.
Dave.
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think, if I'm reading it right, you can't do this the way you've coded.

You are setting a value in jsp2, which you use from jsp1, to determine if you're supposed to include jsp2 or not?

You should pass it to jsp1 first, then *if* you need to include jsp2, jsp2 can be dynamically included, and it will automatically have access to the same request object (and thus, the value you are checking) as jsp1.

Did that make sense?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!