Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get query parameters from HttpServletRequest (Tomcat 9.0)?

The server recieves requests from two clients - Raspberry Pi and Android app, both send requests using HttpURLConnection. I need to pass parameters with theese requests, e.g:

http://192.168.0.10:8080/MyProject/MyServer/rpi/checktask?rpi="rpi"

doing it as:

String requestUrl = "http://192.168.0.10:8080/MyProject/MyServer/rpi";
String query = String.format("/checktask?rpi=%s",
                        URLEncoder.encode("rpi", "UTF-8"));
URL url = new URL(requestUrl + query);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.setRequestProperty("Accept-Charset", "UTF-8");
conn.connect();

The Servlet has annotation:

@WebServlet(name = "MyServer", urlPatterns = { "/MyServer/rpi/*", "/MyServer/app/*"})

But when Servlet gets request as above following happens:

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

String path = request.getRequestURI();     // /MyProject/MyServer/rpi/*
String query = request.getQueryString();   // null
String context = request.getContextPath(); // /MyProject
String servlet = request.getServletPath(); // /MyServer/rpi
String info = request.getPathInfo();       // /*
}

Although according to those answers: How to use @WebServlet to accept arguments (in a RESTFul way)? and How come request.getPathInfo() in service method returns null?

it should look like this:

String path = request.getRequestURI();     // /MyProject/MyServer/rpi//checktask?rpi="rpi"
String query = request.getQueryString();   // rpi="rpi"
String context = request.getContextPath(); // /MyProject
String servlet = request.getServletPath(); // /MyServer/rpi
String info = request.getPathInfo();       // /checktask?rpi="rpi"

What am I doing wrong?

like image 638
Katrikken Avatar asked Sep 15 '16 11:09

Katrikken


People also ask

How do I get query parameters from request?

Your query parameters can be retrieved from the query object on the request object sent to your route. It is in the form of an object in which you can directly access the query parameters you care about. In this case Express handles all of the URL parsing for you and exposes the retrieved parameters as this object.

How do I get all request parameters?

To get all request parameters in java, we get all the request parameter names and store it in an Enumeration object. Our Enumeration object now contains all the parameter names of the request. We then iterate the enumeration and get the value of the request given the parameter name.

How do you send query parameters in GET request in Rest assured?

Rest Assured queryParam() To include Query String Request Parameters into our HTTP GET request we will need to use the queryParam(). The queryParam() function accepts key-value pair where the key is the name of the request parameter and the value is the value of the request parameter.


1 Answers

Your URL string is

http://192.168.0.10:8080/MyProject/MyServer/rpi/checktask?rpi="rpi" 

The name of the parameter in the above String is "rpi".

The below code will give you the required value of the parameter "rpi".

String rpi = request.getParameter("rpi");
like image 151
Rohit Gaikwad Avatar answered Sep 17 '22 12:09

Rohit Gaikwad