Java Reference
In-Depth Information
submit a request, and wait for the response. When that request is complete, the
server will send back a different downstream user interface. We must also apply
sound model-view separation to this downstream interface! Unfortunately, many
programmers usually focus on the cleanly separated upstream HTML view,
missing the generated downstream interface. The servlet becomes a tangled
mass of model and downstream view. This is where the rattlesnake is hiding.
3.2.1
Can we use servlets as the model?
We now have a clear evolution from the Magic Pushbutton antipattern to
present implementations. Instead of hanging an interactive 10 KB script off a
pushbutton, we hang a 10 KB servlet off a Submit button, with all of the code
in a single service method, as in figure 3.3. To review, a servlet is a server-side,
long-running Java program that conforms to an open application program-
ming interface ( API ). Servlets provide a server-side interface to a service.
When we click a Submit button on an HTML page, if the back end is written
in Java, we are likely calling some form of a servlet first. Servlets take advan-
tage of the HTTP GET and POST interfaces. The user invokes the servlet
through one of these commands.
10 KB Servlet
Figure 3.3
The Magic Servlet is perhaps the most common server-side Java
antipattern. Programmers assume that the separation between HTML
and Java servlet code represents a clean separation between user
interface and business logic. For many reasons, such is not the case.
Submit
The typical servlet runs and prints some HTML that is returned to the client
when the request is complete. The heart of the antipattern is here, with the
downstream user interface. Figure 3.4 shows the architecture for a servlet and a
request for a servlet on most commercial web application server architectures.
Listing 3.3 is an example of a servlet that prints “Hello, World” to the user.
Listing 3.3
“Hello, World,” the traditional first Java server-side program
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
B helloWorld class
C HTTP GET interface
public class HelloWorld extends HttpServlet
{
public void doGet(HttpServletRequest req,
Search WWH ::




Custom Search