-1

In PHP, i've been able to use GET methods fairly easily. For example:

www.test.com/test.php?id=29328932

And then using that information i can do this in PHP to get the value:

$id = $_GET["id"];

In Java, i have a similar link:

www.test.com/Test?id=8127

However, i'm finding it a little difficult to figure out how to get the variable/value from the parameter. Is it possible to do this in Java?

Anuj Hari
  • 543
  • 3
  • 9
  • 19

2 Answers2

3

if you are in a Servlet environment, you have a request object, you can try:

request.getParameter("id");

If you are parsing this from plain java, you could use something like what's described here:

How to extract parameters from a given url

Community
  • 1
  • 1
unify
  • 6,161
  • 4
  • 33
  • 34
  • Okay, so getting the id using getParameter inside the doGet(). That's further than i was before. Thanks :). However, now i'm trying to use this ID variable that i have inside a private function on the same page. How can i make it so that this variable can be used inside this function? – Anuj Hari May 27 '14 at 23:28
2

Inside a servlet, use ServletRequest#getParameter:

public class YourServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        String id = request.getParameter("id");
        //use the parameter
    }
}

Inside JSP, use param variable from Expression Language:

id: ${param['id']}
<br />
id: ${param.id}
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • Thanks, this is pretty much the same as the answer above. I have commented above on an issue leading from this one. If you could give me any pointers as to how to resolve it; would save myself a lot of time :). Thanks – Anuj Hari May 27 '14 at 23:31