码迷,mamicode.com
首页 > 编程语言 > 详细

入门:HTML表单与Java 后台交互(复选框提交)

时间:2016-09-18 23:34:07      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:

仅仅给出部分关键代码:

HTML form code:

<form action="JavaFormTest" method="post" name="Form">
  <p>
    清输入用户名:<input type="text" name="username"/><br />
    请输入用户密码:<input type="password" name="password" /><br />
  </p>
     请选择你的课程<br />
  <p>
     <input type="checkbox" name="checkbox" value="足球" />足球
     <input type="checkbox" name="checkbox" value="篮球" /> 篮球
     <input type="checkbox" name="checkbox" value="乒乓球" />乒乓球
     <input type="checkbox" name="checkbox" value="排球" />排球球
   </p>
     <input type="button" name="but" onclick="submit()" value="提交" />
</form>

JavaFormTest.java 处理程序doPost 部分:

protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String[] list = request.getParameterValues("checkbox");
        if (username.isEmpty())
            out.println("name is empty!<br />");
        else
            out.println("name:" + username + "<br />");

        if (password.isEmpty())
            out.println("password is empty!<br />");
        else
            out.println("password:" + password + "<br />");

        if (list == null)
            out.println("none list");
        else {
            out.println("list: <br />");
            for (int i=0;i<list.length;i++) {
                out.println("第 "+(i+1)+" 课程是:"+ list[i]+"<br />");
            }
        }
    }

 

入门:HTML表单与Java 后台交互(复选框提交)

标签:

原文地址:http://www.cnblogs.com/A--Q/p/5883376.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!