Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Declaring a variable in one JSP file and accessing it in another JSP file

Reply
Thread Tools

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

 
 
anubhakhurana@gmail.com
Guest
Posts: n/a
 
      10-09-2006
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.

 
Reply With Quote
 
 
 
 
Manish Pandit
Guest
Posts: n/a
 
      10-09-2006
Hi,

Looks like JSP tag files might be your best bet. They are kind of like
include, but can handle attributes. If all you have to do is change the
title, it can be passed as an attribute to the tag file. If you want to
add more customization, you might have to consider using tag handlers -
they can contain complex java code.

http://today.java.net/pub/a/today/20.../tagfiles.html

http://java.sun.com/j2ee/1.4/docs/tu.../JSPTags5.html

-cheers,
Manish

 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Arne_Vajh=F8j?=
Guest
Posts: n/a
 
      10-09-2006
wrote:
> 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
 
Reply With Quote
 
Chris
Guest
Posts: n/a
 
      10-09-2006
wrote:
> 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.
 
Reply With Quote
 
=?ISO-8859-1?Q?Kristian_Lier_Seln=E6s?=
Guest
Posts: n/a
 
      10-10-2006
You could check out something like Struts tiles as well,
http://struts.apache.org/1.x/struts-tiles/index.html (not tied to Struts
btw).

-Kristian


wrote:
> 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.
>

 
Reply With Quote
 
anubhakhurana@gmail.com
Guest
Posts: n/a
 
      10-10-2006
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

Arne Vajhøj wrote:
> 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


 
Reply With Quote
 
=?ISO-8859-1?Q?Arne_Vajh=F8j?=
Guest
Posts: n/a
 
      10-10-2006
wrote:
> 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
 
Reply With Quote
 
Manish Pandit
Guest
Posts: n/a
 
      10-11-2006
Another way to do it is with jsp:include

In parent.jsp, do this:

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

In header.jsp, do this:

Title is ${param.title}.

-cheers,
Manish

 
Reply With Quote
 
Manish Pandit
Guest
Posts: n/a
 
      10-11-2006

> <jsp:include file='header.jsp>


sorry - please read <jsp:include page="header.jsp">

-cheers,
Manish

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Newbie Q. Accessing a variable in one method from another. Mike Barnard Java 7 06-23-2010 08:43 PM
Proto-Functions: Declaring in one file, using in another? Hal Vaughan C++ 2 03-14-2008 12:34 PM
How to Pass session parameters from one jsp to another jsp sridhar kumar ch Java 1 06-28-2006 05:28 PM
How to Pass session parameters from one jsp to another jsp sridhar kumar ch Java 0 06-28-2006 11:16 AM
Using One XSLT and multiple XML Problem (One is XML and another one is XBRL) loveNUNO XML 2 11-20-2003 06:47 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57