64 lines
2.2 KiB
HTML
64 lines
2.2 KiB
HTML
|
<!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><font color="#808080" class="code">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
|
||
|
{</font>
|
||
|
|
||
|
Enumeration keys;
|
||
|
String key;
|
||
|
String myName = "";
|
||
|
keys = request.getParameterNames();
|
||
|
while (keys.hasMoreElements())
|
||
|
{
|
||
|
key = (String) keys.nextElement();
|
||
|
if (key.equalsIgnoreCase("myName")) myName = request.getParameter(key);
|
||
|
}
|
||
|
System.out.println("Name = ");
|
||
|
if (myName == "") myName = "Hello";
|
||
|
|
||
|
<font color="#808080" class="code"> }
|
||
|
}</font>
|
||
|
</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>
|