7

I try to extract the form data from an javax.servlet.http.HttpServletRequest instance. As recommended online I tried request.getParameter(String paramKey), but it did not work. request.getParameterValues(), request.getParameterNames() and request.getParameterMap() return no form data as well. What I want is a Map with form data or another method to get them.

loresIpsos
  • 327
  • 1
  • 2
  • 12

3 Answers3

6

it will behave where you write the code request.getParameter(). this thing always needs to write in the doGetPost() Method of the servlet like mentioned below. refer the following example.

public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException  {

     String id = req.getParameter("realname");
     String password = req.getParameter("mypassword");
}

public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {

    String id = req.getParameter("realname");
    String password = req.getParameter("mypassword");
}
Ravi MCA
  • 2,491
  • 4
  • 20
  • 30
1

There are few things to configure.

  1. need to set "Content-Type: multipart/form-data" for the request
  2. need to set @MultipartConfig annotation for the class
  3. get Part from request.getPart("KEY");
  4. read the Stream

here is my example

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

@WebServlet("/")
@MultipartConfig
public class TestServlet extends HttpServlet {

    private static final long serialVersionUID = 5589752892736045780L;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        
        //get part by key
        Part part = req.getPart("override");
        
        //read the part
        InputStream inputStream = part.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }
        System.out.println(sb.toString());
        
        //set it to the response
        resp.setContentType("text/plain");
        resp.getWriter().write(sb.toString());
    }
}

Here is the Postman request enter image description here

Gayan Chinthaka
  • 521
  • 6
  • 5
0

There are several methods you need to override. They are doPost(), doGet(), service()

doPost() will get executed when the received request type is POST.

doGet() will get executed when the received request type is GET.

If you want a single method which should be executed for the both post, get you better to use service() method.

Example:

 public class TestServlet{

    public void service( HttpServletRequest request, HttpServletResponse response ){

        request.getParameter( "paramterName" ).
    }
 }
Ravi MCA
  • 2,491
  • 4
  • 20
  • 30