An Id is passing in as a Url parameter. I try to make sure that the id is an number. If not redirect to the main page
if(facilityId != null){
try{
Long.parseLong(facilityId);
}catch(NumberFormatException e){
try {
FacesContext.getCurrentInstance().getExternalContext().redirect("DisplayList.jsf");
} catch (IOException ex) {}
}
facility = documentSBean.findFacilityById(Long.parseLong(facilityId));
...
}
so if I pass in an id like this
www....?facilityId=3?sdfasfda
I catch that 3?sdfasfda is not a number, and get to the redirect statement, but it does not redirect right a way, it execute the next couple lines which try to convert 3?sdfasfda to a Long, hence yield NumberFormatException. So is there a way to force redirect right away, or is there some other way to solve this problem. Wish that there is an else after a catch 😀 :D. The above codes are inside my @PostConstruct init() method
Yes, simply
returnfrom the method:When you invoke
redirect(..)the only thing that happens is that a special header (Location) is set in the response object. But the flow of the invoking method continues, unless youreturnfrom it after you call redirect.