View Javadoc

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