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 
18 import java.io.IOException;
19 import java.io.PrintWriter;
20 import java.util.ResourceBundle;
21 
22 import javax.servlet.ServletException;
23 import javax.servlet.http.HttpServlet;
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
26 
27 import util.HTMLFilter;
28 
29 /**
30  * Example servlet showing request headers
31  *
32  * @author James Duncan Davidson <duncan@eng.sun.com>
33  */
34 
35 public class RequestParamExample extends HttpServlet {
36 
37     private static final long serialVersionUID = 1L;
38 
39     @Override
doGet(HttpServletRequest request, HttpServletResponse response)40     public void doGet(HttpServletRequest request,
41                       HttpServletResponse response)
42         throws IOException, ServletException
43     {
44         ResourceBundle rb = ResourceBundle.getBundle("LocalStrings",request.getLocale());
45 
46         response.setContentType("text/html");
47         response.setCharacterEncoding("UTF-8");
48 
49         PrintWriter out = response.getWriter();
50         out.println("<!DOCTYPE html><html>");
51         out.println("<head>");
52         out.println("<meta charset=\"UTF-8\" />");
53 
54         String title = rb.getString("requestparams.title");
55         out.println("<title>" + title + "</title>");
56         out.println("</head>");
57         out.println("<body bgcolor=\"white\">");
58 
59         // img stuff not req'd for source code HTML showing
60 
61        // all links relative
62 
63         // XXX
64         // making these absolute till we work out the
65         // addition of a PathInfo issue
66 
67         out.println("<a href=\"../reqparams.html\">");
68         out.println("<img src=\"../images/code.gif\" height=24 " +
69                     "width=24 align=right border=0 alt=\"view code\"></a>");
70         out.println("<a href=\"../index.html\">");
71         out.println("<img src=\"../images/return.gif\" height=24 " +
72                     "width=24 align=right border=0 alt=\"return\"></a>");
73 
74         out.println("<h3>" + title + "</h3>");
75         String firstName = request.getParameter("firstname");
76         String lastName = request.getParameter("lastname");
77         out.println(rb.getString("requestparams.params-in-req") + "<br>");
78         if (firstName != null || lastName != null) {
79             out.println(rb.getString("requestparams.firstname"));
80             out.println(" = " + HTMLFilter.filter(firstName) + "<br>");
81             out.println(rb.getString("requestparams.lastname"));
82             out.println(" = " + HTMLFilter.filter(lastName));
83         } else {
84             out.println(rb.getString("requestparams.no-params"));
85         }
86         out.println("<P>");
87         out.print("<form action=\"");
88         out.print("RequestParamExample\" ");
89         out.println("method=POST>");
90         out.println(rb.getString("requestparams.firstname"));
91         out.println("<input type=text size=20 name=firstname>");
92         out.println("<br>");
93         out.println(rb.getString("requestparams.lastname"));
94         out.println("<input type=text size=20 name=lastname>");
95         out.println("<br>");
96         out.println("<input type=submit>");
97         out.println("</form>");
98 
99         out.println("</body>");
100         out.println("</html>");
101     }
102 
103     @Override
doPost(HttpServletRequest request, HttpServletResponse response)104     public void doPost(HttpServletRequest request,
105                       HttpServletResponse response)
106         throws IOException, ServletException
107     {
108         doGet(request, response);
109     }
110 
111 }
112