tags:

views:

120

answers:

4

HI,

I have a URL which will call a method my method in Java.

it looks like this

http://www.example.com/method?value=24

how can i retrieve value 24 and use it in my called method?

I am using Wicket Framework

Thanks

A: 

If you are using Servlets, you can use

ServletRequest.getParameter(...)

If you use JSP, you can use the request implicit object

request.getParameter(...)

bdhar
sorry dude. I forgot to inform you before that its wicket. But thanks for help.
salman
A: 

Something like this:

public class ExampleServlet extends HttpServlet {

   /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    */
   protected void service(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

       String s = request.getParameter("value");
       int value = 0;
       try {
            value = Integer.parseInt(s);
       } catch(NumberFormatException nfe) {
            nfe.printStackTrace();
       }
   }
}
Jeroen Rosenberg
True for generic servlet development, false for wicket
seanizer
You're right, but at the time I answered he didn't specify that he used a framework.
Jeroen Rosenberg
I know, and I'd leave your answer in place because it's a valid answer for many scenarios (just not this one)
seanizer
sorry guys its my fault i forgot to write the framework.. thanks for the help.. @jeroen Rosenberg
salman
+5  A: 

OK, the way to access a request parameter in wicket is this:

final Request request = RequestCycle.get().getRequest();
final String valueAsString = request.getParameter("value");
int value = Integer.parseInt(valueAsString);

But usually you don't have to do that, because you pass parameters by using BookmarkablePageLinks with PageParameters objects and read those objects from the page constructors. Read this page for some material on Bookmarkable Links in Wicket.

seanizer
thank man, U are awesome. and thanks for the link. This wicket link is very usefull. thanks again.
salman
Reading "Wicket in Action" is even more useful: http://www.manning.com/dashorst/
seanizer
In the above script am getting nullpointerException at the 1st line RequestCycle.get().getRequest(); this is my code. final Request request = RequestCycle.get().getRequest();final String value = request.getParameter("value");logger.info(" value from URL : " + value);and my URL is http://localhost:8080/method?value="test"
salman
Is this code inside a wicket component? i.e. is there a current request?
seanizer
I can actually invoke one of the method and can able to print a log msg just before the above code.
salman
Read these about RequestCycle: https://cwiki.apache.org/WICKET/request-cycle-and-request-cycle-processor.html https://cwiki.apache.org/WICKET/lifecycle-of-a-wicket-application.html http://wicket.apache.org/apidocs/1.4/org/apache/wicket/RequestCycle.html
seanizer
@salman: OK, you un-accepted my answer, but you didn't answer my questions. How do you test the code: is the web app running or are you using WicketTester? If you just paste it into some main class than of course you don't have a request. (There's usually no request in a Component Constructor, either)
seanizer
+2  A: 

I am assuming here that you do create the number - the '24' - in your Java code, since you say you are using Wicket.

Thus, as seanizer already said, in 99% of the cases, you do not need to parse the request url to get the value, something like this should be sufficient:

public class MyPage extends WebPage {
   public MyPage() {
      // your number is generated somehow
      final int value = 24;

      this.add(new Link<Integer>("myLink") {
         @Override
         public void onClick() {
            // do something with the value
            int newValue = value * 2;
         }
      }
   }
}

or - with models - like this

public class MyPage extends WebPage {
       public MyPage() {
          // your number is generated somehow
          int value = 24;

          Model<Integer> model = new Model<Integer>(value);

          this.add(new Link<Integer>("myLink", model) {
             @Override
             public void onClick() {
                // your '24'
                Integer value = this.getModelObject();
                // do something with the value
                int newValue = value * 2;
             }
          }
       }
    }

If you really, really, REALLY-REALLY do need the parameter from the URL, I guess this is what you want:

public class MyPage extends WebPage {
       public MyPage(PageParameters parameters) {
          // your number is generated somehow
          Integer value = parameters.getAsInteger("value");
       }
    }

Depending on how you configured your application, you might need to implement the other constructors accordingly.

Good luck.

LeChe