Reputation: 442
I have the following form inside a modal:
<div id="sazModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="sazModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="sazModalLabel">Upload a Test</h3>
</div>
<div class="modal-body">
<form method=POST id='sazForm' class="form-horizontal" action="upload.jsp" enctype='multipart/form-data'>
<div class="control-group">
<label class="control-label" for="inputEmail">Email</label>
<div class="controls">
<input name="email" type="text" id="inputEmail" placeholder="Email">
</div>
</div>
<div class='control-group'>
<label class='control-label' for='inputHost'>Test Server</label>
<div class='controls'>
<input name="hstnme" type='text' id='inputHost' placeholder='Hostname'>
</div>
</div>
<div class='control-group'>
<label class='control-label' for='inputPort'>Port Number</label>
<div class='controls'>
<input name="port" type='text' id='inputPort' placeholder='Port'>
</div>
</div>
<div class="control-group">
<label class="control-label" for="fileUploadButton">Saz File</label>
<div class="controls">
<input name="saz" type="file" id="fileUploadButton" placeholder="Saz File"/>
</div>
</div>
<div id='modalfooter'>
<input class="btn btn-success" type='submit' id='goButton' value="Go!"/>
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
</div>
</form>
</div>
</div>
And I would like to have it send to the following jsp (upload.jsp):
<%@ page import="java.io.*,java.util.*, javax.servlet.*"%>
<%@ page import="javax.servlet.http.*"%>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="org.apache.commons.fileupload.disk.*"%>
<%@ page import="org.apache.commons.fileupload.servlet.*"%>
<%@ page import="org.apache.commons.io.output.*"%>
<%
System.out.println(request.getParameter("hstnme"));
Enumeration<String> paramNames = request.getParameterNames();
while(paramNames.hasMoreElements()) {
String paramName = (String)paramNames.nextElement();
System.out.print(paramName + " : ");
String paramValue = request.getHeader(paramName);
System.out.println(paramValue);
}
%>
I'm running into the problem that the parameters are not coming over properly. My System.out says:
null
and that's it. It's clearly not getting the other inputs (email, hstnme, and port). The null comes from the first .getParameter("hstnme")
I thought that hostname might be protected in some way, so I changed it to hstnme, with no luck. I also notice that when I use the commons.fileupload, all four parameters are made into FileItems, but their values are not sent along.
How do I pass the parameters in my form to the jsp and retrieve them correctly?
Upvotes: 1
Views: 20410
Reputation: 5654
Here is the fix for your issue. I have tested in locally on my machine and it seems to work:
HTML form:
<form id='sazForm' class="form-horizontal" action="upload.jsp" enctype='multipart/form-data'>
<div class="control-group">
<label class="control-label" for="inputEmail">Email</label>
<div class="controls">
<input name="email" type="text" id="inputEmail" placeholder="Email">
</div>
</div>
<div class='control-group'>
<label class='control-label' for='inputHost'>Test Server</label>
<div class='controls'>
<input name="hstnme" type='text' id='inputHost' placeholder='Hostname'>
</div>
</div>
<div class='control-group'>
<label class='control-label' for='inputPort'>Port Number</label>
<div class='controls'>
<input name="port" type='text' id='inputPort' placeholder='Port'>
</div>
</div>
<div class="control-group">
<label class="control-label" for="fileUploadButton">Saz File</label>
<div class="controls">
<input name="saz" type="file" id="fileUploadButton" placeholder="Saz File"/>
</div>
</div>
<div id='modalfooter'>
<input class="btn btn-success" type='submit' id='goButton' value="Go!"/>
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
</div>
</form>
upload.jsp:
<%@ page import="java.io.*,java.util.*, javax.servlet.*"%>
<%@ page import="javax.servlet.http.*"%>
<%
System.out.println(request.getParameter("hstnme"));
Enumeration<String> paramNames = request.getParameterNames();
while(paramNames.hasMoreElements()) {
String paramName = (String)paramNames.nextElement();
System.out.print(paramName + " : ");
String paramValue = request.getParameter(paramName);
System.out.println(paramValue);
}
%>
Inshort the fix was:
getParameter
Hope this helps.
Upvotes: 0
Reputation: 8771
I suggest you to use a servlet to handle the post, there is many way of doing it but one easy is to use the Apache Commons FileUpload library. You only need to add the JAR to your project.
Here is an example that will get your informations and print content in HTML :
@WebServlet(urlPatterns = { "/file-upload" } )
public class FileUpload extends HttpServlet
{
@Override
public void doPost(HttpServletRequest p_oRequest, HttpServletResponse p_oResponse) throws IOException
{
PrintWriter out = p_oResponse.getWriter();
out.println("<html><body>");
List fileItems = null;
// Parsing field values
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(10000000);
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// maximum file size to be uploaded.
upload.setSizeMax(10000000);
try
{
// Parse the request to get file items.
fileItems = upload.parseRequest(p_oRequest);
// Process the uploaded file items
Iterator i = fileItems.iterator();
out.println("<table><tr><td>Type</td><td>Name</td><td>Value</td>");
while(i.hasNext())
{
FileItem fi = (FileItem)i.next();
out.println("<tr>");
if(fi.isFormField())
{
out.println("<td>Field</td>");
out.println("<td>" + fi.getFieldName() + "</td>");
out.println("<td>" + fi.getString() + "</td>");
}
else
{
out.println("<td>File</td>");
out.println("<td>" + fi.getFieldName() + "</td>");
out.println("<td>" + fi.getName() + " / " + fi.getContentType() + " / " + fi.getSize() + "</td>");
}
out.println("</tr>");
}
out.println("</table>");
}
catch(Exception e)
{
e.printStackTrace();
}
out.println("</body></html>");
out.flush();
}
}
Note the FileItem fi
is the object that contains field information. For the file, you can get its content with fi.getInputStream()
or fi.getString()
depending on how you want to get it and how much data, etc.
Upvotes: 3