After the request, enctype = "multipart / form-data" does not work

public class Relay extends HttpServlet { @Override public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String command = request.getParameter("command"); RequestDispatcher rd =request.getRequestDispatcher(command); rd.forward(request, response); System.out.println("Request forwarded to " + command + " servlet"); } } 

This is my relay serv, I am sending a date from

  <form action="Relay" method="POST" enctype="multipart/form-data"> / <input type="hidden" name="command" value="AddProduct" /> <input type="text" name="pname" value="" /> <input name="" type="submit" value="Add Product"> </form> 

It provides a java.lang.NullPointerException exception. enctype = "multipart / form-data" // when im removes its work fine

+4
source share
3 answers

Why do you need to add it? Just go on.

If you need to upload the <input type="file"> that you plan to add later, you must put the @MultipartConfig annotation on your servlet so that request.getParameter() works and that all downloaded files can receive request.getPart()

 @WebServlet("/Relay") @MultipartConfig public class Relay extends HttpServlet { // ... } 

See also:

+7
source

Parameters encoded using multipart/form-data are sent to the POST body - not like normal request parameters, therefore they cannot be read using request.getParamter(...) .

Check out a package to download files to handle multiple requests.

+1
source

I turn this on only for more troubleshooting information. if you’re stuck and want to know that all the parameters come in a multi-profile request, you can print all the parameters using the following code.

 MultipartRequest multi = <Your code to retrieve multipart request goes here. Sorry but can not post code as I use proprietary APIs> Enumeration en1 = multi.getParameterNames(); while (en1.hasMoreElements()) { String strParamName = (String)en1.nextElement(); String[] strParamValues = multi.getParameterValues(strParamName); for (int i = 0; i < strParamValues.length; i++) { System.out.println(strParamName + "=" + strParamValues[i]); } } 
0
source

Source: https://habr.com/ru/post/1440780/


All Articles