user54517
user54517

Reputation: 2430

Java web service request parameters returned as null

I am developing a web service using Eclipse, and in order to try it I launch the tomcat server and try an http request with parameters. The problem is that it seems that the parameters I give are ignored :

enter image description here

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        HttpSession session = request.getSession();
        if(session.getAttribute("lat") == null && session.getAttribute("lon") == null) {
            session.setAttribute("lat", request.getAttribute("lat"));
            session.setAttribute("lon", request.getAttribute("lon"));
            response.setContentType("text/plain");
            response.getWriter().append("RECEIVED");
        }
        else {

With the debugger, I can see that the object request doesn't contain my parameters.

http

Upvotes: 1

Views: 551

Answers (1)

Ryuzaki L
Ryuzaki L

Reputation: 40078

You are trying to get HttpSession attribute but not the parameters passed in URL. You need to use

request.getParameter("lat");

Returns the value of a request parameter as a String, or null if the parameter does not exist. Request parameters are extra information sent with the request. For HTTP servlets, parameters are contained in the query string or posted form data.

To know the difference between session attributes and request params, please refer here

you can also get all parameters in Map

Map<String,String[]> getParameterMap()

Returns a java.util.Map of the parameters of this request.

Request parameters are extra information sent with the request. For HTTP servlets, parameters are contained in the query string or posted form data.

Upvotes: 1

Related Questions