Detecting Frame with JSP

Posted on 2004-11-15
Last Modified: 2010-04-01

I am running a JSP Server with Tomcat 4.1
How can I detect a page if it is in a frame (or frameset).

This question should be easy if we using JavaScript code:

if (self != top)
    alert('In a frame');
    alert('Not in a frame');

but I need a pure JSP code, not javascript !

Please help, Thanks so much.
Question by:mnphan
    LVL 7

    Expert Comment

    You can use JS from JSP.

    A JSP is implicitly compiled into a Servlet by the servlet container.   A Servlet will service a request and return back an HTML response stream (which can include CSS, JS, etc).

    So basically  you have :

        JSP -> Servlet -> HTML Document


    Author Comment

    Thanks lhankins,

    You're right,

    I even finish my JSP program in that way. It worked well except it turns out to another problem.

    When I detect a page in a frame using a javascript  and depending on this page if it is in a frame or not I then redirect it to another page, either frame.jsp or no_frame.jsp

    if (self != top)

    but  when I redirect the page using JavaScript, I lost all the informations (error message, log-in status...) in that section.

    I thought, it will be different if I can detect a page in a frame directly using JSP code and then using <jsp:forward page="no/have_frame.jsp"> which I still keep all information  in that section.


    LVL 27

    Accepted Solution

    > I lost all the informations (error message, log-in status...) in that section.
    Where are you storing that data ?
    Could you use session object ?  ( session.setAttribute(String,Object); )
    Could you use query string ? Something like


    Author Comment

    Could you use session object ?  ( session.setAttribute(String,Object); )
    Yes. I using some thing like:
    <jsp:useBean id = "id1" class="ErrorMsg" scope="session"/>
    When I redirect a page using JavaScript window.location.replace('/no_frame.jsp'); I will lost all information in this session.

    > window.location.replace('/no_frame.jsp?p1=1&p2=2');
    It works but I really don't like this because p1 could be contain some values that I don't want to display to user via URL address.


    LVL 27

    Expert Comment

    >When I redirect a page using JavaScript window.location.replace('/no_frame.jsp'); I will lost all information in this session.
    Why is that  ?
    Did you  use
    to test ?
    LVL 4

    Expert Comment

    mnphan - with pure jsp you can not "bust" a frame. It is _just_ a way how a browser displays you html page.


    Author Comment

    Thank you you all,
    and sorry for this late post, as I was unable to post a message at home, I don't know why my PC couldn't see a text box where to type this message.

    rrz, you're right again, when I redirected a page using JavaScript window.location.replace('/no_frame.jsp'); it does not lose any information in that session, every thing was still there, I was wrong because before redirect the page I did get those info and clear them.

    My problem is now solved, I close this topic and give the credit for rrz, who pointed out my error.

    Thanks all
    PS. However, if some one has any idea to detect a frame using with pure JSP, please post. I still believe that JSP can do it without any help from other language !

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Suggested Solutions

    After several days of searching and hunting for limited documentation, I wanted to share this guide to hopefully save someone the hassle of trying to figure this out on their own. I have tested this on Xendesktop 7.1 and PS 4.5 running simultaneous…
    I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now