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

Discussion in 'Java' started by anubhakhurana@gmail.com, Oct 9, 2006.

  1. Guest

    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.
     
    , Oct 9, 2006
    #1
    1. Advertising

  2. Manish Pandit, Oct 9, 2006
    #2
    1. Advertising

  3. Re: Declaring a variable in one JSP file and accessing it in anotherJSP file

    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
     
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Oct 10, 2006
    #3
  4. Chris Guest

    Re: Declaring a variable in one JSP file and accessing it in anotherJSP file

    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.
     
    Chris, Oct 10, 2006
    #4
  5. Re: Declaring a variable in one JSP file and accessing it in anotherJSP file

    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.
    >
     
    =?ISO-8859-1?Q?Kristian_Lier_Seln=E6s?=, Oct 10, 2006
    #5
  6. Guest

    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
     
    , Oct 10, 2006
    #6
  7. Re: Declaring a variable in one JSP file and accessing it in anotherJSP file

    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
     
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Oct 11, 2006
    #7
  8. 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
     
    Manish Pandit, Oct 11, 2006
    #8

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


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

    -cheers,
    Manish
     
    Manish Pandit, Oct 11, 2006
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. loveNUNO
    Replies:
    2
    Views:
    972
    loveNUNO
    Nov 20, 2003
  2. sridhar kumar ch
    Replies:
    1
    Views:
    8,773
  3. sridhar kumar ch
    Replies:
    0
    Views:
    834
    sridhar kumar ch
    Jun 28, 2006
  4. Hal Vaughan
    Replies:
    2
    Views:
    356
    James Kanze
    Mar 14, 2008
  5. Mike Barnard
    Replies:
    7
    Views:
    314
Loading...

Share This Page