/*以流的形式获取请求参数*/

RequestDemo5.java文件

package com.neu;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/*以流的形式获取请求参数*/
//一般情况下不使用,文件上传时有时用
public class RequestDemo5 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        ServletInputStream in = request.getInputStream();
        int len = -1;
        byte b[] = new byte[1024];
        while((len=in.read(b)) != -1){
            System.out.println(new String(b,0,len));
        }
        in.close();
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doGet(request, response);
    }

}

index.jsp文件:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
  <head>
    <title>注册页面</title>
  </head>

 <body>
    <form action="/Request/servlet/RequestDemo4" method="post">
        用户名:<input type="text" name="username"/><br/>
        密码:<input type="password" name="password"/><br/>
        确认密码:<input type="password" name="password"/><br/>
        <input type="submit" value="注册"/>
    </form>
  </body>

</html>

运行:
http://localhost:8080/Request/
运行结果:
封装前:User [username=null, password=null, gender=null]
封装后:User [username=ttt, password=[444, 666], gender=null]

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wangyanming123/article/details/52372262
个人分类: request
上一篇httpServletRequest对象获取请求参数
下一篇Request对象中请求参数中文编码问题
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭