关闭

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

97人阅读 评论(0) 收藏 举报
分类:

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]

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:205508次
    • 积分:7685
    • 等级:
    • 排名:第2891名
    • 原创:588篇
    • 转载:11篇
    • 译文:0篇
    • 评论:10条
    最新评论