Fileupload 处理表单问题
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | <%@ page language= "java" contentType= "text/html; charset=utf-8" pageEncoding= "utf-8" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" > <html> <head> <meta http-equiv= "Content-Type" content= "text/html; charset=utf-8" > <title>测试</title> </head> <body> <center> <!-- <form action= "/Foods/IndexServlet" method= "post" > --> <form action= "/Foods/IndexServlet" method= "post" enctype= "multipart/form-data" > <table> <tr> <td>用户名</td> <td> <input type= "text" name= "username" > </td> </tr> <tr> <td>密码</td> <td> <input type= "text" name= "password" > </td> </tr> <tr> <td>性别</td> <td> <input type= "radio" name= "sex" value= "男" >男 <input type= "radio" name= "sex" value= "女" >女 </td> </tr> <tr> <td> <input type= "submit" value= "提交" > <input type= "reset" value= "重置" > </td> </tr> </table> </form> </center> </body> </html> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | package com.imooc.servlet; import java.io.IOException; import java.util.List; 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; /** * Servlet implementation class IndexServlet */ @WebServlet ( "/IndexServlet" ) public class IndexServlet extends HttpServlet { private static final long serialVersionUID = 1L; public IndexServlet() { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory(); ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory); try { List<FileItem> list = (List<FileItem>)servletFileUpload.parseRequest(request); for (FileItem fileItem : list){ if (fileItem.isFormField()){ //普通表单项 String name = fileItem.getFieldName(); String value = fileItem.getString( "utf-8" ); System.out.println( "name:" +name); System.out.println( "value:" +value); if (value == "" ){ System.out.println( "1" ); } else if (value == null ){ System.out.println( "2" ); } else { System.out.println( "3" ); } } } } catch (FileUploadException e) { // TODO Auto-generated catch block e.printStackTrace(); } /*String username = request.getParameter("username"); String password = request.getParameter("password"); String sex = request.getParameter("sex"); System.out.println("username:"+username); System.out.println("password:"+password); System.out.println("sex:"+sex); if(username == ""){ System.out.println("1"); }else if(username == null){ System.out.println("2"); }else{ System.out.println("3"); } if(password == ""){ System.out.println("4"); }else if(password == null){ System.out.println("5"); }else{ System.out.println("6"); }*/ } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } //表单提交问题,使用最基本的表单提交时,如果什么都不填写,直接点击提交,在servlet处理的时候,request.getParameter("username")和request.getparameter("password"),对应的打印值为"",性别选择的打印值为null。而在使用fileupload提交表单时,表单什么也不写,对应的fileItem.getString("UTF-8")打印,既不是""也不是null,麻烦帮忙看一下。 |
0
收起
正在回答 回答被采纳积分+1
1回答
从网页搭建入门Java Web2018版
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10204 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧