jsp request ServletInputStream

晴天v1 2009-07-01 11:38:31
最近在看http协议的东西,按道理我的ServletInputStream应该能读到全部的请求信息,请求行,状态行,
请求内容。

但在servlet里面写到文本文件中只有request的请求参数。不得其解。请了解的帮忙看下哪里出问题了。

下面是jsp代码。

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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 'Demo.jsp' starting page</title>
<meta http-equiv="Content-Type" content="text/html">
<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">

</head>
<body>
<form action="/Upload/TestServlet" method="post">

text: <input type="text" name="username" /><br>
password:<input type="password" name="psw" /><br>
checkbox:<br>
favorite1 <input type="checkbox" name="favorites" value="dance"/><br>
favorite2 <input type="checkbox" name="favorites" value="football"/><br>
favorite3 <input type="checkbox" name="favorites" value="kiss"/><br>

select: <select name="selectKey">
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
</select>
<br>

<input type="submit" value="post"/>

</form>
</body>
</html>



servlet代码。

import java.io.File;
import java.io.FileOutputStream;
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 TestServlet extends HttpServlet {



@Override
protected void doGet(HttpServletRequest request, HttpServletResponse res) throws ServletException, IOException {

}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse arg1)
throws ServletException, IOException {

ServletInputStream input = request.getInputStream();
byte [] buffer = new byte[256];

File file = new File("C:\\http","http.txt");
FileOutputStream outputFile = new FileOutputStream(file);
if(!file.exists()) {
file.mkdir();
}

int length = input.read(buffer,0, buffer.length);

while(length > 0){
outputFile.write(buffer,0,buffer.length);
outputFile.flush();
length = input.read(buffer,0, buffer.length);
}

outputFile.close();
input.close();
}


}
...全文
119 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
晴天v1 2009-07-02
  • 打赏
  • 举报
回复
明白了,enctype="multipart/form-data" 是字节流方式来提交,一般用于文件上传。
晴天v1 2009-07-02
  • 打赏
  • 举报
回复
如果我在form表单中加上enctype="multipart/form-data"

提交的话输出文件中的内容又不一样。
aptech_meibo 2009-07-02
  • 打赏
  • 举报
回复
结果肯定是这个样子啊
jsp上用的是表单,你提交到servlet肯定只有表单里面空间的名称和值
如果你要获得一个网页里面所有的代码,你可以用链接跳到一个servlet这样可以或得到。。
晴天v1 2009-07-02
  • 打赏
  • 举报
回复
顶上去。
晴天v1 2009-07-02
  • 打赏
  • 举报
回复
大伙帮忙看看。
晴天v1 2009-07-01
  • 打赏
  • 举报
回复
上面还没说完,如果我在页面上输入 csdn,123456,选个checkbox,选个下拉列表。

在http.txt中输出文件如下:
username=csdn+&psw=123456&favorites=dance&selectKey=20

只有request的parameter参数,没有http请求的信息。

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧