Getting and validating request parameters (Events)

Very often, you write OFBiz Events so as to have complete control over HTTP/HTTPS request parameters. Getting those parameters out of the HttpServletRequest object and into your program is simplified when using OFBiz tools.

How to do it...

The following code snippet demonstrates taking HttpServletRequest object request parameters from the request and placing them in local variables for processing:

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.ofbiz.base.util.UtilHttp;
import org.ofbiz.base.util.UtilValidate
public static String myEvent(HttpServletRequest request,
HttpServletResponse response) {
// Some code up here
Map httpParams = UtilHttp.getParameterMap(request);
// use the OFBiz utility to make this easy
String param1 = (String) httpParams.get("param1");
// Some Validation Examples follow
if(UtilValidate.isEmpty((String) httpParams.get("param2")) {
return "error";
}
if(UtilValidate.isNotEmpty((String) httpParams.get("param3")) {
return "error";
}
if(UtilValidate.isEmail((String) httpParams.get("param4")) {
String emailAddr = (String) httpParams.get("param4"));
// do some processing here
}
return "success";
}

How it works...

The HttpServletRequest object is passed to the OFBiz Event (Java method) as an input parameter where it may be retrieved directly using HttpServletRequest object API calls. To make the programmer's job even easier, OFBiz provides a utility that allows for moving all the request parameters into a local Java Map structure as shown earlier.

There's more...

For a full accounting of the HttpServletRequest object, please see the Java servlet API:

http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/http/HttpServletRequest.html

For more information on available org.ofbiz.util.UtilValidate methods, please see the OFBiz Javadocs available online at:

http://ci.apache.org/projects/ofbiz/site/javadocs/

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.249.174