• Sevlet处理HTML表单


    表单<form>用于搜集不同类型的用户输入。可以轻松地将一个HTML表单从一个Servlet发送到浏览器,当用户提交表单时,在表单元素中输入的值就会被当做请求参数发送到服务器。

    request.getParameter(name) 中的name指的是表单中元素的name属性的值

    HTML输入域(文本域、隐藏域、密码域)或者文本区的值,会被当做字符串发送到服务器,空的输入域或者文本区会发送空字符串。因此,有输入域名称的,ServletRequest.getParameter(String name)绝对不会返回null

    包含多个值的<select>元素,使用ServeltRequest.getParameterValues(name)捕获选中的值,返回一个字符串数组

    复选框<input type="checkbox" />,选中的复选框会发送字符串“on”到服务器,未选中的复选框不向服务器发送任何东西,此时ServletRequest.getParameter(name)返回null

    单选框<input type="radio" />,将被选中按钮的值(即value属性的值)发送到服务器,若没有选中任何按钮,将没有内容发送到服务器,此时Servletrequest.getParameter(name)返回null

    如果同一个表单包含多个输入同名的元素(即name属性的值相同),那么所有值都会被提交,并且必须利用Servlet.getParameterValues(name)来捕获它们并返回一个字符串数组。若使用Servler.getParameter(name)将只会返回最后一个值。

    package app01b;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Enumeration;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * Servlet implementation class FormServlet
     */
    
    @WebServlet(name = "FormServlet", urlPatterns = {"/form"})
    public class FormServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
        private static final String TITLE = "Order Form";
        
        public FormServlet() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            response.setContentType("text/html");
            PrintWriter writer = response.getWriter();
            writer.println("<html>");
            writer.println("<head>");
            writer.println("<title>" + TITLE + "</title>");
            writer.println("</head>");
            writer.println("<body>");
            writer.println("<h1>" + TITLE + "</h1>");
            writer.println("<form method='post'>");
            writer.println("<table>");
            writer.println("<tr>");
            writer.println("<td>Name:</td>");
            writer.println("<td><input type='text' name='name' /></td>");
            writer.println("</tr>");
            writer.println("<tr>");
            writer.println("<td>Address:</td>");
            writer.println("<td><textarea name='address' cols='40' rows='5'></textarea></td>");
            writer.println("</tr>");
            writer.println("<tr>");
            writer.println("<td>Country:</td>");
            writer.println("<td>");
            writer.println("<select name='country'>");
            writer.println("<option>United States</option>");
            writer.println("<option>Canada</option>");
            writer.println("</select>");
            writer.println("</td>");
            writer.println("</tr>");
            writer.println("<tr>");
            writer.println("<td>Delivery Method:</td>");
            writer.println("<td>");
            writer.println("<input type='radio' name='deliveryMethod' value='First Class' />First Class");  // 将被选中按钮的值(即value属性的值)发送到服务器,若没有选中任何按钮,将没有内容发送到服务器,此时Servletrequest.getParameter(name)返回null
            writer.println("<input type='radio' name='deliveryMethod' value='Second Class' />Second Class");
            writer.println("</td>");
            writer.println("</tr>");
            writer.println("<tr>");
            writer.println("<td>Shipping Instructions:</td>");
            writer.println("<td><textarea name='instructions' cols='40' rows='5'></textarea></td>");  // 如果同一个表单包含多个输入同名的元素(即name属性的值相同),那么所有值都会被提交,并且必须利用Servlet.getParameterValues(name)来捕获它们并返回一个字符串数组。
            writer.println("</tr>");
            writer.println("<tr>");
            writer.println("<td>&nbsp;</td>");
            writer.println("<td><textarea name = 'instructions' cols='40' rows='5'></textarea></td>");
            writer.println("</tr>");
            writer.println("<tr>");
            writer.println("<td>Please send me the leatest product catalog:</td>");
            writer.println("<td><input type='checkbox' name='catalogRequest' /></td>");  // 若选中,发送字符串“on”到服务器;若未选中,不向服务器发送任何东西,此时ServletRequest.getParameter(name)返回null
            writer.println("</tr>");
            writer.println("<tr>");
            writer.println("<td>&nbsp;</td>");
            writer.println("<td><input type='reset' /><input type='submit' /></td>");
            writer.println("</tr>");
            writer.println("</table>");
            writer.println("</form>");
            writer.println("</body>");
            writer.println("</html>");
        }
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            response.setContentType("text/html");
            PrintWriter writer = response.getWriter();
            writer.println("<html>");
            writer.println("<head>");
            writer.println("<title>" + TITLE + "</title>");
            writer.println("</head>");
            writer.println("<body>");
            writer.println("<h1>" + TITLE + "</h1>");
            writer.println("<table>");
            writer.println("<tr>");
            writer.println("<td>Name:</td>");
            writer.println("<td>" + request.getParameter("name") + "</td>");
            writer.println("</tr>");
            writer.println("<tr>");
            writer.println("<td>Address:</td>");
            writer.println("<td>" + request.getParameter("address") + "</td>");
            writer.println("</tr>");
            writer.println("<tr>");
            writer.println("<td>Country:</td>");
            writer.println("<td>" + request.getParameter("country") + "</td>");
            writer.println("</tr>");
            writer.println("<tr>");
            writer.println("<td>Shipping Instructions:</td>");
            writer.println("<td>");
            String[] instructions = request.getParameterValues("instruction");
            if(instructions != null) {
                for(String instruction : instructions) {
                    writer.println(instruction + "<br />");
                }
            }
            writer.println("</td>");
            writer.println("</tr>");
            writer.println("<tr>");
            writer.println("<td>Delivery Method:</td>");
            writer.println("<td>" + request.getParameter("deliveryMethod") + "</td>");
            writer.println("</tr>");
            writer.println("<tr>");
            writer.println("<td>Catalog Request:</td>");
            writer.println("<td>");
            if(request.getParameter("catalogRequest") == null) {
                writer.println("No");
            }else {
                writer.println("Yes");
            }
            writer.println("</td>");
            writer.println("</tr>");
            writer.println("</table>");
            writer.println("<div style='border:1px solid #ddd; margin-top:40px; font-size:90%'>");
            writer.println("Debug Info<br />");
            Enumeration<String> parameters = request.getParameterNames();
            while(parameters.hasMoreElements()) {
                String parameterName = parameters.nextElement();
                writer.println(parameterName + ": ");
                String[] parameterValues = request.getParameterValues(parameterName);
                for(String parameterValue : parameterValues) {
                    writer.println(parameterValue + "<br />");
                }
            }
            writer.println("</div>");
            writer.println("</body>");
            writer.println("</html>");
        }
    }

    Servlet中写的html代码(doGet()方法中的)。通过浏览器查看

    Servlet中写的html代码(doPost()方法中的)

  • 相关阅读:
    关于获取路径
    今天最好的生日礼物就是重新找到目标
    Fedora与Ubuntu安装g++的命令
    CMPXCHG8B 比较并交换 8 字节
    关于 WIN32_LEAN_AND_MEAN
    i386和i686
    Intrinsic function
    VC9: LINK : warning LNK4068: /MACHINE not specified; defaulting to X86
    Linux内核中的Min和Max函数
    linux重定向命令应用及语法
  • 原文地址:https://www.cnblogs.com/0820LL/p/9813949.html
Copyright © 2020-2023  润新知