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
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
If you are using Servlets, you can use
ServletRequest.getParameter(...)
If you use JSP, you can use the request
implicit object
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();
}
}
}
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.
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.