Datasheet
Patrick c01.tex V3 - 09/18/2009 12:15pm Page 28
Chapter 1: Building Web Applications in WebLogic
<servlet-name>SimpleExcelServlet</servlet-name>
<url-pattern>/simpleexcel</url-pattern>
</servlet-mapping>
As noted earlier, WebLogic Server supports a
@WLServet
annotation for registering servlets and
specifying URL patterns. In this case, the
SimpleExcelServlet
source file could include the following
@WLServlet
annotation to eliminate the need for
web.xml
entries:
@WLServlet (
name = "SimpleExcelServlet",
mapping = {"/simpleexcel"}
)
public class SimpleExcelServlet extends HttpServlet
{
...
}
In both registration approaches, users accessing the
/simpleexcel
location will be presented with a
spreadsheet embedded in their browser window. The servlet may also be registered for a
<url-pattern>
that includes an
.xls
file extension to assist the users by providing a suitable default file name and type
if they choose to use
Save As...
from within the browser:
<servlet-mapping>
<servlet-name>SimpleExcelServlet</servlet-name>
<url-pattern>/multitable.xls</url-pattern>
</servlet-mapping>
Simple tab- and newline-based formatting may be sufficient in many cases, but you can achieve addi-
tional control by building HTML tables and using HTML formatting options such as
<b>
and
<i>
in the
generated output. Because the content type was specified as
ms-excel
, these HTML tags are interpreted
by the browser and spreadsheet application as equivalent spreadsheet formatting options.
The
FancyExcelServlet
example servlet in Listing 1-5 builds the same multiplication table as
SimpleExcelServlet
but uses HTML to control formats and cell sizes.
Listing 1-5: FancyExcelServlet.java.
package professional.weblogic.ch01.example3;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class FancyExcelServlet extends HttpServlet
{
public static final String CONTENT_TYPE_EXCEL =
"application/vnd.ms-excel";
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException
28