web基础----->servlet中得到请求的数据

时间:2024-03-22 18:03:50

  对tomcat的源码做一些分析,今天我们就开始servlet中的请求分析。

form表单中的默认类型

一、在index.jsp中get请求:

<form action="ParameterServlet" method="get">
username: <input type="text" name="username"/><br>
password: <input type="password" name="password"/><br>
file:<input type="file" name="file"><br>
<input type="submit" value="submit">
</form>

二、在ParameterServlet中的doGet方法中:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletInputStream inputStream = request.getInputStream();
System.out.println("queryUrl: " + request.getMethod() + ", data: " + request.getQueryString());
System.out.println("--------------------------------------------------------------------------");
int len;
while((len = inputStream.read()) != -1) {
System.out.print((char)len);
}
inputStream.close();
} protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}

get请求的结果:

queryUrl: GET, data: username=huhx&password=&file=efdjoue.png
--------------------------------------------------------------------------

post请求结果:

queryUrl: POST, data: null
--------------------------------------------------------------------------
username=huhx&password=&file=test.png

三、get和post请求的一些总结

getQueryString方法得到的url中的请求参数部分,而request.getInputStream读取的是请求体的内容。对于get请求来说,从请求流中读取的数据是为空的。

form表单中的multipart类型

修改form表单的enctype为:multipart/form-data

一、index.jsp如下:

<form action="ParameterServlet" method="get" enctype="multipart/form-data">
username: <input type="text" name="username"/><br>
password: <input type="password" name="password"/><br>
file:<input type="file" name="file"><br>
<input type="submit" value="submit">
</form>

get请求的结果和浏览器的地址url:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAocAAAAiCAIAAACiOOTBAAAI7ElEQVR4nO1dPXPbOBDlP/Iv8n9hq4q1GxQ3mYyT8dCamyuiuOHFiZWkukLD6q64SpaU3EzkmcSRb6wrRAK72AU/LMomfe/NK0wQWC6ABR4A0nZ0vb4DQRAEQbAPjK6/3YEgCIIg2AdG8293IAiCIAj2gVBlEARBEOwLocogCIIg2BdGv/7+BwiCIAiCfWD0LwAAAAAA/UC0BQAAAACgH+hAlZfL5f5GAAAAAADoQJVns9n+R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GOqVpWxF1qIahZ+dL4ICXSuy6C32caY9zOVkfBlqNzhztOr8dIvvTjx8/Xrx4EforIi9fvvz+/btqou5rL/WDLJJM3jTvDq9HDffK28CXZMTUmD9Mfu3lbwRoqozOupxy1USWiXK+8UqyFJvFRRSfnqoeXa3KzRygQ1IYULWHmLAfQPCSpFJp470ys8xGCG8ez311K+p3bG2xIk9AD2VxzS9vOSNaQAu5Gscq5M+zXq/KgXoEwXc+TFdDQahEBmlVOsdR42S2dFGYBuOUfXfjDx+lidnUyi4C0eKHBmm1Jh/8DMR9tte3Yzh2i7wqVdaHak2j+Nn9JbTaPErsqjMsHWfK45gqFx/DMVOhE4aWnd8OEb2gf3Fz948e8Rc326D6iGS4eK71AgaF7k8KHxUDd79POMSE9HCb3XsTyaTdf6c4PT09PT3Ff6dogecqXs+1XsCwMHBZG7j7PcJBJqRWRsnpvPfapRMoqtwWUGUAAA6OgcvawN3vBfxPDbo13cYsOejvvlM7UOXFYrG/EQAAAAAAOlBlAAAAAAA6AVQZAAAAAPqC6JffrkAQBEEQ7AOjv//ZgCAIgiDYB0Z/rX6CIAiCINgHQpVBEARBsC+EKoMgCIJgXxj9ufoJgiAIgmAf+B9suMjy56A8zwAAAABJRU5ErkJggg==" alt="" />

queryUrl: GET, data: username=Linux&password=password&file=efdjoue.png
--------------------------------------------------------------------------

post请求的结果:

queryUrl: POST, data: null
--------------------------------------------------------------------------
-----------------------------
Content-Disposition: form-data; name="username" huhx
-----------------------------
Content-Disposition: form-data; name="password" -----------------------------
Content-Disposition: form-data; name="file"; filename="efdjoue.png"
Content-Type: image/png ‰PNG
....后面是图片的乱码信息,这里省略....

二、在设置了multipart/form-data之后的请求中

  • get请求可以使用request.getParameter("username")得到对应的值
  • post请求使用request.getParameter("username")得到的是null,getParameter("file")也是null。

友情链接

相关文章