001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 /* $Id: RequestParamExample.java 664175 2008-06-06 22:43:44Z djencks $ 018 * 019 */ 020 021 import java.io.IOException; 022 import java.io.PrintWriter; 023 import java.util.ResourceBundle; 024 025 import javax.servlet.ServletException; 026 import javax.servlet.http.HttpServlet; 027 import javax.servlet.http.HttpServletRequest; 028 import javax.servlet.http.HttpServletResponse; 029 030 import util.HTMLFilter; 031 032 /** 033 * Example servlet showing request headers 034 * 035 * @author James Duncan Davidson <duncan@eng.sun.com> 036 */ 037 038 public class RequestParamExample extends HttpServlet { 039 040 041 ResourceBundle rb = ResourceBundle.getBundle("LocalStrings"); 042 043 public void doGet(HttpServletRequest request, 044 HttpServletResponse response) 045 throws IOException, ServletException { 046 response.setContentType("text/html"); 047 048 PrintWriter out = response.getWriter(); 049 out.println("<html>"); 050 out.println("<body>"); 051 out.println("<head>"); 052 053 String title = rb.getString("requestparams.title"); 054 out.println("<title>" + title + "</title>"); 055 out.println("</head>"); 056 out.println("<body bgcolor=\"white\">"); 057 058 // img stuff not req'd for source code html showing 059 060 // all links relative 061 062 // XXX 063 // making these absolute till we work out the 064 // addition of a PathInfo issue 065 066 out.println("<a href=\"../reqparams.html\">"); 067 out.println("<img src=\"../images/code.gif\" height=24 " + 068 "width=24 align=right border=0 alt=\"view code\"></a>"); 069 out.println("<a href=\"../index.html\">"); 070 out.println("<img src=\"../images/return.gif\" height=24 " + 071 "width=24 align=right border=0 alt=\"return\"></a>"); 072 073 out.println("<h3>" + title + "</h3>"); 074 String firstName = request.getParameter("firstname"); 075 String lastName = request.getParameter("lastname"); 076 out.println(rb.getString("requestparams.params-in-req") + "<br>"); 077 if (firstName != null || lastName != null) { 078 out.println(rb.getString("requestparams.firstname")); 079 out.println(" = " + HTMLFilter.filter(firstName) + "<br>"); 080 out.println(rb.getString("requestparams.lastname")); 081 out.println(" = " + HTMLFilter.filter(lastName)); 082 } else { 083 out.println(rb.getString("requestparams.no-params")); 084 } 085 out.println("<P>"); 086 out.print("<form action=\""); 087 out.print("RequestParamExample\" "); 088 out.println("method=POST>"); 089 out.println(rb.getString("requestparams.firstname")); 090 out.println("<input type=text size=20 name=firstname>"); 091 out.println("<br>"); 092 out.println(rb.getString("requestparams.lastname")); 093 out.println("<input type=text size=20 name=lastname>"); 094 out.println("<br>"); 095 out.println("<input type=submit>"); 096 out.println("</form>"); 097 098 out.println("</body>"); 099 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 }