RequestParamExample.java
上传用户:bj_pst
上传日期:2019-07-07
资源大小:7353k
文件大小:4k
源码类别:

Java编程

开发平台:

Java

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