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

64 lines
2.1 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>Get the HTTP request information</title>
</head>
<BODY>
<!-- Java sync-link -->
<SCRIPT LANGUAGE="Javascript" SRC="../../../rzahg/synch.js" TYPE="text/javascript"></SCRIPT>
<h6><A NAME="servreq"></A>Get the HTTP request information</h6>
<p>After the method declaration, add the request code (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
{
Enumeration keys;
String key;
String myName = &quot;&quot;;
keys = request.getParameterNames();
while (keys.hasMoreElements())
{
key = (String) keys.nextElement();
if (key.equalsIgnoreCase(&quot;myName&quot;)) myName = request.getParameter(key);
}
System.out.println(&quot;Name = &quot;);
if (myName == &quot;&quot;) myName = &quot;Hello&quot;;
}
}
</pre>
<p>This new section of code handles the client request parameters. The HttpServletRequest class has the following specific methods to retrieve information provided by the client:</p>
<ul>
<li><p><strong>getParameterNames()</strong><br>
This method returns the names of the parameters in an enumeration of strings.</p></li>
<li><p><strong>getParameterValues()</strong><br>
This method returns the values of the parameters in an array of strings.</p></li>
<li><p><strong>getParameter()</strong><br>
This method returns a single parameter value as a string. Use this method when you know the client request returns only one parameter.</p></li>
</ul>
<p>In the case of ServletSample, the getParameterNames() method gets the name of the parameters, and the getParameter() method gets the value of the myName parameter.</p>
</body>
</html>