我正在尝试从嵌入在我的 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/