Search code examples
htmlmysqlgwt

How to include a variable in an html request?


I can't figure out the syntax for using a variable in a html request as opposed to a static value. For example, I have :

CloseableHttpClient httpClient = HttpClients.createDefault();

HttpGet request = new HttpGet("someDatabaseAdress/getUser?id=1");

but when I try:

Int userId = 1;

CloseableHttpClient httpClient = HttpClients.createDefault();

HttpGet request = new HttpGet("someDatabaseAdress/getUser?id=userId");

I get a build error. The query is going out a mysql table with an integer value in the queried parameter. Does anyone know if the syntax I'm using is correct? Or is there perhaps a better way to go about this?


Solution

  • Everything within the quotes is treated as a string, so here it tries to make the id value (which should be an int) the string "userId".

    HttpGet request = new HttpGet("someDatabaseAdress/getUser?id=" + userId);

    should fix it by adding the value itself rather than its name to the string.