Servlet---表单参数接收/简单的文件上传

时间:2022-11-22 14:37:15

本篇主要讲到的是读取单个参数和多个参数和最简单的文件上传。

一、参数接收

前面一直使用到request.getParameter("参数名")获取单个参数的方法,这里使用request.getParameterValues("参数名")来获取多个参数的。可以用于表单中的checkbox这种复选框,但记住,前台写checkbox时name一定要相同。

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>

<body>
<h2>--以下专门演示表单参数接收 </h2>
<form action="<%= request.getContextPath() %>/param" method="post">
name:<input type="text" name="name" /><br/>
age:<input type="text" name="age" /><br/>
hobby:
<input type="checkbox" name="hobby" value="music"/>music  
<input type="checkbox" name="hobby" value="painting"/>painting  
<input type="checkbox" name="hobby" value="basketball"/>basketball<br/>
sex:
<input type="radio" name="sex" value="male"/>male 
<input type="radio" name="sex" value="female"/>female<br/>
<input type="submit" value="提交" />
</form>
</body>
</html>


ParamServlet.java
package cn.hncu.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ParamServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String queryString=request.getQueryString();
System.out.println(queryString);//get方式请求地址中,"?"号后面的那部分全部封装在这个字符串当中
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");

//读取单个参数值
String name=request.getParameter("name");
String age=request.getParameter("age");
System.out.println(name+","+age);

String hobby[]=request.getParameterValues("hobby");//如果某个参数的值有好几个,用这个方法
if (hobby!=null){
for (String str:hobby){
System.out.println(str);
}
}
String sex=request.getParameter("sex");
System.out.println("sex="+sex);

//演示getParameterMap()
Map<String, String[]> map=request.getParameterMap();
//遍历出所有参数及值
Iterator<Entry<String, String[]>> it=map.entrySet().iterator();
while(it.hasNext()){
Entry<String, String[]> en=it.next();
System.out.println(en.getKey()+","+en.getValue());
}
//String names[] = map.get("name");
//以后用的BeanUtils工具会使用到getParameterMap()方法

//获取客户端的ip和端口号
String host=request.getRemoteHost();
int port=request.getRemotePort();
System.out.println(host+":"+port);

System.out.println(request.getContextPath());//输出: /项目名
}

}


结果:

服务器提交如下

Servlet---表单参数接收/简单的文件上传

MyEclipse的输出如下:
Servlet---表单参数接收/简单的文件上传

同样,通过点击“请求param” 的超链接发出doGet方式的请求结果如下:

Servlet---表单参数接收/简单的文件上传


二、文件上传

这里的文件上传只演示将文件当中的数据发送到服务器,暂时不重新接收成文件。

如下:

在index.jsp中加上如下几句话:

<form action="<%= request.getContextPath() %>/uploadHello" method="post" enctype="multipart/form-data">
file:<input type="file" name="file" /><br/>
<input type="submit" value="上传" />
</form>
注意:enctypr="multipart/form-data"是一定要加上去的,否则只能够传送文件名过去,因为enctype默认是application/x-www-form-urlencoded


UploadHelloServlet.java

package cn.hncu.servlets;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UploadHelloServlet extends HttpServlet {

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

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
InputStream in=request.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(in));
String line=null;
while ((line=br.readLine())!=null){
System.out.println(line);
}
}

}

结果如下:

浏览器:

Servlet---表单参数接收/简单的文件上传


MyEclipse输出:

Servlet---表单参数接收/简单的文件上传


这里演示的文件上传只能算是意思意思一下,以后我会做一个真正的文件上传发到博客。