Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

after enctype="multipart/form-data" request not working

Tags:

java

jsp

servlets

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 servlet, I'm sending data from this form

<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 is throwing a java.lang.NullPointerException.

But works fine when I remove this:

enctype="multipart/form-data"
like image 397
Amal Prasad Avatar asked Oct 19 '12 13:10

Amal Prasad


2 Answers

Why do you need to add it then? Just keep it out.

If you need it in order to upload a file by <input type="file"> which you intend to add later on, then you should put @MultipartConfig annotation on your servlet, so that request.getParameter() will work and that all uploaded files can be retrieved by request.getPart().

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

See also:

  • How to upload files to server using JSP/Servlet?
like image 182
BalusC Avatar answered Sep 23 '22 13:09

BalusC


Parameters encoded with multipart/form-data are sent in POST body - not as regular request parameters, therefore can't be read using request.getParamter(...).

Check out Commons file upload package for multipart requests processing.

like image 22
Maciej Dragan Avatar answered Sep 25 '22 13:09

Maciej Dragan