• Post Reply Bookmark Topic Watch Topic
  • New Topic

variable access in jSP

 
sitaram irrinki
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have declared a variable in A.Jsp. I want to access in B.Jsp How ?, without using any Scope variables or static variables?. If i use
<%jsp:include page = "B.jsp" %> in A.Jsp how to access a variable which i have declared in A.jsp
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your container supports them, you might want to look into tag files.
 
Sushma Sharma
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
I suppose you are using include directive, then you can use variable of A.jsp in B.jsp like any other variable of a jsp file, but do remember to declare the variable in A.jsp before including B.jsp
Also, in this case you will not be able to use B.jsp alone as you will not declare the variable in B.jsp.... if you'll try to do that, you will get an compiler error stating variable not declared...
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sushma Sharma:
hi,
I suppose you are using include directive, then you can use variable of A.jsp in B.jsp like any other variable of a jsp file, but do remember to declare the variable in A.jsp before including B.jsp
Also, in this case you will not be able to use B.jsp alone as you will not declare the variable in B.jsp.... if you'll try to do that, you will get an compiler error stating variable not declared...


Note:
Sushma, correctly said the include directive (<%@ include file=".."%>)
This is an important distinction.
In this case, the code gets merged at compile time so you can use variables declared in page A from included page B.

With the <jsp:include page="... element each page is run and the output is merged at run time. In this case, you can not share variables.
You can, however pass a varible to the included page with the jsp:param element.


Then, the parameter can then be retrieved from the included page using request.getParameter("...").

Again, if your server supports them, you're better off looking into tag files.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!