java - 在表单中使用 multipart/form-data enctype 时无法将参数传递给 servlet

标签 java forms jsp servlets

我正在尝试从嵌入在我的 jsp 文件中的文本编辑器传递格式化文本。我在表单标签中使用 enctype="multipart/form-data"。使用默认 enctype 时,我可以将参数传递给底层 servlet。但在我的 servlet 中使用 multipart/form-data enctype 时,我得到 null。

我的表格

<form action="pdfGenServlet" method="post" enctype="multipart/form-data">
                    <!-- input notes title-->
                    <div class="form-group">
                        <div class="input-group">
                            <input type="text" class="form-control" placeholder="Title of the notes" name="title">
                        </div>
                    </div>
                    <!-- input notes description-->
                    <div class="form-group">
                        <div class="input-group">
                            <input type="text" class="form-control" placeholder="Enter short description" name="description">
                        </div>
                    </div>

                    <div class="form-group">
                      <textarea name="content" id="myEditor"></textarea>

                     <div id="button-panel" class="panel panel-default">
                          <p>
                              <button type="submit" class="btn btn-primary "><span class="glyphicon glyphicon-plus"></span><strong> Create Note</strong></button>
                              <button class="btn btn-primary" type="reset"><strong>Reset</strong></button>
                          </p><!-- buttons -->
                     </div><!-- panel Button -->

                    </div>

                </form> 

我的 pdfGenServlet.java

  @WebServlet("/pdfGenServlet")
 public class pdfGenServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      try {
            // Get the text that will be added to the PDF
            request.setCharacterEncoding("UTF-8");

            String title = request.getParameter("title");
            String description = request.getParameter("description");
            String notes_content = request.getParameter("content");
            Date date = new Date();
        } catch(exception e)
        {e.printStackTrace();}
      }

最佳答案

如果您想从 HTML(JSP) 表单获取参数,请使用内容属性“enctype="multipart/form-data"”。

  • 您应该在将接收所有这些参数的 servlet 中添加 @MultipartConfig(同时在 Convey/Dispatch Servlet 中添加 @MutipartConfig)。
  • @MultipartConfig中包含三个元素:fileSizeThreshold、maxFileSize、maxRequestSize;
  • (我使用Servlet 3 File Upload来上传文件)

关于java - 在表单中使用 multipart/form-data enctype 时无法将参数传递给 servlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28902693/

相关文章:

java - Spring Cloud Gateway - 尝试读取 Web 过滤器中的请求正文时请求卡住

javascript - 对 Spring Boot 端点的 Ajax 请求无法读取 HTTP MSG

javascript - PHP循环与html表单和javascript提交按钮仅提交循环中的最后一个表单

c# - 我可以提高 Windows 窗体的分辨率/DPI 吗? (一些扩大控制的方法?)

java - response.sendRedirect 的 IllegalStateException

Java 正则表达式匹配单词或标点符号

java - JScrollPane 屏幕撕裂

javascript - 简单的 javascript 函数在表单内不起作用

jsp - JasperException : absolute uri: http://java. sun.com/jsp/jSTL/core 无法解析

java - Struts 2.0 - 动态附加 s :property value