1

我在模态中有以下形式:

    <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>

我想让它发送到以下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);
   }
%>

我遇到了参数没有正确过来的问题。我的 System.out 说:

null

就是这样。它显然没有得到其他输入(电子邮件、hstnme 和端口)。null 来自第一个 .getParameter("hstnme")

我认为主机名可能会以某种方式受到保护,所以我将其更改为 hstnme,但没有运气。我还注意到,当我使用 commons.fileupload 时,所有四个参数都被制成 FileItems,但它们的值不会一起发送。

如何将表单中的参数传递给 jsp 并正确检索它们?

4

2 回答 2

3

我建议您使用 servlet 来处理帖子,有很多方法可以做到这一点,但一种简单的方法是使用Apache Commons FileUpload库。您只需要将 JAR 添加到您的项目中。

这是一个示例,它将获取您的信息并以 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();
    }
}

注意FileItem fi是包含字段信息的对象。对于文件,您可以使用fi.getInputStream()fi.getString()根据您想要获取它的方式以及多少数据等来获取其内容。

于 2013-06-07T22:38:23.780 回答
0

这是解决您的问题的方法。我已经在我的机器上进行了本地测试,它似乎可以工作:

HTML 表单

<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>

上传.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);
   }
%>

简而言之,修复是:

  • 从表单中删除 HTTP 方法。
  • 使用获取upload.jsp中的值getParameter

希望这可以帮助。

于 2013-06-08T14:41:03.153 回答