Fileupload 处理表单问题
Fileupload 处理普通表单项的时候,如果我在input中没有输入内容,然后提交表单,那么我后台是否能够获得name的值和此input中的值,如果通过fileItem.getFieldName();可以获取到name值,那对应的fileItem.getString("UTF-8");是什么。null还是"" ? 我自己测试过,在input中不出入内容,直接提交表单,除了<input type="file">以外,<input type="text">可以获取到name,但是对应的input的内容不是null,也不是"",<input type="radio"> 压根儿就获取不到name。 麻烦帮忙看一下。
正在回答 回答被采纳积分+1
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.util.*" %> <%@ page import="com.imooc.models.*" %> <% List<Food> list = (List<Food>)this.getServletContext().getAttribute("selectFoodList"); System.out.print("list:"+list); %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <style type="text/css"> tr { text-align: center; } </style> </head> <body> <center> <h2>菜品查询</h2> <table border="1px" cellpadding="0px" cellspacing="0px" width="80%"> <tr> <td>菜品ID</td> <td>菜名</td> <td>口味</td> <td>菜品图片</td> <td>价格</td> <td>菜品描述</td> </tr> <% for(Food food : list){ String path = food.getFoodImage(); int num = path.lastIndexOf("/"); String a = path.substring(num+1); String b = "/Foods/upload/" + a; %> <tr> <td><%=food.getId() %></td> <td><%=food.getFoodName() %></td> <td><%=food.getTaste() %></td> <td> <%-- <img src="<%=food.getFoodImage() %>"> --%> <img src="<%=b %>"> </td> <td><%=food.getPrice() %></td> <td><%=food.getDescription() %></td> </tr> <% } %> </table> </center> </body> </html> /********************************/ <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <center> <h1>菜品管理系统</h1> </center> </body> </html> /*********************************/ <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <center> <form action="/Foods/UpdateFoodServlet" method="post" enctype="multipart/form-data"> <table border="1px" width="400px" cellspacing="0px" cellpadding="0px"> <tr> <td>菜品ID</td> <td> <input type="text" name="id"> </td> </tr> <tr> <td>菜名</td> <td> <input type="text" name="foodName"> </td> </tr> <tr> <td>口味</td> <td> <input type="radio" name="taste" value="香辣">香辣 <input type="radio" name="taste" value="微辣">微辣 <input type="radio" name="taste" value="麻辣">麻辣 <input type="radio" name="taste" value="不辣">不辣 </td> </tr> <tr> <td>菜品图片</td> <td> <input type="file" name="foodImage"> </td> </tr> <tr> <td>价格</td> <td> <input type="text" name="price"> </td> </tr> <tr> <td>菜品描述</td> <td> <textarea name="description"></textarea> </td> </tr> <tr> <td colspan="2" style="text-align: center; width="20px""> <input type="submit" value="修改"> <input type="reset" value="重置"> </td> </tr> </table> </form> </center> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <center> <form action="/Foods/SelectFoodByNameServlet" method="post"> <p> <input type="text" id="searchName" name="searchName" placeholder="请输入菜名进行查询"> <input type="submit" value="查询"> </p> </form> </center> </body> </html> /**********************/ <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.util.*" %> <%@ page import="com.imooc.models.*" %> <% List<Food> list = (List<Food>)this.getServletContext().getAttribute("foodList"); %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>菜品查询</title> <style type="text/css"> tr { text-align: center; } </style> </head> <body> <center> <h2>菜品查询</h2> <table border="1px" cellpadding="0px" cellspacing="0px" width="80%"> <tr> <td>菜品ID</td> <td>菜名</td> <td>口味</td> <td>菜品图片</td> <td>价格</td> <td>菜品描述</td> </tr> <% for(Food food : list){ String path = food.getFoodImage(); int num = path.lastIndexOf("/"); String a = path.substring(num+1); String b = "/Foods/upload/" + a; %> <tr> <td><%=food.getId() %></td> <td><%=food.getFoodName() %></td> <td><%=food.getTaste() %></td> <td> <%-- <img src="<%=food.getFoodImage() %>"> --%> <img src="<%=b %>"> </td> <td><%=food.getPrice() %></td> <td><%=food.getDescription() %></td> </tr> <% } %> </table> </center> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>菜品管理系统</title>
</head>
<frameset rows="20%, 80%">
<frame src="./top.jsp"></frame>
<frameset cols="20%, 80%">
<frame src="./left.jsp"></frame>
<frame name="main"></frame>
</frameset>
</frameset>
</html>
/*********************/
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<p><a href="addFood.jsp" target="main">菜品添加</a></p>
<p><a href="selectFood.jsp" target="main">菜品查询</a></p>
<p><a href="updateFood.jsp" target="main">菜品修改</a></p>
<p><a href="deleteFood.jsp" target="main">菜品删除</a></p>
</body>
</html>
/******************************/
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>菜品查询</title>
</head>
<body>
<center>
<p><a href="/Foods/showFoodList.jsp">查询所有菜系</a></p>
<p><a href="/Foods/selectFoodByName.jsp">按条件查询菜系</a></p>
</center>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>菜品添加</title> </head> <body> <center> <h2>菜品添加</h2> <form action="/Foods/AddFoodServlet" method="post" enctype="multipart/form-data"> <table border="1px" width="400px" cellspacing="0px" cellpadding="0px"> <tr> <td>菜品ID</td> <td> <input type="text" name="id"> </td> </tr> <tr> <td>菜名</td> <td> <input type="text" name="foodName"> </td> </tr> <tr> <td>口味</td> <td> <input type="radio" name="taste" value="香辣">香辣 <input type="radio" name="taste" value="微辣">微辣 <input type="radio" name="taste" value="麻辣">麻辣 <input type="radio" name="taste" value="不辣">不辣 </td> </tr> <tr> <td>菜品图片</td> <td> <input type="file" name="foodImage"> </td> </tr> <tr> <td>价格</td> <td> <input type="text" name="price"> </td> </tr> <tr> <td>菜品描述</td> <td> <textarea name="description"></textarea> </td> </tr> <tr> <td colspan="2" style="text-align: center; width="20px""> <input type="submit" value="添加"> <input type="reset" value="重置"> </td> </tr> </table> </form> </center> </body> </html>
package com.imooc.servlet; import java.io.IOException; import java.util.ArrayList; 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 com.imooc.models.Food; /** * 条件查询Servlet */ @WebServlet("/SelectFoodByNameServlet") public class SelectFoodByNameServlet extends HttpServlet { private static final long serialVersionUID = 1L; public SelectFoodByNameServlet() { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { List<Food> selectFoodList = new ArrayList<Food>(); selectFoodList.clear(); List<Food> list = (List<Food>)this.getServletContext().getAttribute("foodList"); String searchName= request.getParameter("searchName"); searchName = new String(searchName.getBytes("ISO8859-1"),"UTF-8"); System.out.println("searchName:"+searchName); for(Food food : list) { if(searchName.equals(food.getFoodName())) { selectFoodList.add(food); } } this.getServletContext().setAttribute("selectFoodList", selectFoodList); request.getRequestDispatcher("/showFoodListByName.jsp").forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
package com.imooc.servlet; import java.io.IOException; import java.util.ArrayList; 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 com.imooc.models.Food; /** * 初始化Servlet */ public class InitServlet extends HttpServlet { private static final long serialVersionUID = 1L; public InitServlet() { } @Override public void init() throws ServletException { System.out.println("int....."); List<Food> list = new ArrayList<Food>(); this.getServletContext().setAttribute("foodList", list); } }
package com.imooc.models; public class Food { private String id; private String foodName; private String taste;//口味 private String foodImage;//菜品图片 private String price; private String description;//菜品描述 public String getId() { return id; } public void setId(String id) { this.id = id; } public String getFoodName() { return foodName; } public void setFoodName(String foodName) { this.foodName = foodName; } public String getTaste() { return taste; } public void setTaste(String taste) { this.taste = taste; } public String getFoodImage() { return foodImage; } public void setFoodImage(String foodImage) { this.foodImage = foodImage; } public String getPrice() { return price; } public void setPrice(String price) { this.price = price; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } @Override public String toString() { return "Food [id=" + id + ", foodName=" + foodName + ", taste=" + taste + ", foodImage=" + foodImage + ", price=" + price + ", description=" + description + "]"; } } /******************************/ package com.imooc.models; import java.util.UUID; public class UUidName { public static String setUUIDName(String name) { int num = name.lastIndexOf("."); String lastName = name.substring(num); String firstName = UUID.randomUUID().toString(); String UUIDName = firstName.replace("-", "")+lastName; return UUIDName; } }
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星