封装表单案例

将页面的数据封装到表单中

register.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP ‘register.jsp‘ starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>

  <body>

   <form action="/app/servlet/requestDemo3" method="post">
         姓名:<input type="text" name="username"><br>
         密码:<input type="password" name="password"><br>
         确认密码:<input type="password" name="password"><br>
         性别:<input type="radio" name="gander" value="男" >男
             <input type="radio" name="gander" value="女">女<br>
         爱好:<input type="checkbox" name="loves" value="打篮球">打篮球
             <input type="checkbox" name="loves" value="踢足球">踢足球
             <input type="checkbox" name="loves" value="抱孩子">抱孩子<br>
         国籍:<select name="country">
                 <option value="中国">中国</option>
                 <option value="美国">美国</option>
                 <option value="加拿大">加拿大</option>
                 <option value="日本">日本</option>
                 <option value="韩国">韩国</option>
                 <option value="俄罗斯">俄罗斯</option><br>
         </select>
         备注:<textarea rows="5" cols="20" name="description">大家一起去请两天假</textarea><br>
         <input type="submit" value="提交"><br>
   </form>

  </body>
</html>

User.java

package bean;

import java.io.Serializable;
import java.util.Arrays;

/**
 * javabean
 * @author kj
 *
 */
public class User implements Serializable{

    private String username;
    private String[] password;
    private String gander;
    private String[] loves;
    private String country;
    private String description;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String[] getPassword() {
        return password;
    }
    public void setPassword(String[] password) {
        this.password = password;
    }
    public String getGander() {
        return gander;
    }
    public void setGander(String gander) {
        this.gander = gander;
    }
    public String[] getLoves() {
        return loves;
    }
    public void setLoves(String[] loves) {
        this.loves = loves;
    }
    public String getCountry() {
        return country;
    }
    public void setCountry(String country) {
        this.country = country;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    @Override
    public String toString() {
        return "User [username=" + username + ", password="
                + Arrays.toString(password) + ", gander=" + gander + ", loves="
                + Arrays.toString(loves) + ", country=" + country
                + ", description=" + description + "]";
    }
}

requestDeom3.java    (Servlet类)

public class requestDemo3 extends HttpServlet {

    /**
     * 演示封装表单中的数据
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     *
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        request.setCharacterEncoding("UTF-8");
        User user = new User();

        //将表单数据封装到user对象中
        try {
            BeanUtils.populate(user, request.getParameterMap());
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("-----封装后-----" + user);
    }
时间: 2024-11-07 11:36:22

封装表单案例的相关文章

web设计——表单案例

案例地址:http://wanwanweb.sinaapp.com/yglogin/ 源码在下方仅供参考,自己动手设计一款类似的版式,然后亲自动手试一下. 1 <?php 2 ?> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta http-equiv="content-type" content="text/html;charset=utf-8"> 7 <tit

httpclient模拟post请求json封装表单数据

1 public static String httpPostWithJSON(String url) throws Exception { 2 3 HttpPost httpPost = new HttpPost(url); 4 CloseableHttpClient client = HttpClients.createDefault(); 5 String respContent = null; 6 7 // json方式 8 JSONObject jsonParam = new JSON

封装表单自定义错误信息。(阻止默认错误信息提示,添加自定义错误信息提示)

1 前台提交信息到后台,两种表单验证: 2 1,form 表单验证 3 2,ajax 无刷新页面提交 4 5 表单验证方法一般有: 6 1,浏览器端验证 7 2,服务器端验证 8 3,ajax验证 9 4,浏览器和服务器双重验证 10 11 html5表单新增类型: 12 email,url,number,range,data(date,month,week,time,datetime,datetime-local),search,color,tel等 13 ======== 14 重点:pat

封装表单模块

后台管理系统经常用到表单查询,因此做了个封装 myorder.js import React from 'react'; import { Card, Button, Table, Form, Select, Modal, DatePicker, message} from 'antd' import axios from './axios' import Utils from './utils' import BaseForm from './BaseForm' const FormItem

.Net Core使用视图组件(ViewComponent)封装表单文本框控件

实例程序的界面效果如下图所示: 在表单中的搜索条件有姓名,学号,成绩.他们在一行中按照水平三等分排列. 在cshtml中用html实现上述表单效果的的代码如下: 1 <form class="form-horizontal" role="form"> 2 <div class="row"> 3 <div class="form-group col-md-4"> 4 <label for

vue+elementui 封装表单验证

其实很简单:步骤1:先用element 把页面写出来:步骤2.规则验证,需要自定义验证的,引入对应的自定义验证方法, 3.封装一个自定义验证的js. 1.先把结构写出来: 注意: from 表单上有model(绑定数据),ref(提交的时有用),rules(对表单的验证规则),这三者缺一不可. 2.代码: <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width

request对象封装表单数据

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // test1(request); // test2(request); test3(request); } //获取单个控件的值 public void test1(HttpServletRequest request){ String name = request

dom 封装表单控件

<!doctype html> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> <style> body { margin: 0; padding: 0; font-family: '微软雅黑'; } form { border: 1px solid #eee; border-radius: 10px; width: 600px; ma

yii2_vue_表单案例

<?php /** * Created by PhpStorm. * User: Administrator * Date: 2017/2/5 * Time: 17:36 */ use yii\helpers\Url; use \common\models\AdminSite; use \common\utils\Cache; use \common\services\TudouService; $adminsite=Cache::GAutoRoute('adminsite',TudouServ