简体   繁体   English

在文件上载期间从一个JSP获取标记值到另一个JSP

[英]Get the tag value from one JSP to another JSP during file uploading time

My project concept is to upload file to data base. 我的项目概念是将文件上传到数据库。 The below code is working fine but the problem is I can't get the <select> value from one page to another it always show null value. 下面的代码工作正常,但问题是我无法从一个页面到另一个页面获取<select>值,它总是显示null值。

How to get the <select> element value from one JSP to another JSP during file uploading time? 如何在文件上传期间将<select>元素值从一个JSP获取到另一个JSP?

  <FORM ENCTYPE="multipart/form-data" ACTION="upload_page.jsp" METHOD=POST>
       <center>
       <table border="0" bgcolor=#ccFDDEE>
       <tr><center><td colspan="2" align="center"><B>UPLOAD THE FILE</B>
      <center></td></tr>
       <tr><td colspan="2" align="center"> </td></tr>
       <tr><td><b>Choose the file To Upload:</b></td><td><INPUT NAME="file" TYPE="file"> </td></tr>
       <tr><td colspan="2" align="center"> </td></tr>
       <tr><td colspan="2" align="center"><input type="submit" value="Send File">            </td></tr>
       <tr>
       <td>
       <select name="t1">
      <option value="mars">Mars</option>
      <option value="moon">Moon</option>
      <option value="sun">Sun</option>
      <option value="earth">Earth</option>
      </select>
      </td>

******** 
<% String s1=request.getParameter("t1"); System.out.println(s1); 
********

You're submitting the form in multipart/form-data encoding. 您是以multipart/form-data编码提交表单。 This is different from default application/x-www-form-urlencoded encoding which getParameterXxx() methods by default use. 这与默认application/x-www-form-urlencoded默认application/x-www-form-urlencoded编码不同, getParameterXxx()方法。 They would all return null on multipart/form-data because multipart/form-data is by default not supported. 它们都会在multipart/form-data上返回null ,因为默认情况下不支持multipart/form-data

Basically, you need to use the very same API to extract the regular form fields as you're currently using to extract the uploaded file. 基本上,您需要使用相同的API来提取常规表单字段,因为您当前正在使用它来提取上传的文件。 Usually this is Apache Commons FileUpload, so I'm assuming that you're also using that. 通常这是Apache Commons FileUpload,所以我假设你也在使用它。 You should have somewhere an if (!fileItem.isFormField()) check to grab the file field. 您应该在某处使用if (!fileItem.isFormField())检查来获取文件字段。 You need to hook on the else to grab the normal form field. 您需要挂钩else以获取正常的表单字段。

Alternatively, if you're already on Servlet 3.0, submit the form to a normal servlet with the @MultipartConfig annotation and use getPart() method to get the file field and you can continue using getParameterXxx() methods to get the normal form field. 或者,如果您已经在Servlet 3.0上,请使用@MultipartConfig注释将表单提交给普通的servlet,并使用getPart()方法获取文件字段,然后您可以继续使用getParameterXxx()方法来获取正常的表单字段。

See also: 也可以看看:


Unrelated to the concrete problem, using 90's style uppercased HTML tag/attribute names and using the since 1998 deprecated <center> element and using discouraged styling attributes of HTML table elements instead of CSS doesn't give me the impression that you're learning HTML based on proper and up to date resources. 具体问题无关 ,使用90的样式大写HTML标记/属性名称并使用自1998年以来弃用的<center>元素并使用HTML表格元素而不是CSS的气馁样式属性不会让我觉得你正在学习HTML基于适当和最新的资源。 Ensure that you're doing that. 确保你这样做。 Also, submitting the form to a JSP and using scriptlets is disoucraged since a decade. 此外,从十年开始, 不允许将表单提交给JSP并使用scriptlet Ensure that you're also reading sane JSP resources. 确保您还阅读合理的JSP资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM