Ebedding JavaScript or printing it to jsp/html page through Java

Posted on 2005-05-17
Last Modified: 2010-03-31
Hi Experts,

I would like to find out an efficient / appropriate way to display JavaScript in a jsp page; the JavaScript changes based on values in my Java code.  For now I've been using the out.println, as show bellow, to put into the jsp page but I'm not sure if that's how its supposed to be done.  Any recommendations or words of wizdom?

              switch ($nFrame) {
                        case 1:      // Top frame
                            global.out.println("<title>"+ frameTitle +"</title>");
                            global.out.println("<script language=\"JavaScript\" type=\"text/JavaScript\">");
                            global.out.println("function TabClick(href, target) {");
                            global.out.println("if (window.parent.parent.NewPageLoading == \"YES\") {");
                                global.out.println("alert('ANOTHER PAGE IS CURRENTLY BEING LOADED. PLEASE TRY AGAIN AFTER THIS LOADING SESSION HAS ENDED.')");
Question by:kasiencja
    1 Comment
    LVL 29

    Accepted Solution

    Is this in a servlet or a JSP?

    it looks more like a servlet as you are embedding HTML in Java code. In a JSP you should embed the Java code in the HTML.

    //blah, some processing
    <script language="JavaScript" type="text/JavaScript">
     switch ($nFrame) {
      case 1:     // Top frame
        alert('AN ALERT');
     break;case 2:
        alert('ANOTHER ALERT');
    break ;default:
        alert('DEFAULT ALERT');
    //blah, rest of code


    Featured Post

    Courses: Start Training Online With Pros, Today

    Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    changePi Challenge 15 57
    nestparen challenge 4 37
    wordcount challenge 11 51
    java  and programming certification ? 4 35
    INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

    746 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

    16 Experts available now in Live!

    Get 1:1 Help Now