Download
FAQ History |
![]() ![]() ![]() |
API
Search Feedback |
Java Servlet Technology
As noted in the previous section, Java Servlet Technology (page 613) is the foundation technology for all interactive Web applications whose user interfaces is generated on the server.
Java Servlet technology consists of two parts:
- A Java programming language API that encapsulates requests and responses and their subobjects and a process for handling these objects.
- A declarative mechanism for specifying Web application properties outside the Web application code and which can be modified at deployment time. See Configuring Web Applications (page 82) for an introduction to this aspect of Java Servlet technology.
A servlet is a Java programming language class that dynamically processes requests and constructs responses. Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by Web servers. For such applications, Java Servlet technology defines HTTP-specific servlet classes.
The main methods in an HTTP servlet process the standard HTTP requests: GET and PUT. Here's an example of a servlet that allows the user to input their name into a form, and then calls another servlet to generate a greeting response:
public class GreetingServlet extends HttpServlet { public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Set response headers response.setContentType("text/html"); response.setBufferSize(8192); PrintWriter out = response.getWriter(); // then write the response out.println("<html>" + "<head><title>Hello</title></head>"); out.println("<body bgcolor=\"#ffffff\">" + "<img src=\"duke.waving.gif\">" + "<h2>Hello, my name is Duke. What's yours?</h2>" + "<form method=\"get\">" + "<input type=\"text\" name=\"username\" size=\"25\">" + "<p></p>" + "<input type=\"submit\" value=\"Submit\">" + "<input type=\"reset\" value=\"Reset\">" + "</form>"); UserNameBean userNameBean = new UserNameBean(); userNameBean.setName(request.getParameter("username")); if ( userNameBean.getName() != null && userNameBean.getName().length() > 0 ) { RequestDispatcher dispatcher = getServletContext().getRequestDispatcher( "/response"); if (dispatcher != null) dispatcher.include(request, response); } out.println("</body></html>"); out.close(); } }As you can see from this example, the one limitation of servlets is in generating responses whose main content is static text, such as HTML markup. Since the response is wholly generated within a Java class, the text must be embedded within
println
statements that write to the response writer object. Not only is this difficult to maintain, but forces the content developer to be a Java programmer. Conversely, when the content is binary, for example an image, servlets are well suited to the task. Servlets are also well suited to performing control functions because the full capabilities of the Java programming language are available. In fact, servlets often serve as the Controller in Web applications that employ an MVC architecture.
Download
FAQ History |
![]() ![]() ![]() |
API
Search Feedback |
All of the material in The Java(TM) Web Services Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.