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

The server receives requests from two clients - Raspberry Pi and Android-application, both send requests using HttpURLConnection. I need to pass parameters with these requests, for example:

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

does it like:

 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 an annotation:

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

But when the Servlet receives the request, as indicated above, 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 these answers: How to use @WebServlet to accept arguments (using the RESTFul method)? and also Why does the request.getPathInfo () method in the service method return 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?

+6
source share
1 answer

Your url string

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

The parameter name in the line above: " rpi ".

The code below will give you the required rpi value.

 String rpi = request.getParameter("rpi"); 
+8
source

All Articles