为什么我只打印这些?后面list跟map里的打印不出来?
package servlet; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import bean.Users; @WebServlet("/registServlet") public class RegistServlet extends HttpServlet { private static final long serialVersionUID = 1L; public RegistServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { //数据接收 //文件上传的基本操作 Map<String, String> map = new HashMap<String, String>(); //1. 创建一个磁盘文件想的工厂 DiskFileItemFactory fac = new DiskFileItemFactory(); //2.创建一个核心解析类 ServletFileUpload ser = new ServletFileUpload(fac); //3.解析request请求,返回的是一个list的集合,list集合中存放的是fileitem对象 List<FileItem> list = ser.parseRequest(request); List<String> hobbyList = new ArrayList<String>(); //4.遍历集合,获得每个fileitem,判断是表单项还是文件上传项 for (FileItem it : list) { //判断是表单项还是文件上传项 if(it.isFormField()){ //普通项 //接收表单项的参数 String name = it.getFieldName(); String value = it.getString("utf-8"); System.out.println(name+" "+ value); if("hobby".equals(name)){ String hobbyValue = it.getString("utf-8"); hobbyList.add(hobbyValue); System.out.println(name+" " +hobbyList); map.put(name, hobbyValue); }else{ map.put(name, value); } }else{ //文件上传项 //文件上传功能 } System.out.println(map); } Users u = new Users(); u.setUsername(map.get("username")); u.setPassword(map.get("password")); u.setSex(map.get("sex")); u.setNickname(map.get("nickname")); u.setHobby(map.get("hobby")); u.setPath(map.get("path")); List<Users> userList = (List<Users>) this.getServletContext().getAttribute("list"); userList.add(u); for (Users users : userList) { System.out.println(users); } this.getServletContext().setAttribute("list", userList); } catch (FileUploadException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
15
收起
正在回答 回答被采纳积分+1
2回答
qq_哭的狠丶低调_0
2017-10-31 19:52:38
username s
password sa
nickname a
sex male
hobby 篮球
hobby 足球
hobby 排球
为什么只打印这些?????????
Java Web基础入门2018版
- 参与学习 716 人
- 提交作业 185 份
- 解答问题 1363 个
会Java?懂前端基础?想学后台开发?那么,赶快来学习《Java Web入门》路径吧。本路径主要介绍Java Web的基础知识,并配有大量案例,定会让你收获多多!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星