-2

In PHP all the GET parameters and values can all be easily accessed through $_GET. How about in Java, is there any way to do the same as that? Probably by using JsonObject class.

I hope you guys can help.

Abel Callejo
  • 13,779
  • 10
  • 69
  • 84
  • 2
    *In PHP all the GET parameters and values can all be easily accessed through $_GET.* In your servlet, in your `doGet` method, there's a `HttpServletRequest` parameter, which contains all the request parameters (including the query string parameters). Note that this has nothing to do with retrieving the request parameters as a Json Object. – Luiggi Mendoza Nov 28 '13 at 15:22
  • Have you checked, you know, basic documentation or one of the millions of tutorials on Java and the web? `I think I'm not alone with this one.` I disagree. – Taylor Nov 28 '13 at 15:26
  • Yes, I have read about that one but my concern is say a single get parameter is an array. It can be 1-dimensional or multi-dimensional. Is it really necessary to just examine the data structure of every query parameter? – Abel Callejo Nov 28 '13 at 15:27
  • 1
    @AbelMelquiadesCallejo Surely you should know what parameters to expect and be coding your servlet to handle them accordingly. Alternatively you could likely use `HttpServletRequest#getParameterMap` which should return String (parameter name) to String[] (parameter values) mappings, regardless of the number of actual values. – Anthony Grist Nov 28 '13 at 15:34

1 Answers1

0

In PHP all the GET parameters and values can all be easily accessed through $_GET.

In Java, more specifically in plain Java EE, you will use a Servlet to process the requests in server side, never do it directly in your JSP by using scriptlets, avoid its usage. In your servlet, you will have a doGet method that receives a HttpServletRequest parameter that gives you access to all the request parameters (including the query string parameters) by usage of getParameter method. Here's a small sample:

@WebServlet("/hello")
public class HelloServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //retrieving the query string parameter "name"
        //this will work if you access to http://your.host.url/app/hello?name=foo
        String nameQueryStringParameterValue = request.getParameter("name");
        //will print "foo" (or the query string parameter value) if the url contains it
        //otherwise, it will print null
        //this will be printed in console output
        //Note: in real world apps, you should use a logger instead of sysout
        System.out.println(nameQueryStringParameterValue);
        //continue your request processing logic...
    }
}

In case you don't want/need to manually ask for every request parameter, you may use a web MVC framework that helps you handling this. For example, JSF <f:viewParam> as shown here: What can <f:metadata>, <f:viewParam> and <f:viewAction> be used for?.

Note that this has nothing to do with retrieving the request parameters as a Json Object.

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332