Question

I'm trying to get param values passed to a Java Servlet but the string returned is not correct. I'm storing the values in a Map and checking if the key exists.

Map params;
params = request.getParameterMap();

String id = params.get("id").toString();
String data = params.get("data").toString();

System.out.println("streaming" + data + " with id of " + id);

Yet if I call this servlet via http://localhost:8080/Serv/stream/data?data=hereisdata&id=you my output looks like this:

streaming[Ljava.lang.String;@5e2091d3 with id of [Ljava.lang.String;@36314ab8

What am I missing?

EDIT: as the suggested answers are not working, I'm including the entire class as I'm likely messing something up within the class:

import java.util.*;
import javax.servlet.http.HttpServletRequest;
import Engine.Streamer;


public class AnalyzerController {

    private Map params;
    private String pathInfo;
    private HttpServletRequest request;

    public AnalyzerController(HttpServletRequest request)
    {
        this.params = request.getParameterMap();
        this.pathInfo = request.getPathInfo();
    }

    public void processRequest()
    {
        System.out.println("procing with " + pathInfo);

        switch(pathInfo){
        case "/stream/data":
            if(params.containsKey("id") && params.containsKey("data")) processStream();
            break;
        }
    }

    private void processStream()
    {
        System.out.println("we are told to stream");
        String data = request.getParameter("data");
        String id = request.getParameter("id");
        Streamer stream = new Streamer();   

        stream.streamInput(data, "Analyzer", id);           
    }
}

This line specifically is throwing the NPE: String data = request.getParameter("data");

Was it helpful?

Solution

If you look at the docs of the Request#getParameterMap(), it returns a Map of the type Map<String, String[]>. Therefore, you need to take out the first element from the value String[] array returned from the map.

String id = params.get("id")[0];

Ofcourse, you can avoid all this and directly get the parameters from the request objects using the Request#getParameter() method.

String id = request.getParameter("id");

Edit: Looking at your class code, it seems that the instance variable request is not initialized. Initialize that in the constructor like this:

public AnalyzerController(HttpServletRequest request)
{
    this.request = request; // Initialize your instance variable request which is used in the other methods.
    this.params = request.getParameterMap();
    this.pathInfo = request.getPathInfo();
}

OTHER TIPS

You can get the required parameters instead of the whole map

String id = request.getParameter("id");
String data = request.getParameter("data");

Try something like this.

String data = ((String)params.get("data"));

Or directly from the Request.

String data = request.getParameter("data");

You can use request object plus it's method for to get data usinggetParameter() of you can use getParameterValues() if multiple data are from page.

String id = request.getParameter("id")
String data = request.getParameter("data")

why are you using Map ? Any special need of it or any reason ?

or you can use like this :

String id = params.get("id")[0];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top