Servlet 3.1 处理复选框数据

Java Servlet 处理复选框数据

form-checkbox.html

<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" %> 
<!DOCTYPE html>
<meta charset="utf-8">
<title>获取复选框数据| 简单教程(www.twle.cn)</title>
<p>获取复选框数据 | 简单教程(www.twle.cn)</p>
<form action="/servlet/form/checkbox" method="POST">
<input type="checkbox" name="twle" checked="checked" /> 简单编程
<input type="checkbox" name="google"  /> Google
<input type="checkbox" name="taobao" checked="checked" /> 淘宝
<input type="submit" value="选择站点" />
</form>

cn/twle/demo/FormCheckboxServlet.java

// author: 简单教程(www.twle.cn)
// Copyright © 2015-2065 www.twle.cn. All rights reserved.

package cn.twle.demo;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*; 
import javax.servlet.annotation.WebServlet;
import java.util.Enumeration;

@WebServlet(name = "FormCheckboxServlet", urlPatterns = {"form/checkbox"}, loadOnStartup = 1) 
public class FormCheckboxServlet extends HttpServlet
{

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置响应内容类型
        response.setContentType("text/html;charset=UTF-8");

        PrintWriter out = response.getWriter();
        String title = "获取复选框数据 | 简单教程(www.twle.cn)";
        String docType = "<!DOCTYPE html> \n";
            out.println(docType +
                "<title>" + title + "</title>" +
                "<p>" + title + "</p>\n" +
                "<ul>\n" +
                "  <li><b>简单教程标识:</b>: "
                + request.getParameter("twle") + "\n" +
                "  <li><b>Google 标识:</b>: "
                + request.getParameter("google") + "\n" +
                "  <li><b>淘宝标识:</b>: "
                + request.getParameter("taobao") + "\n" +
                "</ul>");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

在浏览器上输入 http://localhost:8080/servlet/form-checkbox.jsp 显示结果如下

关于   |   FAQ   |   我们的愿景   |   广告投放   |  博客

  简单教程,简单编程 - IT 入门首选站

Copyright © 2013-2022 简单教程 twle.cn All Rights Reserved.