3

I m trying to pass formatted text from a text-editor that has been embedded in my jsp file. I m using enctype="multipart/form-data" in my form tag. I can pass the parameters to underyling servlet when using the default enctype. But I get null when using the multipart/form-data enctype in my servlet.

My form

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

My 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();}
      }
Kamlesh Sharma
  • 222
  • 1
  • 7
  • 23

3 Answers3

2

If you want to get parameters from HTML(JSP) form that content attribute " enctype="multipart/form-data" ".

  • You should add @MultipartConfig in a servlet that will receive all these parameters (Also add @MutipartConfig in the Convey/Dispatch Servlet).
  • In @MultipartConfig contains three elements :fileSizeThreshold, maxFileSize, maxRequestSize;
  • (I used Servlet 3 File Upload for upload file)
1

Refer to the following link it will help you to upload image with input parameters:

http://www.avajava.com/tutorials/lessons/how-do-i-upload-a-file-to-a-servlet.html

Brijesh Bhatt
  • 3,810
  • 3
  • 18
  • 34
1

I solve this problem by using below code. You should add @MultipartConfig annotation into your Servlet class: For Example:

@WebServlet("/admin/create_book")
@MultipartConfig(
        fileSizeThreshold = 1024 * 10,  // 10 KB
        maxFileSize = 1024 * 300,       // 300 KB
        maxRequestSize = 1024 * 1024    // 1 MB 
)
public class CreateBookServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    public CreateBookServlet() {
        super();
    }

    
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BookServices bookServices = new BookServices(request, response);
        bookServices.createBook();
    }

}
Dharman
  • 30,962
  • 25
  • 85
  • 135
Luan Pham
  • 76
  • 4