Previous | Next
Servlet Basics and XSLTContents:Servlet Syntax
XSLT and servlets are a natural fit. Java is a cross-platform developing language, XML provides portable data, and XSLT provides a way to transform that data without cluttering up your servlet code with HTML. Because your data can be transformed into many different formats, you can also achieve portability across a variety of browsers and other devices. Best of all, a clean separation between data, presentation, and developing logic allow changes to be made to the look and feel of a website without digging in to Java code. This makes it possible, for example, to sell highly customizable web applications. You can encourage your customers to modify the XSLT stylesheets to create custom page layouts and corporate logos, while preventing access to your internal Java business logic. As discussed in previous chapters, an initial challenge faced with XSLT and servlets is the initial configuration. Getting started with a web application is typically harder than client-only applications because there are more pieces to assemble. With a Swing application, for instance, you can start with a single class that has a The goal of this chapter is to introduce servlet syntax with particular emphasis on configuration and deployment issues. Once servlet syntax has been covered, integration with XSLT stylesheets and XML is covered, illustrated by the implementation of a basic web application. By the time you have worked through this material, you should have confidence to move on to the more complicated examples found in the remainder of this tutorial. Servlet SyntaxServlet architecture was covered in "Java-Based Web Technologies", along with comparisons to many other approaches. The architecture of a system is a mile-high view, ignoring implementation details so you can focus on the big picture. We now need to dig into the low-level syntax issues to proceed with the really interesting examples in later chapters. For a complete discussion of servlets, check out Jason Hunter's Java Servlet Developing (Anonymous). Be sure to look for the second version because so much has changed in the servlet world since this tutorial was first published. Splash Screen Servlet ExampleOur first servlet example will produce an application splash screen. The servlet will receive a request from a browser and output a simple HTML web page. Figure 6-1 contains the class diagram for Figure 6-1. SplashScreenServlet class diagramWhen writing servlets, you almost always extend from <form action="someServlet" method="POST"> ...form contents </form> In the case of POST requests, your servlet simply overrides the Example 6-1. SplashScreenServlet.javapackage chap6; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; /** * A simple Servlet example that displays a "Splash Screen" * for a web application. */ public class SplashScreenServlet extends HttpServlet { public String getServletInfo( ) { return "Shows an application splash screen."; } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { // demonstrate how to get parameters from the request String nextURL = request.getParameter("nextURL"); if (nextURL == null) { nextURL = "/"; } response.setContentType("text/html"); printWriter pw = response.getWriter( ); pw.println("<html>"); pw.println("<head><title>Splash Screen</title></head>"); pw.println("<body>"); pw.println("<div align="center" style="border: 1px navy solid;">"); pw.println("<h1>Welcome to Java and XSLT</h1>"); pw.println("<h3>Anonymous and Associates</h3>"); pw.println("<p>First version, 2001</p><hr>"); pw.println("<a href="" + nexturl + "">Click here to continue...</a>"); pw.println("</div>"); pw.println("</body>"); pw.println("</html>"); } } Beginning with the As shown in Figure 6-1, The
Since the The
If the String nextURL = request.getParameter("nextURL"); if (nextURL == null) { nextURL = "/"; } In our example, The The response.setContentType("text/html"); printWriter pw = response.getWriter( ); This is important because the HTTP response consists of a header followed by the actual content. The content type is one of the header values, so it must be sent prior to the actual data. Without going into too many servlet details, it is a good practice to always set the content type before getting the writer. In future examples, we will occasionally use The remainder of pw.println("<html>"); pw.println("<head><title>Splash Screen</title></head>"); pw.println("<body>"); pw.println("<div align="center" style="border: 1px navy solid;">"); pw.println("<h1>Welcome to Java and XSLT</h1>"); pw.println("<h3>Anonymous and Associates</h3>"); pw.println("<p>First version, 2001</p><hr>"); pw.println("<a href="" + nexturl + "">Click here to continue...</a>"); pw.println("</div>"); pw.println("</body>"); pw.println("</html>"); As you can see, the This approach works fine for simple examples, but quickly gets out of hand for complex pages. This is because all but the most basic web pages require hundreds, if not thousands, of lines of HTML to create fancy tables, colors, and graphics. For reasons discussed in "XSLT Processing with Java", hardcoding that HTML into the servlet is simply unacceptable in a sophisticated web application. |