【问题标题】:File Uploading with form data from one jsp page to another jsp page将带有表单数据的文件从一个 jsp 页面上传到另一个 jsp 页面
【发布时间】:2015-10-14 06:13:14
【问题描述】:
我有两个jsp页面,在第一页我写html代码。在 html 中,我有两个文本框和一个文件上传控件和提交按钮的表单。
现在,当我单击提交按钮时,我想在第二个 jsp 页面中获取整个表单数据(文本框数据和文件路径)。
在第二个jsp页面我写了文件上传的代码。
我的问题是我没有在第二个 jsp 页面中获得这两个数据。我得到了文本框数据或文件。但我一次都想要。
所以请帮助我。
谢谢。
【问题讨论】:
标签:
javascript
java
jquery
html
jsp
【解决方案1】:
假设您将 myFile 名称定义为私有并在变量上应用 setter 和 getter 方法。这将为您提供一个文件名,无论您从上传控件上传了什么。
-
使用 taglib 获取 JSP 代码中的属性名称。
private File myFile; //give file name
private String TextValue; // give text field value
public File getMyFile() {
return myFile;
}
public void setMyFile(File myFile) {
this.myFile = myFile;
}
public String getTextValue() {
return TextValuee;
}
public void setTextValue(String TextValue) {
this.TextValue = TextValue;
}
然后转到 JSP 并使用属性名称代码定义 taglib 和值:-
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>File Upload Success</title>
</head>
<body>
You have successfully uploaded <s:property value="<s:property value="Myfile"/>"/>
with TextBox <s:property value="<s:property value="TextValue"/>"/>
</body>
</html>
【解决方案2】:
这样试试
first.jsp
<form action='secondjsp.jsp' method='post' enctype='multipart/form-data'>
<input type='textbox' name="textbox"/>
<input type='submit' value='submit' />
</form>
second.jsp
<%
String file = request.getParameter('textbox');
%>