View Javadoc

1   /*
2   * Licensed to the Apache Software Foundation (ASF) under one or more
3   * contributor license agreements.  See the NOTICE file distributed with
4   * this work for additional information regarding copyright ownership.
5   * The ASF licenses this file to You under the Apache License, Version 2.0
6   * (the "License"); you may not use this file except in compliance with
7   * the License.  You may obtain a copy of the License at
8   *
9   *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17  /* $Id: RequestParamExample.java 664175 2008-06-06 22:43:44Z djencks $
18   *
19   */
20  
21  import java.io.IOException;
22  import java.io.PrintWriter;
23  import java.util.ResourceBundle;
24  
25  import javax.servlet.ServletException;
26  import javax.servlet.http.HttpServlet;
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletResponse;
29  
30  import util.HTMLFilter;
31  
32  /**
33   * Example servlet showing request headers
34   *
35   * @author James Duncan Davidson <duncan@eng.sun.com>
36   */
37  
38  public class RequestParamExample extends HttpServlet {
39  
40  
41      ResourceBundle rb = ResourceBundle.getBundle("LocalStrings");
42  
43      public void doGet(HttpServletRequest request,
44                        HttpServletResponse response)
45              throws IOException, ServletException {
46          response.setContentType("text/html");
47  
48          PrintWriter out = response.getWriter();
49          out.println("<html>");
50          out.println("<body>");
51          out.println("<head>");
52  
53          String title = rb.getString("requestparams.title");
54          out.println("<title>" + title + "</title>");
55          out.println("</head>");
56          out.println("<body bgcolor=\"white\">");
57  
58          // img stuff not req'd for source code html showing
59  
60          // all links relative
61  
62          // XXX
63          // making these absolute till we work out the
64          // addition of a PathInfo issue 
65  
66          out.println("<a href=\"../reqparams.html\">");
67          out.println("<img src=\"../images/code.gif\" height=24 " +
68                  "width=24 align=right border=0 alt=\"view code\"></a>");
69          out.println("<a href=\"../index.html\">");
70          out.println("<img src=\"../images/return.gif\" height=24 " +
71                  "width=24 align=right border=0 alt=\"return\"></a>");
72  
73          out.println("<h3>" + title + "</h3>");
74          String firstName = request.getParameter("firstname");
75          String lastName = request.getParameter("lastname");
76          out.println(rb.getString("requestparams.params-in-req") + "<br>");
77          if (firstName != null || lastName != null) {
78              out.println(rb.getString("requestparams.firstname"));
79              out.println(" = " + HTMLFilter.filter(firstName) + "<br>");
80              out.println(rb.getString("requestparams.lastname"));
81              out.println(" = " + HTMLFilter.filter(lastName));
82          } else {
83              out.println(rb.getString("requestparams.no-params"));
84          }
85          out.println("<P>");
86          out.print("<form action=\"");
87          out.print("RequestParamExample\" ");
88          out.println("method=POST>");
89          out.println(rb.getString("requestparams.firstname"));
90          out.println("<input type=text size=20 name=firstname>");
91          out.println("<br>");
92          out.println(rb.getString("requestparams.lastname"));
93          out.println("<input type=text size=20 name=lastname>");
94          out.println("<br>");
95          out.println("<input type=submit>");
96          out.println("</form>");
97  
98          out.println("</body>");
99          out.println("</html>");
100     }
101 
102     public void doPost(HttpServletRequest request,
103                        HttpServletResponse response)
104             throws IOException, ServletException {
105         doGet(request, response);
106     }
107 
108 }