我在JSP文件中使用以下代码,但我发现隐藏字段的值未提交
<form action="/processor/RequestActivityByCsv" method="post" enctype="multipart/form-data">
<div class="inputDiv">
<h5>comma delimited file: <input type="file" name="file"/></h5>
<input name="csrfToken" type="hidden" value="<%=CryptoUtils.generateCsrfToken()%>" />
<input type="submit" value="upload" class="btn btn-primary"/>
</div>
</form>
页面源中的来自:
<form action="/processor/RequestActivityByCsv" method="post" enctype="multipart/form-data">
<div class="inputDiv">
<h5>comma delimited file: <input type="file" name="file"/></h5>
<input name="csrfToken" type="hidden" value="t5ipRVFNIP83IUh5NCf7PiTp4mM2kBFVdHjqwlGx7PI=" />
<input type="submit" value="upload" class="btn btn-primary"/>
</div>
</form>
任何明显的我在这里都会失踪?
答案 0 :(得分:2)
您可以使用
获取隐藏字段以及所有其他字段值String value = request.getParameter("fieldname").toString();
在您获取字段na
的隐藏值的情况下String csrfToken= request.getParameter("csrfToken").toString();
更新2
<强>的index.jsp 强>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form action="test.jsp" method="get">
<input type="hidden" name="csrfToken" id="csrfToken" value="t5ipRVFNIP83IUh5NCf7PiTp4mM2kBFVdHjqwlGx7PI=" />
<input type="submit" value="sumbit"/>
</form>
</body>
</html>
<强> test.jsp的强>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
String csrfToken= (String)request.getParameter("csrfToken");
out.print(csrfToken);
%>
</body>
</html>
<强>输出强>
t5ipRVFNIP83IUh5NCf7PiTp4mM2kBFVdHjqwlGx7PI =
答案 1 :(得分:0)
是的,我之前遇到过这个问题。
试试这个: -
<form name="frmfileupload" action="/processor/RequestActivityByCsv" method="post" enctype="multipart/form-data">
<div class="inputDiv">
<h5>comma delimited file: <input type="file" name="file"/></h5>
<input name="csrfToken" type="hidden" value="<%=CryptoUtils.generateCsrfToken()%>" />
<input type="button" value="upload" onclick="funSubmit()" class="btn btn-primary"/>
</div>
</form>
使用Java脚本提交表单: -
<script >
function funSubmit(){
//You can do javascript validation here
var val=document.frmfileupload.csrToken.value;
document.frmfileupload.action="<%=request.getContextPath()%>/processor/RequestActivityByCsv?csrToken="+val;
document.frmfileupload.submit();
}
</script>
在上面的java脚本中,我使用URL发送令牌值。
希望它会对你有所帮助。