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

75 lines
2.2 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>Troubleshoot by problem</title>
</head>
<BODY>
<!-- Java sync-link -->
<SCRIPT LANGUAGE="Javascript" SRC="../../../rzahg/synch.js" TYPE="text/javascript"></SCRIPT>
<h5><A NAME="sampserv"></A>ServletSample.java</h5>
// ServletSample.java
<pre>
// Step 1: Add the necessary import statements.
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
// Step 2: Extend HttpServlet.
public class ServletSample extends HttpServlet
{
// Step 3: Specify the required methods.
public void doGet (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
// Step 4: Get the HTTP request information, if any.
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;;
// Step 5: Create the HTTP response.
response.setContentType(&quot;text/html&quot;);
response.setHeader(&quot;Pragma&quot;, &quot;No-cache&quot;);
response.setDateHeader(&quot;Expires&quot;, 0);
response.setHeader(&quot;Cache-Control&quot;, &quot;no-cache&quot;);
PrintWriter out = response.getWriter();
out.println(&quot;&lt;html&gt;&quot;);
out.println(&quot;&lt;head&gt;&lt;title&gt;Just a basic servlet&lt;/title&gt;&lt;/head&gt;&quot;);
out.println(&quot;&lt;body&gt;&quot;);
out.println(&quot;&lt;h1&gt;Just a basic servlet&lt;/h1&gt;&quot;);
out.println (&quot;&lt;p&gt;&quot; + myName + &quot;, this is a very basic servlet.&quot;);
out.println(&quot;&lt;/body&gt;&lt;/html&gt;&quot;);
out.flush();
}
}
</pre>
</body>
</html>