Recipe 12.3 Creating a New Window with JavaScript in a Servlet


You want a servlet to contain JavaScript that can generate a new browser window.


Use a javax.servlet.RequestDispatcher to include the JavaScript function in the servlet. The JavaScript function calls the JavaScript window object's open method.


This recipe uses the same imported module as the first two recipes, but this time the servlet uses the second function definition ( CreateWindow ) rather than the first. Example 12-4 generates an HTML button widget. When the user clicks the button, JavaScript generates a small window (sometimes referred to as a windoid , or pop up). The servlet dynamically retrieves the URL for loading into the new window from a servlet init parameter, which is something you cannot do with a static HTML page.

Example 12-4. A servlet that loads JavaScript for creating a window
 package com.jspservletcookbook;            import javax.servlet.*; import javax.servlet.http.*; public class WindowServlet extends HttpServlet {        public void doGet(HttpServletRequest request, HttpServletResponse response) throws    ServletException, {  //URL for the pop-up window is configured      String url = getInitParameter("popup-url");  //just in case the initParameter is misconfigured      if (url == null  url.equals(""))          url = "/displayHeaders.jsp";  //add the context path as a prefix to the URL, as in /home      url = request.getContextPath( ) + url;  response.setContentType("text/html"); out = response.getWriter( );       out.println("<html><head>");  RequestDispatcher dispatcher = request.getRequestDispatcher(           "/WEB-INF/javascript/functions.js");       dispatcher.include(request, response);  out.println("<title>Help Page</title></head><body>");       out.println("<h2>Cookie Info</h2>");       out.println("<form action =\"\" onSubmit=\" return false\">");       out.println("<table border=\"0\"><tr><td valign=\"top\">");       out.println(           "Click on the button to get more info on cookies: </td>");       out.println("<td valign=\"top\">");  out.println("<input type=\"button\" name=\"button1\" " +           "value=\"More Info\" onClick=\"CreateWindow('" + url +            "')\"></td></tr>");  out.println("</table></form>");             out.println("</body></html>");      } //end doGet } 

This servlet assumes some configuration steps have been taken in the application's deployment descriptor. This configuration includes an init-param that specifies the URL for loading into the new window. The url variable is the CreateWindow function's parameter (see Example 12-1 for a definition of the JavaScript functions). The servlet generates the HTML and dynamically provides the URL for loading into the new window. Here is the HTML button definition in the servlet's output:

 <input type="button" name="button1" value="More Info"         onClick="CreateWindow('home/cookieReader.jsp')"> 

If the application is dynamically reloadable (the web container monitors the deployment descriptor for any changes and reloads the context if the file is altered ), then the developer can change the value of the servlet init-param in the deployment descriptor. The servlet pop-up window then loads the new URL without recompiling the servlet or stopping the server.

Here is the configuration for this servlet in web.xml :

 <servlet>     <servlet-name>windowservlet</servlet-name>     <servlet-class>com.jspservletcookbook.WindowServlet</servlet-class>     <init-param>         <param-name>popup-url</param-name>         <param-value>/cookieReader.jsp</param-value>     </init-param> </servlet> 

The servlet loads the definition for the JavaScript function CreateWindow with this code:

 RequestDispatcher dispatcher = request.getRequestDispatcher(           "/WEB-INF/javascript/functions.js"); 

In the HTML code the servlet generates, the script tag containing the JavaScript code appears within the HTML head tag. When the user clicks the form button on the servlet-generated web page, a new window is created and the URL specified by the init-param element in web.xml ( cookieReader.jsp ) is loaded. Figure 12-2 shows the servlet output. Figure 12-3 shows the pop-up window.

Figure 12-2. A servlet that creates a pop-up window
Figure 12-3. The new window loads a URL value from a servlet init-param

See Also

The Netscape DevEdge site at; Recipe 12.2, Recipe 12.4, and Recipe 12.6 on using JavaScript with JSPs; Recipe 12.5 on validating form values with a servlet and JavaScript; Recipe 18.3 on using a filter with HTTP requests .

Java Servlet & JSP Cookbook
Java Servlet & JSP Cookbook
ISBN: 0596005725
EAN: 2147483647
Year: 2004
Pages: 326

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: