Fileupload 处理表单问题

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

登陆购买课程后可参与讨论,去登陆

9回答
提问者 慕粉3355010 2018-11-25 16:29:00
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<%@ 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>


提问者 慕粉3355010 2018-11-25 16:27:04
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
<%@ 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>


提问者 慕粉3355010 2018-11-25 16:26:01

<%@ 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>


提问者 慕粉3355010 2018-11-25 16:25:03
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
<%@ 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>


提问者 慕粉3355010 2018-11-25 16:24:03
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
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);
}
}


提问者 慕粉3355010 2018-11-25 16:21:14
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
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);
}
}


提问者 慕粉3355010 2018-11-25 16:09:17
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
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;
}
}


提问者 慕粉3355010 2018-11-25 16:04:52
一叶知秋519 2018-11-25 11:43:15

获取到的应该是空字符串;建议同学将你的测试代码贴一下,结合你的测试我们帮助你解决一下~

在实际开发中,在对对象进行操作时,都会进行空值的判定,同学担心在使用的过程中会出现空指针么?

祝学习愉快!

  • 提问者 慕粉3355010 #1
    您好 代码有点多 servlet上传的时候一直在加载 不显示上传 怎么办!!!
    2018-11-25 16:35:02
  • 一叶知秋519 回复 提问者 慕粉3355010 #2
    同学你好,你的代码已经上传了,请同学把报错信息一并贴出来,另外说明一下每个jsp页面的名称,方便老师运行你的代码解决问题,祝学习愉快!
    2018-11-26 11:29:27
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
从网页搭建入门Java Web2018版
  • 参与学习           人
  • 提交作业       1088    份
  • 解答问题       10204    个

如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!

了解课程
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师
插入代码