From 1972128d94225c4f5b43b4aee94d1b9c38284b3c Mon Sep 17 00:00:00 2001 From: remm Date: Wed, 16 Aug 2006 12:57:35 +0000 Subject: [PATCH] - Note: the paths are not completely correct. git-svn-id: https://svn.apache.org/repos/asf/tomcat/tc6.0.x/trunk@431890 13f79535-47bb-0310-9956-ffa450edef68 --- webapps/examples/servlets/cookies.html | 60 +++++++++++++++ webapps/examples/servlets/helloworld.html | 49 ++++++++++++ webapps/examples/servlets/index.html | 120 ++++++++++++++++++++++++++++++ webapps/examples/servlets/reqheaders.html | 48 ++++++++++++ webapps/examples/servlets/reqinfo.html | 67 +++++++++++++++++ webapps/examples/servlets/reqparams.html | 77 +++++++++++++++++++ webapps/examples/servlets/sessions.html | 69 +++++++++++++++++ 7 files changed, 490 insertions(+) create mode 100644 webapps/examples/servlets/cookies.html create mode 100644 webapps/examples/servlets/helloworld.html create mode 100644 webapps/examples/servlets/index.html create mode 100644 webapps/examples/servlets/reqheaders.html create mode 100644 webapps/examples/servlets/reqinfo.html create mode 100644 webapps/examples/servlets/reqparams.html create mode 100644 webapps/examples/servlets/sessions.html diff --git a/webapps/examples/servlets/cookies.html b/webapps/examples/servlets/cookies.html new file mode 100644 index 000000000..f59ccba6f --- /dev/null +++ b/webapps/examples/servlets/cookies.html @@ -0,0 +1,60 @@ + + + +Untitled Document + + + + +

+

Source Code for Cookie Example
+

+ +
import java.io.*;
+import javax.servlet.*;
+import javax.servlet.http.*;
+
+public class CookieExample extends HttpServlet {
+
+    public void doGet(HttpServletRequest request, HttpServletResponse response)
+    throws IOException, ServletException
+    {
+        response.setContentType("text/html");
+        PrintWriter out = response.getWriter();
+        
+        // print out cookies
+
+        Cookie[] cookies = request.getCookies();
+        for (int i = 0; i < cookies.length; i++) {
+            Cookie c = cookies[i];
+            String name = c.getName();
+            String value = c.getValue();
+            out.println(name + " = " + value);
+        }
+
+        // set a cookie
+
+        String name = request.getParameter("cookieName");
+        if (name != null && name.length() > 0) {
+            String value = request.getParameter("cookieValue");
+            Cookie c = new Cookie(name, value);
+            response.addCookie(c);
+        }
+    }
+}
+ + diff --git a/webapps/examples/servlets/helloworld.html b/webapps/examples/servlets/helloworld.html new file mode 100644 index 000000000..25bdf00ef --- /dev/null +++ b/webapps/examples/servlets/helloworld.html @@ -0,0 +1,49 @@ + + + +Untitled Document + + + + +

+

Source Code for HelloWorld Example
+

+ +
import java.io.*;
+import javax.servlet.*;
+import javax.servlet.http.*;
+
+public class HelloWorld extends HttpServlet {
+
+    public void doGet(HttpServletRequest request, HttpServletResponse response)
+    throws IOException, ServletException
+    {
+        response.setContentType("text/html");
+        PrintWriter out = response.getWriter();
+        out.println("<html>");
+        out.println("<head>");
+        out.println("<title>Hello World!</title>");
+        out.println("</head>");
+        out.println("<body>");
+        out.println("<h1>Hello World!</h1>");
+        out.println("</body>");
+        out.println("</html>");
+    }
+}
+ + diff --git a/webapps/examples/servlets/index.html b/webapps/examples/servlets/index.html new file mode 100644 index 000000000..05a8ab276 --- /dev/null +++ b/webapps/examples/servlets/index.html @@ -0,0 +1,120 @@ + + + + + + + + Servlet Examples + + +Servlet +Examples with Code +

This is a collection of examples which demonstrate some of the more +frequently used parts of the Servlet API. Familiarity with the Java(tm) +Programming Language is assumed. +

These examples will only work when viewed via an http URL. They will +not work if you are viewing these pages via a "file://..." URL. Please +refer to the README file provide with this Tomcat release regarding +how to configure and start the provided web server. +

Wherever you see a form, enter some data and see how the servlet reacts. +When playing with the Cookie and Session Examples, jump back to the Headers +Example to see exactly what your browser is sending the server. +

To navigate your way through the examples, the following icons will +help: +
  + + + + + + + + + + + + + + + + + + +
Execute the example
Look at the source code for the example
Return to this screen
+ +

Tip: To see the cookie interactions with your browser, try turning on +the "notify when setting a cookie" option in your browser preferences. +This will let you see when a session is created and give some feedback +when looking at the cookie demo. +
  + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Hello WorldExecuteSource
Request InfoExecuteSource
Request HeadersExecuteSource
Request ParametersExecuteSource
CookiesExecuteSource
SessionsExecuteSource
+ +

Note: The source code for these examples does not contain all of the +source code that is actually in the example, only the important sections +of code. Code not important to understand the example has been removed +for clarity. + + diff --git a/webapps/examples/servlets/reqheaders.html b/webapps/examples/servlets/reqheaders.html new file mode 100644 index 000000000..c9c239101 --- /dev/null +++ b/webapps/examples/servlets/reqheaders.html @@ -0,0 +1,48 @@ + + + +Untitled Document + + + + +

+

Source Code for RequestHeader Example
+

+ +
import java.io.*;
+import java.util.*;
+import javax.servlet.*;
+import javax.servlet.http.*;
+
+public class RequestHeaderExample extends HttpServlet {
+
+    public void doGet(HttpServletRequest request, HttpServletResponse response)
+    throws IOException, ServletException
+    {
+        response.setContentType("text/html");
+        PrintWriter out = response.getWriter();
+        Enumeration e = request.getHeaderNames();
+        while (e.hasMoreElements()) {
+            String name = (String)e.nextElement();
+            String value = request.getHeader(name);
+            out.println(name + " = " + value);
+        }
+    }
+}
+ + diff --git a/webapps/examples/servlets/reqinfo.html b/webapps/examples/servlets/reqinfo.html new file mode 100644 index 000000000..1d926b796 --- /dev/null +++ b/webapps/examples/servlets/reqinfo.html @@ -0,0 +1,67 @@ + + + +Untitled Document + + + + +

+

Source Code for Request Info Example
+

+ +
import java.io.*;
+import javax.servlet.*;
+import javax.servlet.http.*;
+
+public class RequestInfo extends HttpServlet {
+
+    public void doGet(HttpServletRequest request, HttpServletResponse response)
+    throws IOException, ServletException
+    {
+        response.setContentType("text/html");
+        PrintWriter out = response.getWriter();
+        out.println("<html>");
+        out.println("<body>");
+        out.println("<head>");
+        out.println("<title>Request Information Example</title>");
+        out.println("</head>");
+        out.println("<body>");
+        out.println("<h3>Request Information Example</h3>");
+        out.println("Method: " + request.getMethod());
+        out.println("Request URI: " + request.getRequestURI());
+        out.println("Protocol: " + request.getProtocol());
+        out.println("PathInfo: " + request.getPathInfo());
+        out.println("Remote Address: " + request.getRemoteAddr());
+        out.println("</body>");
+        out.println("</html>");
+    }
+
+    /**
+     * We are going to perform the same operations for POST requests
+     * as for GET methods, so this method just sends the request to
+     * the doGet method.
+     */
+
+    public void doPost(HttpServletRequest request, HttpServletResponse response)
+    throws IOException, ServletException
+    {
+        doGet(request, response);
+    }
+}
+ + diff --git a/webapps/examples/servlets/reqparams.html b/webapps/examples/servlets/reqparams.html new file mode 100644 index 000000000..2c132f198 --- /dev/null +++ b/webapps/examples/servlets/reqparams.html @@ -0,0 +1,77 @@ + + + +Untitled Document + + + + +

+

Source Code for Request Parameter Example
+

+ +
import java.io.*;
+import java.util.*;
+import javax.servlet.*;
+import javax.servlet.http.*;
+
+public class RequestParamExample extends HttpServlet {
+
+    public void doGet(HttpServletRequest request, HttpServletResponse response)
+    throws IOException, ServletException
+    {
+        response.setContentType("text/html");
+        PrintWriter out = response.getWriter();
+        out.println("<html>");
+        out.println("<head>");
+        out.println("<title>Request Parameters Example</title>");
+        out.println("</head>");
+        out.println("<body>");
+        out.println("<h3>Request Parameters Example</h3>");
+        out.println("Parameters in this request:<br>");
+        if (firstName != null || lastName != null) {
+            out.println("First Name:");
+            out.println(" = " + HTMLFilter.filter(firstName) + "<br>");
+            out.println("Last Name:");
+            out.println(" = " + HTMLFilter.filter(lastName));
+        } else {
+            out.println("No Parameters, Please enter some");
+        }
+        out.println("<P>");
+        out.print("<form action=\"");
+        out.print("RequestParamExample\" ");
+        out.println("method=POST>");
+        out.println("First Name:");
+        out.println("<input type=text size=20 name=firstname>");
+        out.println("<br>");
+        out.println("Last Name:");
+        out.println("<input type=text size=20 name=lastname>");
+        out.println("<br>");
+        out.println("<input type=submit>");
+        out.println("</form>");
+        out.println("</body>");
+        out.println("</html>");
+    }
+
+    public void doPost(HttpServletRequest request, HttpServletResponse res)
+    throws IOException, ServletException
+    {
+        doGet(request, response);
+    }
+}
+ + diff --git a/webapps/examples/servlets/sessions.html b/webapps/examples/servlets/sessions.html new file mode 100644 index 000000000..779d68450 --- /dev/null +++ b/webapps/examples/servlets/sessions.html @@ -0,0 +1,69 @@ + + + +Untitled Document + + + + +

+

Source Code for Session Example
+

+ +
import java.io.*;
+import java.util.*;
+import javax.servlet.*;
+import javax.servlet.http.*;
+
+public class SessionExample extends HttpServlet {
+
+    public void doGet(HttpServletRequest request, HttpServletResponse response)
+    throws IOException, ServletException
+    {
+        response.setContentType("text/html");
+        PrintWriter out = response.getWriter();
+        
+        HttpSession session = request.getSession(true);
+
+        // print session info
+
+        Date created = new Date(session.getCreationTime());
+        Date accessed = new Date(session.getLastAccessedTime());
+        out.println("ID " + session.getId());
+        out.println("Created: " + created);
+        out.println("Last Accessed: " + accessed);
+
+        // set session info if needed
+
+        String dataName = request.getParameter("dataName");
+        if (dataName != null && dataName.length() > 0) {
+            String dataValue = request.getParameter("dataValue");
+            session.setAttribute(dataName, dataValue);
+        }
+
+        // print session contents
+
+        Enumeration e = session.getAttributeNames();
+        while (e.hasMoreElements()) {
+            String name = (String)e.nextElement();
+            String value = session.getAttribute(name).toString();
+            out.println(name + " = " + value);
+        }
+    }
+}
+ + -- 2.11.0