Declaring a variable in one JSP file and accessing it in another JSP file

A

anubhakhurana

I am new to JSPs and am actually a core java programmer. I have a
problem. I have different jsp files and all these jsp files should be
including a header.jsp. I want to display different title in this
header.jsp for each of the above files, I mentioned. The way I thought
was that I'll have a variable called pageTitle in all the above jsp
files and access this variable in header.jsp. But I am not successful
uptil now.
 
M

Manish Pandit

?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

I am new to JSPs and am actually a core java programmer. I have a
problem. I have different jsp files and all these jsp files should be
including a header.jsp. I want to display different title in this
header.jsp for each of the above files, I mentioned. The way I thought
was that I'll have a variable called pageTitle in all the above jsp
files and access this variable in header.jsp. But I am not successful
uptil now.

page.jsp

<%
String pagename = "example page";
%>
<%@ include file="hdr.jsp" %>
Bla bla bla

hdr.jsp

I am <%=pagename%>.
<hr>

works here.

I do not believe that you can do it with the jsp:include
tag.

And in general I think you are on the wrong track - this is
not the JSP way of doing things.

Arne
 
C

Chris

I am new to JSPs and am actually a core java programmer. I have a
problem. I have different jsp files and all these jsp files should be
including a header.jsp. I want to display different title in this
header.jsp for each of the above files, I mentioned. The way I thought
was that I'll have a variable called pageTitle in all the above jsp
files and access this variable in header.jsp. But I am not successful
uptil now.

myjsp.jsp:

<html>
<%
String title = "A Title";
%>
<%@ include file = "includes/header.jsp" %>

Rest of the page here

</html>

Reference <%=title%> in your header.jsp. You can set title to a
different value in each page that includes the header.
 
A

anubhakhurana

Thanks to all who replied. This was of great help and got many other
pointers too through your replies. Thanks a ton.

Also Arne,

Can you please elaborate on what you exactly mean by "you are on the
wrong track - this is
not the JSP way of doing things". This might help me to have a
different point of view.

-Anubha
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Can you please elaborate on what you exactly mean by "you are on the
wrong track - this is
not the JSP way of doing things". This might help me to have a
different point of view.

You should be looking at:
- having requests going to a controller servlet
that sets various info in request and forward
to a JSP page
- having no embedded Java code in the JSP code
and only using taglibs
etc.etc.

Arne
 
M

Manish Pandit

Another way to do it is with jsp:include

In parent.jsp, do this:

<jsp:include file='header.jsp>
<jsp:param name="title" value="some text"/>
</jsp:include>

In header.jsp, do this:

Title is ${param.title}.

-cheers,
Manish
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top