11

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?

Community
  • 1
  • 1
Katrikken
  • 123
  • 1
  • 1
  • 9
  • If you try hitting the url manually (e.g. with a browser) does it work? – Taylor Sep 15 '16 at 13:06
  • It doesn't work both from browser and app. – Katrikken Sep 15 '16 at 13:37
  • My answer was rubbish, sorry. I am a bit confused now and am reading the Servlet Spec, the hundredths time on this topic .... :-( – gsl Sep 15 '16 at 14:21
  • Are you really trying to pass quotes on the URL? That's not a valid URL character. Pass it as %34 instead. See also [this post](http://stackoverflow.com/questions/7109143/what-characters-are-valid-in-a-url) – stdunbar Sep 15 '16 at 14:40
  • Are you sure that you called with the right URL, as shown in the servlet comment, first line? There is "/*" at the end! You intended to use `checktask?rpi=rpi". I tried your source code on a Tomcat 8 and it works out of the box. – gsl Sep 15 '16 at 15:04
  • @stdunbar No, I use URLEncoder for passing values – Katrikken Sep 15 '16 at 15:38
  • @gsl I don't understand what you mean by right URL. I use Tomcat 9, but will try on Tomcat 8 the same thing, may be this is the problem. – Katrikken Sep 15 '16 at 15:38
  • Well, deleting "name" from the annotation did solve the problem, although I have no idea why. – Katrikken Sep 15 '16 at 16:08
  • Have a look at [http://stackoverflow.com/questions/6535676/webservlet-annotation-with-tomcat-7](http://stackoverflow.com/questions/6535676/webservlet-annotation-with-tomcat-7), it discusses the "name" attribute of the @WebServlet annotation. – gsl Sep 16 '16 at 06:48

1 Answers1

19

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");
Rohit Gaikwad
  • 3,677
  • 3
  • 17
  • 40