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

12.4. Creating a New Window with JavaScript in a JSP

Problem

You want to use JavaScript in a JSP to create a new browser window.

Solution

Use the c:import JSTL tag to import the JavaScript code into the JSP. Then use the initParam JSTL implicit object to dynamically provide the URL for a JavaScript-generated window.

Discussion

The JSP in Example 12-5 (windowJ.jsp) uses the JSTL's c:import core tag to import the JavaScript function definition for creating a new window. The JSP then calls the JavaScript function (CreateWindow) in the onClick event handler for a web page button. The CreateWindow function loads the URL specified in its parameter into the new browser window. Example 12-5 uses the c:out core tag and EL syntax to dynamically acquire the URL for the JavaScript window from a context parameter. The c:out tag looks like this:

<c:out value=
"${pageContext.request.contextPath}${initParam[\"jsp-url\"]}"/>

The value attribute specifies two EL expressions. The first one provides the JSP's context path, while the second gives the value of the context-param element jsp-url. The full URL specified by these concatenated EL expressions is /home/cookieReader.jsp.

Example 12-5. Using the JSTL to import JavaScript into a JSP

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<html>
<head>

<c:import url="/WEB-INF/javascript/functions.js" /> <title>Help Page</title></head><body> <h2>Cookie Info</h2> <form action ="" onSubmit=" return false"> <table border="0"><tr><td valign="top"> Click ...

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