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( ...

Get Java Servlet & JSP Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.