Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Servlet parameters and doPut

Trying to get parameters from a PUT request using HttpServlet#doPut:

public void doPut(HttpServletRequest request, HttpServletResponse response) {
    String name = request.getParameter("name");
    // name is null
}

Using curl to send the request:

curl  -X PUT \
      --data "name=batman" \
      --header "Content-Type: text/plain" http://localhost:8080/sample.html

works fine with using doGet and GET curl request. Am I missing something?

like image 578
Erick Fleming Avatar asked May 13 '09 02:05

Erick Fleming


People also ask

What is parameter in servlet?

Request parameters are extra information sent with the request. For HTTP servlets, parameters are contained in the query string or posted form data. You should only use this method when you are sure the parameter has only one value. If the parameter might have more than one value, use getParameterValues(java.

Which are the two parameter of HTTP servlet?

An HTTP servlet gets its request parameters as part of its query string (for GET requests) or as encoded post data (for POST requests).

What is the difference between getParameter and getAttribute?

getParameter() returns http request parameters. Those passed from the client to the server. getAttribute() is for server-side usage only - you fill the request with attributes that you can use within the same request. For example - you set an attribute in a servlet, and read it from a JSP.

What are initialization parameters in servlets?

Initialization parameters are stored as key value pairs. They are included in web. xml file inside init-param tags. The key is specified using the param-name tags and value is specified using the param-value tags. Servlet initialization parameters are retrieved by using the ServletConfig object.


2 Answers

Based on comments and further research I realized that the Servlet cannot assume anything about the data being put onto the server and therefore, will not parse name/value pairs.

The following solution seems to be the proper way to handle any data passed via PUT, and can be parsed as XML, Name/Value, or whatever.

BufferedReader br = new BufferedReader(new InputStreamReader(req.getInputStream());

String data = br.readLine();
like image 172
Erick Fleming Avatar answered Oct 17 '22 11:10

Erick Fleming


Unlike in doGet() and doPost() methods, we are not able to get the request parameters using the getParameter() method in doPut() and doDelete() methods. We need to retrieve them manually from the input stream.

The following method retrieves request parameters and returns them in a map:

public static Map<String, String> getParameterMap(HttpServletRequest request) {

    BufferedReader br = null;
    Map<String, String> dataMap = null;

    try {

        InputStreamReader reader = new InputStreamReader(
                request.getInputStream());
        br = new BufferedReader(reader);

        String data = br.readLine();

        dataMap = Splitter.on('&')
                .trimResults()
                .withKeyValueSeparator(
                        Splitter.on('=')
                        .limit(2)
                        .trimResults())
                .split(data);

        return dataMap;
    } catch (IOException ex) {
        Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException ex) {
                Logger.getLogger(Utils.class.getName()).log(Level.WARNING, null, ex);
            }
        }
    }

    return dataMap;
}

The example uses Google's Guava library to parse the parameters. For a complete example containing doGet(), doPost(), doPut() and doDelete() methods, you can read my Using jsGrid tutorial.

like image 31
Jan Bodnar Avatar answered Oct 17 '22 12:10

Jan Bodnar