2013-05-17 25 views
1

我使用下面的JSP文件中的代码,但我看到的隐藏字段的值是没有得到在此页面的源代码提交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> 

任何明显的我mignt在这里失踪?

+0

你的参数代码是什么? – Jason

+0

在java中执行req.getParameter()给出null – vishesh

+0

我想问题是'enctype =“multipart/form-data”'因为只有文件正在提交,没有其他字段被提交。 – vishesh

回答

2

你可以使用

String value = request.getParameter("fieldname").toString(); 

在你的情况下,隐藏字段,以及所有其他字段值获取隐藏的价值领域呐

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 =

+0

这只会给一个NullPointerException,因为req.getParameter(“csrfToken”)给出了空值 – vishesh

+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发送令牌值。

希望它能帮助你。