BUY THIS BOOK
Add to Cart

Print Book $44.95


Add to Cart

Print+PDF $58.44

Add to Cart

PDF $35.99

Safari Books Online

What is this?

Add to UK Cart

Print Book £31.95

What is this?

Looking to Reprint or License this content?


Java Servlet & JSP Cookbook
Java Servlet & JSP Cookbook

By Bruce W. Perry
Book Price: $44.95 USD
£31.95 GBP
PDF Price: $35.99

Cover | Table of Contents | Colophon


Table of Contents

Chapter 1: Writing Servlets and JSPs
The purpose of this chapter is to bring relative newcomers up to speed in writing, compiling, and packaging servlets and JSPs. If you have never developed a servlet or JSP before, or just need to brush up on the technology to jumpstart your development, then the upcoming recipes provide simple programming examples and an overview of the components that you require on the user classpath to compile servlets.
Recipe 1.1 and Recipe 1.2 provide a brief introduction to servlets and JSPs, respectively. A comprehensive description of a servlet or JSP's role in the Java 2 Platform, Enterprise Edition (J2EE), is beyond the scope of these recipes. However, information that relates directly to J2EE technology, such as databases and JDBC; using servlets with the Java Naming and Directory Interface (JNDI); and using servlets with JavaMail (or email) is distributed throughout the book (and index!).
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Introduction
The purpose of this chapter is to bring relative newcomers up to speed in writing, compiling, and packaging servlets and JSPs. If you have never developed a servlet or JSP before, or just need to brush up on the technology to jumpstart your development, then the upcoming recipes provide simple programming examples and an overview of the components that you require on the user classpath to compile servlets.
Recipe 1.1 and Recipe 1.2 provide a brief introduction to servlets and JSPs, respectively. A comprehensive description of a servlet or JSP's role in the Java 2 Platform, Enterprise Edition (J2EE), is beyond the scope of these recipes. However, information that relates directly to J2EE technology, such as databases and JDBC; using servlets with the Java Naming and Directory Interface (JNDI); and using servlets with JavaMail (or email) is distributed throughout the book (and index!).
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Writing a Servlet
You want to write a servlet that is part of a web application.
Create a Java class that extends javax.servlet.http.HttpServlet. Make sure to import the classes from servlet.jar (or servlet-api.jar)—you'll need them to compile the servlet.
A servlet is a Java class that is designed to respond with dynamic content to client requests over a network. If you are familiar with Common Gateway Interface (CGI) programs, then servlets are a Java technology that can replace CGI programs. Often called a web component (along with JSPs), a servlet is executed within a runtime environment provided by a
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Writing a JSP
You want to create a JSP and include it in a web application.
Create the JSP as a text file using HTML template text as needed. Store the JSP file at the top level of the web application.
A JavaServer Pages (JSP) component is a type of Java servlet that is designed to fulfill the role of a user interface for a Java web application. Web developers write JSPs as text files that combine HTML or XHTML code, XML elements, and embedded JSP actions and commands. JSPs were originally designed around the model of embedded server-side scripting tools such as Microsoft Corporation's ASP technology; however, JSPs have evolved to focus on XML elements, including custom-designed elements, or
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Compiling a Servlet
You have written a servlet, and now you want to compile it into a class file.
Make sure that servlet.jar (for Tomcat 4.1.24) or servlet-api.jar (for Tomcat 5) is on your user classpath. Use javac as you would for any other Java source file.
At a minimum, you have to place the servlet classes on your classpath in order to compile a servlet. These classes are located in these Java packages:
  • javax.servlet
  • javax.servlet.http
Tomcat 5 supports the servlet API 2.4; the JAR file that you need on the classpath is located at <Tomcat-5-installation-directory>/common/lib/servlet-api.jar
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Packaging Servlets and JSPs
You want to set up a directory structure for packaging and creating a Web ARchive (WAR) file for servlets and JSPs.
Set up a directory structure in your filesystem, then use the jar tool or Ant to create the WAR.
Except in the rarest of circumstances, you'll usually develop a servlet or JSP as part of a web application. It is relatively easy to set up a directory structure on your filesystem to hold web-application components, which include HTML files, servlets, JSPs, graphics, JAR libraries, possibly movies and sound files, as well as XML configuration files (such as the deployment descriptor; see Recipe 1.5).
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Creating the Deployment Descriptor
You want to create the deployment descriptor for your application.
Name the XML file web.xml and place it in the WEB-INF directory of your web application. If you do not have an existing example of web.xml, then cut and paste the examples given in the servlet v2.3 or 2.4 specifications and start from there.
The deployment descriptor is a very important part of your web application. It conveys the requirements for your web application in a concise format that is readable by most XML editors. The web.xml file is where you:
  • Register and create URL mappings for your servlets
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Chapter 2: Deploying Servlets and JSPs
This chapter describes how to take servlets or Java Server Pages (JSPs) and make them available to receive web requests on Tomcat's servlet container or BEA WebLogic Server 7.0. This discussion begins with deploying servlets and JSPs; in other words, getting them running on Tomcat or WebLogic, either alone or as part of a web application.
Developing and compiling a servlet or JSP within an integrated development environment (IDE) is one thing. Having the web component respond to HTTP requests is another. This is what deployment is all about with web-related software: placing the software into service within a web container like Tomcat or an application server such as BEA WebLogic Server 7.0. The following recipes detail deployment of servlets and JSPs on these web containers, first individually, and then as part of a web application.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Introduction
This chapter describes how to take servlets or Java Server Pages (JSPs) and make them available to receive web requests on Tomcat's servlet container or BEA WebLogic Server 7.0. This discussion begins with deploying servlets and JSPs; in other words, getting them running on Tomcat or WebLogic, either alone or as part of a web application.
Developing and compiling a servlet or JSP within an integrated development environment (IDE) is one thing. Having the web component respond to HTTP requests is another. This is what deployment is all about with web-related software: placing the software into service within a web container like Tomcat or an application server such as BEA WebLogic Server 7.0. The following recipes detail deployment of servlets and JSPs on these web containers, first individually, and then as part of a web application.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Deploying an Individual Servlet on Tomcat
You want to take a compiled servlet and install it in Tomcat to find out if it is working. You are doing a preliminary test and do not want to take the time to build a complete web application for the servlet.
Copy and paste the class file into Tomcat's default web application (or into a web application that you have already installed), then request it using the invoker servlet. Or use an Ant build.xml file to move the file temporarily into the Tomcat default web application.
Sometimes you design a servlet and are anxious to see if the servlet works. Unless the servlet depends on other servlets or components in the application, you can test it on Tomcat by pasting the class file (including its package-related directories) into the default Tomcat web application. By default, this application is located at the path
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Using a Context Element in Tomcat'sserver.xml
You want to deploy and redeploy a servlet on Tomcat 4.1.x without restarting the Tomcat web container.
Deploy the servlet as part of a Context element in Tomcat's server.xml file.
You can paste a recompiled servlet class over an existing servlet class and invoke the servlet without restarting Tomcat:
  1. Locate the Context element for your web application or create a new Context element in the
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Deploying an Individual Servlet on WebLogic
You want to take your compiled servlet and install it in BEA WebLogic Server 7.0 to find out if it is working.
Copy and paste the class file into WebLogic's default web application (or into a web application that you have already installed). Use the WebLogic Administration Console to alter the web.xml file and give the servlet a sensible name with which to request it in a browser, or use an Ant build file to move the file temporarily into the WebLogic default web application.
WebLogic 7.0's default web application is located on the following path: <WebLogic-installation-directory>/user_projects/<mydomain>/applications/DefaultWebApp. In the default installation of the WebLogic 7.0 server, not much exists in the default web application but a
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Deploying an Individual JSP on Tomcat
You want to place a JSP file into a web application.
Copy the new or revised JSP file into the top-level directory of the default Tomcat web application or of another deployed web application.
The easiest way to test a new JSP file is to place it at the top level of Tomcat's default web application. This application is located in the <Tomcat-installation-directory>/webapps/ROOT/ directory. Tomcat 4.1.x compiles (or recompiles, if you are pasting a new JSP file over an old one) the JSP and display its response in a web page. You do not have to stop and start Tomcat using the Tomcat manager application for the new JSP file to be available to your web application.
Placing a JSP file in a deployed web application will
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Deploying an Individual JSP on WebLogic
You want to quickly test a JSP without deploying it as part of a new web application.
Copy and paste the JSP into the top-level directory of BEA WebLogic Server 7.0's default web application, then request the JSP in a browser.
A JSP file can be "hot deployed" on WebLogic 7.0's default web application without having to redeploy the entire web application. This default web application is located at <WebLogic-installation-directory>/user_projects/<name-of-your-domain>/applications/DefaultWebApp. If you paste your JSP file into this directory (DefaultWebApp), it will be available to receive requests without redeploying the default web application. If your JSP file is named newfile.jsp, then the URL for requests to this page would be
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Deploying a Web Application on Tomcat
You want to deploy an entire web application on Tomcat 4.1.x.
Create a Jakarta Ant build file. Ant can automatically compile your servlet classes, create a web application archive (.war) file, then deploy the WAR to the Tomcat 4.1.x server.
The recommended method for the compilation and deployment of web applications is to use the Jakarta Ant automation tool. If you change anything in the application (such as altering a servlet or JSP), then all it takes is a single command-line execution of ant to compile, package, and redeploy the application on Tomcat. You do not have to go to the trouble of manually recompiling a changed servlet, creating a new WAR file, starting and stopping Tomcat, and redeploying the application.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Deploying a Web Application on WebLogic Using Ant
You want to deploy a web application on WebLogic Server 7.0 using Jakarta Ant.
Create a Jakarta Ant build file. Ant can automatically compile your servlet classes, create a web-application archive (.war) file, and then deploy the WAR to WebLogic Server 7.0.
You can either manually cut and paste web components into the WebLogic applications directory (as described in the sidebar), or use Ant to automate the process of compiling, generating a WAR file, and copying the WAR to this directory. An example directory path to applications is k:\bea\user_projects\bwpdomain\applications. This method would entail a minor edit of the
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Using the WebLogic Administration Console
You want to deploy a web application using WebLogic's Administration Console.
Bring up the Administration Console in your web browser and use its graphical interface to deploy either a WAR file or a web-application directory.
The WebLogic Administration Console is a servlet- and browser-based tool for managing WebLogic server resources and Java 2 Enterprise Edition (J2EE) applications. To use the Console, WebLogic Server must be running. First, request the URL http://localhost:7001/console (or whichever your server address and port is, as in http://<weblogic-server-address>:<port>/console). Then enter your login name and password to gain entry to the browser-based tool. The resulting screen looks like Figure 2-6, with a hierarchical list of choices in the lefthand column and the current screen choice in the righthand column.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Using WebLogic Builder to Deploy a Web Application