ibm-information-center/dist/eclipse/plugins/i5OS.ic.rzatz_5.4.0.1/51/program/servmeth.htm

64 lines
2.7 KiB
HTML
Raw Normal View History

2024-04-02 14:02:31 +00:00
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
<LINK rel="stylesheet" type="text/css" href="../../../rzahg/ic.css">
<title>Write the required servlet methods </title>
</head>
<BODY>
<!-- Java sync-link -->
<SCRIPT LANGUAGE="Javascript" SRC="../../../rzahg/synch.js" TYPE="text/javascript"></SCRIPT>
<h6><A NAME="servmeth"></A>Write the required servlet methods </h6>
<p>After the class declaration, add the method declaration (in black text below):</p>
<pre>import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ServletSample extends HttpServlet
{
public void doGet (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
}
}
</pre>
<p>The important parts of the above code snippet are:</p>
<ul>
<li><p><strong>The doGet() method</strong><br>
Classes that extend the HttpServlet class should override at least one method of the HttpServlet class. SimpleServlet overrides the doGet() method.</p>
<p><strong>Note:</strong> Since doGet and doPost throw two exceptions (javax.servlet.ServletException and java.io.IOException), you must include them in the declaration.</p>
</li>
<li><p><strong>HttpServletRequest and HttpServletResponse</strong><br>
When the server calls an HTTP servlet's service() method, it passes the following two objects as parameters:</p>
<ul>
<li><p><strong>HttpServletRequest: the request object</strong><br>
HttpServletRequest represents a client's request. This object gives a servlet access to incoming information such as HTML form data and HTTP request headers.</p></li>
<li><p><strong>HttpServletResponse: the response object</strong><br>
HttpServletResponse represents the servlet's response. The servlet uses this object to return data to the client such as HTTP errors (200, 404, and others), response headers (Content-Type, Set-Cookie, and others), and output data by writing to the response's output stream or output writer.</p></li>
</ul>
</li>
</ul>
<p>The servlet communicates with the HTTP server and ultimately with the client through these objects. The servlet can invoke the HttpServletRequest object's (request) methods to get information about the client environment, the HTTP server environment, and any information provided by the client (for example, HTML form information set by GET or POST).</p>
<p>The servlet invokes the HttpResponse object's (response) methods to send the response that it has prepared back to the client. These same objects are also passed to the service() method.</p>
</body>
</html>