O'Reilly logo

Java Servlet & JSP Cookbook by Bruce W. Perry

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 12. Integrating JavaScript with Servlets and JSPs

Introduction

JavaScript is a scripting system for web pages, standalone applications, and servers. Netscape Corporation invented JavaScript, which has become such a popular and useful programming tool that all major browsers now support it. Unlike the Java code shown in this book, JavaScript is mainly executed in the web browser as a client-side scripting system, rather than on the server.

Most busy web sites use JavaScript for dynamic behavior, such as validating form input or creating new browser windows (much to the chagrin of users, who are often overwhelmed by irresponsible and dynamically generated pop ups!). Just choose “View Source” from the browser menu bar for a typical web page, and often the first text items you’ll see displayed are endless lines of JavaScript. JavaScript is used for advanced tasks such as controlling or animating browser shapes (dynamic HTML), creating flying objects, and initializing the behavior of embedded videos.

Developers converting static web pages to JSPs or servlets may have to integrate existing JavaScript code into their Java source code. This is what the upcoming recipes are all about.

Tip

JavaScript guides are available from http://devedge.netscape.com/.

12.1. Including JavaScript Modules in a Servlet

Problem

You want to import a module or file containing JavaScript code so that the JavaScript can be included in the servlet’s HTML output.

Solution

Use the javax.servlet.RequestDispatch.include( ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required