obtenir null sur request.getParameter () lors de la soumission du formulaire en code java?
-
06-07-2019 - |
Question
IN JAVA CODE IN JSP as below i m getting null value for field"noOfRecords".
if (request.getMethod().equalsIgnoreCase("POST")) {
try
{
noOfRecords=Integer.parseInt(request.getParameter("noOfRecords").trim());
}
catch(NumberFormatException e)
{
throw new Exception("No of records Field should be numeric.");
}
catch(NullPointerException e)
{
throw new Exception("No of records should be specified.");
}
LE CODE HTML EST COMME CI-DESSOUS.
<BODY leftmargin="0" topmargin="0" marginheight="0" marginwidth="0" bgcolor="#ffffff" onLoad="document.customerFileUpload.filePath.focus();">
<FORM method="post" name="customerFileUpload" action="CustomerFileUpload.jsp" onSubmit="return validate(this);" enctype="multipart/form-data">
<table width="98%" border="0" cellpadding="2" cellspacing="1" align="center">
<tr>
<td width="30%" class="bandcolorlabel">Select File *:</td>
<td width="70%"><input type="FILE" name="filePath" class="ibox" size="30"></td>
</tr>
<tr>
<td width="30%" class="bandcolorlabel">No Of Records</td>
<td width="70%"><input type="text" name="noOfRecords" class="ibox" size="30"></td>
</tr>
</table>
<table width="100%" border="0" cellpadding="2" cellspacing="1">
<tr>
<td><input type="submit" class="btn" value="Upload" >
</td>
</tr>
</table>
</form>
</BODY>
La solution
Etant donné que vous postez en utilisant un codage en plusieurs parties ("multipart / form-data"), les paramètres ne sont pas présents comme prévu.
Si, par exemple, vous utilisez commons-fileupload , les paramètres seraient présents en tant que et sont identifiables. en utilisant la méthode 'isFormField' sur l'objet FileItem.
Cet article sur coderanch explique comment: coderanch
La plupart (tous) les webframeworks modernes résument ce problème et le rendent beaucoup plus facile en passant.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow