Day04JavaWeb【Request】请求体

request-获取请求体-获取一个参数和中文乱码问题***

  • (1)请求体是什么?
    提交的参数
  • (2)有什么特点?
    1)只有POST请求才有请求体
    2)请求体中包含的是请求的参数
  • (3)方法
String getParameter(String name)  //根据一个参数名,获取参数的值      、
 request.setCharacterEncoding("UTF-8");

src\com\wzx\pack03_body\Demo03GetServlet.java

@WebServlet("/demo03")
public class Demo03GetServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //Tomcat 8.5 已默认可以处理GET请求的中文的提交,但是没有处理POST的中文的提交
        //如果是POST请求,自己设置编码utf-8
        //if("POST".equals(request.getMethod())){
            //设置请求体的数据的编码为utf-8
            request.setCharacterEncoding("UTF-8");
        //}

        //获取请求体中的参数,才是真正要传给服务端的数据
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        System.out.println(username);
        System.out.println(password);
    }
}

web\login.html

    <form method="post" action="/abc/demo03">
        post 账号:<input type="text" name="username"/><br/>
        密码:<input type="password" name="password"/><br/>
        <input type="submit" value="提交"/><br/>
    </form>
<hr/>

    <form method="get" action="/abc/demo03">
        get 账号:<input type="text" name="username"/><br/>
        密码:<input type="password" name="password"/><br/>
        <input type="submit" value="提交"/><br/>
    </form>
<hr/>
<a href="/abc/demo03?age=13">get请求</a>

request-获取请求体-获取多个参数和所有参数

//根据一个参数名,获取参数的多个值   如果没有值,则返回null
String[] getParameterValues(String name)    一个键多个值
 //获取所有的参数名和参数值
Map<String,String[]> getParameterMap()   所有参数

如果表单中的参数,是一个key对应一个value,类似username,使用getParamter()
如果表单中的参数,是一个key对应多个value,类似hobby,使用getParamterValues()
getParameterMap()很少单独用。

<form method="post" action="/abc/demo04">
        post 账号:<input type="text" name="username"/><br/>
        密码:<input type="password" name="password"/><br/>
        爱好:<input type="checkbox" name="hobby" value="1"/>代码
        <input type="checkbox" name="hobby" value="2"/>美女
        <input type="checkbox" name="hobby" value="3"/>钞票<br/>
        性别:   <input type="radio" name="sex" value="1"/><input type="radio" name="sex" value="0"/><br/>
        <input type="submit" value="提交"/><br/>
    </form>

src\com\wzx\pack03_body\Demo04GetServlet.java

@WebServlet("/demo04")
public class Demo04GetServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取所有请求参数的值s
        String[] usernames = request.getParameterValues("username");
        System.out.println(usernames[0]);

        //复选框
        String[] hobby = request.getParameterValues("hobby");
        System.out.println(Arrays.toString(hobby));

        //单选
        String[] sex = request.getParameterValues("sex");
        System.out.println(Arrays.toString(sex));

        //---------------------------------------------
        Map<String,String[]> map = request.getParameterMap();
        //取出map中所有的key
        Set<String> keys = map.keySet();
        for(String key:keys){
            System.out.println(key+" "+Arrays.toString(map.get(key)));//get方法根据key查找value
        }
        //System.out.println(map);
    }
}

Arrays.toString(数组) 用于将数组中的元素拼接成字符串返回,避免直接打印显示内存地址username [Ljava.lang.String;@773f238d

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

翁老师的教学团队

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值